GreenDao数据库框架的使用
来源:互联网 发布:激光雷达数据 编辑:程序博客网 时间:2024/05/16 05:26
- 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 }}
二、实体类的生成
@Entitypublic 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
阅读全文
0 0
- GreenDao数据库框架的使用
- greenDao 数据库框架 使用
- Android GreenDao数据库框架使用
- 使用ORM数据库框架greenDao
- 【开发框架】GreenDao数据库框架的搭建和使用
- GreenDao数据库的使用
- GreenDao数据库的使用
- GreenDao(数据库)的使用
- 应用数据库管理ORM框架---GreenDao的使用
- Android操作数据库的框架-GreenDao基本使用记录
- AndroidStudio 离线情况下使用GreenDao数据库框架的方式
- 数据库开源框架GreenDao的使用解析
- Android 初使用GreenDAO框架操作数据库
- android高效ORM数据库框架greenDao使用
- Android studio使用greendao作为数据库框架
- Android数据库框架GreenDao使用详解
- 数据库之greenDAO 的使用
- Android--------GreenDao数据库的使用
- 日期类
- 微信小程序--后端管理平台业务1
- 查看django 是什么版本
- Elasticsearch过滤与聚合的先后顺序java实现
- hdu 6183 Color it(cdq分治)
- GreenDao数据库框架的使用
- MFC 程序开发
- linux中ftp提示--553 Could not create file (绝对有用)
- java经典设计模式案例解析
- window7 tomcat8 配置python27的cgi
- Android学习:线程同步之synchronized
- 转载 c++11实现自旋锁
- redis优缺点
- jsp + servlet 文件上传