LitePal 的使用
LitePal 的简介
LitePal 是郭霖郭神的开源代码。相信大家都知道郭神吧,不知道的,也应该听说过<第一行代码> 这本书吧。LitePal是一款开源的Android数据库框架,它采用了对象关系映射(ORM)的模式,并将我们平时开发时最常用到的一些数据库功能进行了封装,使得不用编写一行SQL语句就可以完成各种建表、増删改查的操作。并且LitePal很“轻”,jar包只有100k不到,而且近乎零配置,这一点和Hibernate这类的框架有很大区别。
1. 导入依赖库
在build.gradle中导入依赖库
如果使用Java:
dependencies {
implementation 'org.litepal.android:java:3.0.0'
}
如果使用Kotlin:
dependencies {
implementation 'org.litepal.android:kotlin:3.0.0'
}
在工程的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 automatically for you.
For example:
<dbname value="demo" /> 数据库名字
-->
<dbname value="demo" />
<!--
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 automatically without concern.
For example:
<version value="1" />
-->
<version value="1" />
<!--
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="com.test.model.Reader" /> 表名对应的model
<mapping class="com.test.model.Magazine" />
</list>
-->
<list>
</list>
<!--
Define where the .db file should be. "internal" means the .db file
will be stored in the database folder of internal storage which no
one can access. "external" means the .db file will be stored in the
path to the directory on the primary external storage device where
the application can place persistent files it owns which everyone
can access. "internal" will act as default.
For example:
<storage value="external" />
-->
</litepal>
3. 配置LitePalApplication(有两种方式)
1). 在AndroidManifest.xml 中配置,用这种方式的话,所有的数据库操作就不用传Context 了
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>
2). 如果你的应用程序已经有Application 了。则可以让其继承LitePalApplication 。如果继承的是Application。则可以在onCreate 方法里面加入LitePal.initialize(this);
public class MyOwnApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
...
}
4.建表
使用LitePal 建表非常简单,因为LitePal 把每一个表都和一个Model 映射到一块了。因为一个数据库中有很多字段,model中也有。例如:
public class CategoryInfo extends LitePalSupport {
// 继承 LitePalSupport
String title;
String description;
String imageUrl;
String albumId;
String filter;
String nextPageToken;
boolean exist;
public long getObjectId() {
return getBaseObjId();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
配置XML
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--db name-->
<dbname value="mmp" />
<!--database version-->
<version value="1" />
<!-- database table-->
<list>
<mapping class="com.smartdevice.multimediaplayer.models.CategoryAndCardInfo"/> //要写全名字路径
</list>
</litepal>
5. 保存
CategoryInfo categoryInfo = new CategoryInfo();
categoryInfo.setTitle("title");
categoryInfo.setDescription("sdff");
categoryInfo.save();
6. 更新
categoryInfo.update(id);
7. 查找
查询某一条数据 Song song = LitePal.find(Song.class, id);
查询整个表 List<Song> allSongs = LitePal.findAll(Song.class);
条件查询 List<Song> songs = LitePal.where("name like ? and duration < ?", "song%", "200").order("duration").find(Song.class);
8. 删除
LitePal.delete(Song.class, id);
条件删除 LitePal.deleteAll(Song.class, "duration > ?" , "350");
9.如果要混淆的话,需在Proguard 中加入
-keep class org.litepal.** {
*;
}
-keep class * extends org.litepal.crud.DataSupport {
*;
}
-keep class * extends org.litepal.crud.LitePalSupport {
*;
}