LitePal学习总结————怕忘记
1.集成添加依赖
api 'com.alibaba:arouter-api:1.5.0'
2.初始化
在Application中LitePal.initialize(this);
3.创建表
LitePal.getDatabase();
4.在assets文件夹下添加litepal.xml
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="imMine" />
<!--
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="14" />
<!--
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" />
<mapping class="com.test.model.Magazine" />
</list>
-->
<list>
<mapping class="com.im.immine.been.MsgConversation" />
<mapping class="com.im.immine.been.MsgContextBean" />
</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" />
-->
<storage value="external" />
</litepal>
只要数据库表发生变化就要修改版本号
5.创建表对应的bean 实现 继承extends DataSupport
6.增删改查写法可以在网上搜索有很多,同时支持sql语句
7.特殊表关联查询
https://www.jianshu.com/p/34b3fd13de68
public class Person extends DataBaseModel{
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class User extends DataBaseModel{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Person extends DataBaseModel{
//person表中自动生成列名为id的自增key,此处拿出来是为了方便子表查询
private long id;
private String name;
private String sex;
private User user;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//1对1表结构
public User getUser() {
//子表中会生成一个关联父表的id供父表查询,且字表中id生成符合规则:"父表类名小写_id"
//若父表为Person类(父表中会自动生成一个id自增列),子表为User类,则字表中会自动生成字段person_id对应父表中id,以供查询
String linkId=this.getClass().getSimpleName().toLowerCase();
List<User>list= DataSupport.where(linkId+"_id=?",String.valueOf(id)).find(User.class);
if(CollectionUtil.isEmpty(list)){
user= null;
}else{
user=list.get(0);
}
return user;
}
public void setUser(User user) {
//set的时候存储子表数据
user.save();
this.user = user;
}
}
1对多表结构
public class Person extends DataBaseModel{
//person表中自动生成列名为id的自增key,此处拿出来是为了方便子表查询
private long id;
private String name;
private String sex;
private List<User>userList;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<User> getUserList() {
//子表中会生成一个关联父表的id供父表查询,且字表中id生成符合规则:"父表类名小写_id"
//若父表为Person类(父表中会自动生成一个id自增列),子表为User类,则字表中会自动生成字段person_id对应父表中id,以供查询
String linkId=this.getClass().getSimpleName().toLowerCase();
List<User>list= DataSupport.where(linkId+"_id=?",String.valueOf(id)).find(User.class);
if(list==null){
list=new ArrayList<>();
}
return list;
}
public void setUserList(List<User> userList) {
//批量存储userList
if(!CollectionUtil.isEmpty(userList)){
DataSupport.saveAll(userList);
}
this.userList = userList;
}
}
注意想使用sql查询语句的话注意
Cursor bySQL = DataSupport.findBySQL("select * FROM msgconversation INNER JOIN msgcontextbean on msgconversation.id=msgcontextbean.msgconversation_id");
List<MsgConversation> s = new ArrayList<>();
LogUtil.e(bySQL.getCount() + "bySQL.getCount()");
if (bySQL!=null&&bySQL.getCount()>0&&bySQL.moveToFirst()) {
for (int i = 0; i < bySQL.getCount(); i++) {
int msgReceiverId = bySQL.getColumnIndex("msgreceiverid");//数据库列明
String string = bySQL.getString(msgReceiverId);
LogUtil.e(string + ""+i);
}
}
bySQL.close();