GreenDao数据库框架的使用

xiaoxiao2021-02-28  74

greenDAO 可以将我们数据库的中数据转换为相应的对象,这样可以省去我们自己去转换对象的时间。和同类型的库相比,性能是最好的。(官网如是说)主流同类型库的条形对比图如下图。greenDAO 也是当前 Android 开发中比较流行的库。使用 SQL 语句进行查询容易出错,而且错误比较难以发现,使用 greenDAO 的话可以在编译阶段就发现错误。(官网说的)还有就是代码比较简单明了,程序员都想用最少的代码,做最多的事。轻量级,整个库大小小于 150 k。

一、引用 greenDAO

当前的 greenDAO 要求 gradle 的版本至少是 3.3!!

1.在as中导入相关的包

apply plugin: 'org.greenrobot.greendao' apply plugin: 'com.android.application' dependencies { compile 'org.greenrobot:greendao:3.2.2' }

2.在build.gradle中进行配置:

buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }

二、实体类的生成

@Entity public class StudentMsgBean { @Id private Long id; @Property(nameInDb = "STUDENTNUM") private String studentNum; @Property(nameInDb = "NAME") private String name; @Generated(hash = 381350025) public StudentMsgBean(Long id, String studentNum, String name) { this.id = id; this.studentNum = studentNum; this.name = name; } @Generated(hash = 160565988) public StudentMsgBean() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getStudentNum() { return this.studentNum; } public void setStudentNum(String studentNum) { this.studentNum = studentNum; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }

实体类中常用的注解:

@Entity  表明这个实体类会在数据库中生成一个与之相对应的表。

@Id  对应数据表中的 Id 字段,有了解数据库的话,是一条数据的唯一标识。

@Property(nameInDb = “STUDENTNUM”)  表名这个属性对应数据表中的 STUDENTNUM 字段。

@Property  可以自定义字段名,注意外键不能使用该属性

@NotNull  该属性值不能为空

@Transient  该属性不会被存入数据库中

@Unique 表名该属性在数据库中只能有唯一值

三、增删改查(Activity中)

1.初始化

DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "student.db", null); DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb()); DaoSession daoSession = daoMaster.newSession();

StudentMsgBeanDao msgBeanDao = daoSession.getStudentMsgBeanDao(); StudentMsgBean studentMsgBean = new StudentMsgBean(); studentMsgBean.setName("zone"); studentMsgBean.setStudentNum("123456"); msgBeanDao.insert(studentMsgBean);

删除

List<StudentMsgBean> list2 = msgBeanDao.queryBuilder() .build().list(); for (int i = 0; i < list2.size(); i++) { String name = list2.get(i).getName(); if (name.equals("zone")) { msgBeanDao.deleteByKey(list2.get(i).getId());//通过 Id 来删除数据 /*msgBeanDao.delete(list2.get(i));//通过传入实体类的实例来删除数据*/ } }

List<StudentMsgBean> list3 = msgBeanDao.queryBuilder() /*.offset(1)//偏移量,相当于 SQL 语句中的 skip .limit(3)//只获取结果集的前 3 个数据 .orderAsc(StudentMsgBeanDao.Properties.StudentNum)//通过 StudentNum 这个属性进行正序排序 .where(StudentMsgBeanDao.Properties.Name.eq("zone"))//数据筛选,只获取 Name = "zone" 的数据。*/ .build() .list(); for (int i = 0; i < list3.size(); i++) { list3.get(i).setStudentNum("zone==========>"); msgBeanDao.update(list3.get(i)); }

List<StudentMsgBean> list = msgBeanDao.queryBuilder() .offset(1)//偏移量,相当于 SQL 语句中的 skip .limit(3)//只获取结果集的前 3 个数据 .orderAsc(StudentMsgBeanDao.Properties.StudentNum)//通过 StudentNum 这个属性进行正序排序 .where(StudentMsgBeanDao.Properties.Name.eq("zone"))//数据筛选,只获取 Name = "zone" 的数据。 .build() .list();

源码下载地址:https://github.com/a2978157/MygreenDAO

转载请注明原文地址: https://www.6miu.com/read-41707.html

最新回复(0)