LitePal学习笔记

来源:互联网 发布:html5斗地主源码 编辑:程序博客网 时间:2024/05/16 13:38

Litepal是一个ORM框架。写个学习笔记增加记忆,分享成果。Litepal支持数据类型如下:

booleanjava.lang.Booleanfloatjava.lang.Floatdoublejava.lang.Doubleintjava.lang.Integerlongjava.lang.Longshortjava.lang.Shortcharjava.lang.Characterjava.lang.Stringjava.util.Date

一,准备工作。

1、eclipse:下载最新的JAR包,引入Eclipse。Anndroid Studio, 在build.gradle中添加如下

dependencies {    compile 'org.litepal.android:core:1.3.0'}

2、编辑配置文件

在assets文件夹中创建文件:litepal.xml。内容如下。可以直接拷贝下面内容到你的文件中

<?xml version="1.0" encoding="utf-8"?><litepal>    <!--      Define the database name of your application.       By default each database name should be end with .db.       If you didn't name your database end with .db,       LitePal would plus the suffix automaticly for you.      For example:           <dbname value="demo" ></dbname>    -->    <dbname value="sample" ></dbname>    <!--       Define the version of your database. Each time you want        to upgrade your database, the version tag would helps.       Modify the models you defined in the mapping tag, and just        make the version value plus one, the upgrade of database       will be processed automaticly without concern.      For example:           <version value="1" ></version>    -->    <version value="1" ></version>    <!--       Define your models in the list with mapping tag, LitePal will       create tables for each mapping class. The supported fields       defined in models will be mapped into columns.       For example:        -->    <list>        <mapping class="org.litepal.litepalsample.model.Album"></mapping>        <mapping class="org.litepal.litepalsample.model.Song"></mapping>        <mapping class="org.litepal.litepalsample.model.Singer"></mapping>        <!--      <mapping class="com.litepaltest.model.Classroom"></mapping>              <mapping class="com.litepaltest.model.Teacher" ></mapping>              <mapping class="com.litepaltest.model.Product"></mapping>-->    </list>    <!--       Define the cases of the tables and columns name. Java is a       case sensitive language, while database is case insensitive.       LitePal will turn all classes names and fields names into lowercase       by default while creating or upgrading database. Developers can change       this behavior into the styles their like. "keep" will keep the       cases of classes and fields. "upper" will turn all classes names       and fields names into uppercase. "lower" will act as default.       Do not change the value after you run your app for the first time,       or it might cause the exception that column can not be found.       value options: keep lower upper       For example:           <cases value="lower" ></cases>             Note that this tag is optional for developers.    --></litepal>

如注释:dbname对应数据库名字(别用.db做名字的结尾),version对应版本,list中写入映射模型。

3,配置Application

方案a:如果你没有自己的Application,可以在AndroidManifest.xml 中直接配置LitePalApplication,如下:

<application    android:name="org.litepal.LitePalApplication"    android:icon="@drawable/logo"    android:label="@string/app_name"
方案b:如果有自己的Application,可以修改继承关系,继承LitePalApplication。如下:

public class MyApplication extends LitePalApplication{
方案c:在你的Application中的onCreate中初始化LitePalApplication。如下:

public class MyApplication extends Application{    @Override    public void onCreate() {        super.onCreate();        LitePalApplication.initialize(this);    }}

二、开始使用。

1、创建表。

首先,想创建表的需先定义好model。

因为Litepal是对象关系映射(ORM)框架,根据javabean创建表。另外,不是Litepal支持的类型,是不会创建相应的加了如下注解是不会创建对应的列的。

@Column(ignore = true)

例如下:

public class News extends DataSupport{    private int id;    private String title;    private String content;    private Date publishDate;    private int commentCount;    // 自动生成get、set方法    ...}

其次、在配置文件中litepal.xml的list中添加(如上文)。如果是如需更新数据库,只需写明数据库版本,litepal会自动更新数据库,甚至包括删除列!

2、升级表

只需要按你自己的需求修改model,然后更新litepal中的数据库版本。当升级后进行数据库操作的时候,litepal会自动为您修改表的结构。例如下面,添加一个header列,减少个tetile列。

public class News {    private int id;//    private String title;    private String header;    private String content;    private Date publishDate;    private int commentCount;    // 自动生成get、set方法}
然后修改litepal.xml

<litepal>    <dbname value="sample" ></dbname>    <version value="2" ></version><!-- 这里 -->    <list>        <mapping class="org.litepal.litepalsample.model.Album"></mapping>    </list></litepal>
特别注意:以下情况litepal可能没发处理并且会导致数据丢失。

a,添加属性,并该属性有注解,unique = true

b,修改一个属性的注解,unique = true.

c,修改一个属性的注解,nullable = false.

3、保存数据

model在继承了org.litepal.crud.DataSupport类后,可以直接调用save方法保存到数据库。

News news = new News();news.setHeader("Fk");news.setContent("i am crazy at my body");news.save();

4、更新数据

a,可以先查询出对应的数据,修改后保存。

News temp = DataSupport.find(News.class, 1);temp.setHeader("New header");temp.save();
b,可以先创建数据,修改后更新到指定id的数据中。

News newsId = new News();newsId.setHeader("New Header");newsId.update(1);

c,可以批量修改数据。先创建一个对象,修改后更新到符合条件的数据上。

News newsUpdate = new News();newsUpdate.setHeader("New Header");newsUpdate.updateAll("header = ?", "Fk");

5、删除数据

a,删除指定数据

DataSupport.delete(News.class, id);

b,删除符合条件数据

DataSupport.deleteAll(News.class, "name = ?", "FK");
c,删除表中全部数据

DataSupport.deleteAll(News.class);

6、查询数据

// 查询// 根据id查询单条数据News newsQuery = DataSupport.find(News.class,id);// 查询一个表中所有数据List<News> newsAll = DataSupport.findAll(News.class);// 查询符合条件的所有数据List<News> newsList =DataSupport.where("name = ?", "Fk").find(News.class);

本文使用的是 litepal 1.3.0,项目地址:https://github.com/LitePalFramework/LitePal


我自己的使用体验,创建数据库的时候会有明显的不到0.5秒的卡顿。

参考文章:1、项目地址,2、郭霖的博客 Android数据库高手秘籍(二)——创建表和LitePal的基本用法,3、译文Android LitePal介绍与使用说明


0 0
原创粉丝点击