赞
踩
部分内容参考博客https://blog.csdn.net/shineflowers/article/details/53405644?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
1,在项目的build.gradle中添加greendao插件依赖
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
2,在项目app的build.gradle中添加GreenDao依赖库
implementation 'org.greenrobot:greendao:3.3.0'
implementation 'org.greenrobot:greendao-generator:3.3.0'
3,在项目app的build.gradle中配置grennDao的dao层文件存放位置
greendao {
schemaVersion 1 //版本号
daoPackage 'com.example.greendao.gen' //自动生成的数据库相关类的包名
targetGenDir 'src/main/java' //自动生成的数据库的相关灯的目录
}
4,在项目app的build.gradle文件顶部添加
apply plugin: 'org.greenrobot.greendao'
5,添加混淆配置
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties { *; }
# If you DO use SQLCipher:
-keep class org.greenrobot.greendao.database.SqlCipherEncryptedHelper { *; }
# If you do NOT use SQLCipher:
-dontwarn net.sqlcipher.database.**
# If you do NOT use RxJava:
-dontwarn rx.**
通过注解的方式与GreenDao关联,类上需要使用@Entity注解表示此类是与GreenDao关联的类
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
@Entity
public class Person {
//设置Id为自增长,id必须是long类型
@Id(autoincrement = true)
private Long id;
private String name;
private int age;
}
Entity类配置完成后,如果配置正常,clean或者rebuild一下项目,就会在com.example.greendao.gen这个包下生成数据库相关的类文件
private DaoMaster.DevOpenHelper mDevOpenHelper;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
private PersonDao mPersonDao;
public void initDataBase(){
mDevOpenHelper = new DaoMaster.DevOpenHelper(mContext, "person.db", null);
mDaoMaster = new DaoMaster(mDevOpenHelper.getWritableDb());
mDaoSession = mDaoMaster.newSession();
mPersonDao = mDaoSession.getPersonDao();
}
1,insert
public void insert(){ //实例化Entity实体类 mPerson = new Person(); //从表中查询名称为test的数据 Query<Person> query = mPersonDao.queryBuilder().where(PersonDao.Properties.Name.eq("test")).build(); //判断查询结果 Person person = query.unique(); if(person == null){ mPerson.setId(1L); mPerson.setName("test"); mPerson.setAge(18); mPersonDao.insert(mPerson); LogUtil.show("插入数据"); }else { LogUtil.show("数据库中有相同数据,无法重复插入"); } }
2,delete
public void delete(){
//查询得到表中所有数据的集合
List<Person> list = mPersonDao.queryBuilder().build().list();
if(list.size()==0){
LogUtil.show("数据库中没有匹配数据,无法执行删除");
}else {
LogUtil.show("删除数据");
for (Person person : list) {
mPersonDao.delete(person);
}
}
}
3,update
public void update(){ //查询表中包含名称test的数据 Query<Person> query = mPersonDao.queryBuilder().where(PersonDao.Properties.Name.eq("test")).build(); //获取查询结果的entity对象 Person person = query.unique(); if(person != null){ int id = person.getId().intValue(); person.setAge(22); person.setName("aa"); person.setId((long) id); mPersonDao.insertOrReplace(person); LogUtil.show("更新数据"); }else { LogUtil.show("表中无相同数据"); } }
4,query
public List<Person> query(){
//查询表中所有数据的Entity对象集合
List<Person> mList = mPersonDao.queryBuilder().list();
return mList;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。