LitePal学习总结————怕忘记

1.集成添加依赖
api 'com.alibaba:arouter-api:1.5.0'
2.初始化
在Application中LitePal.initialize(this);
3.创建表
LitePal.getDatabase();
4.在assets文件夹下添加litepal.xml








    
    





只要数据库表发生变化就要修改版本号
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();
Listlist= 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 ListuserList;

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 getUserList() {
    //子表中会生成一个关联父表的id供父表查询,且字表中id生成符合规则:"父表类名小写_id"
    //若父表为Person类(父表中会自动生成一个id自增列),子表为User类,则字表中会自动生成字段person_id对应父表中id,以供查询
    String linkId=this.getClass().getSimpleName().toLowerCase();
    Listlist= DataSupport.where(linkId+"_id=?",String.valueOf(id)).find(User.class);
    if(list==null){
        list=new ArrayList<>();
    }
    return list;
}

public void setUserList(List 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 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();

你可能感兴趣的:(LitePal学习总结————怕忘记)