greenDao简单使用
来源:互联网 发布:linux 建立目录 编辑:程序博客网 时间:2024/05/16 04:26
在项目的build.gradle中添加
compile 'de.greenrobot:greendao:2.1.0'
下载DaoGenerator项目,打开DaoGenerator项目
public static void main(String args[]) throws Exception { Schema schema = new Schema(1, "com.green.bean"); schema.setDefaultJavaPackageDao("com.green.dao"); initStudent(schema); new DaoGenerator().generateAll(schema, args[0]);}private static void initStudent(Schema schema) { Entity student = schema.addEntity("Student"); student.addIntProperty("id").primaryKey().index(); student.addStringProperty("name"); student.addIntProperty("age"); student.addStringProperty("hobby");}DaoGenerator的build.gradle中修改输出目录
def outputDir = "../app/src/main/java-gen"
运行即可生成相应java文件
new Schema()第一个参数是数据库版本,第二个参数是bean的包名
setDefaultJavaPackageDao 设置dao的包名
可以在生成的java文件中加入自己的代码或者改写,当设置了schema.enableKeepSectionsByDefault 的时候
用//KEEP METHODS注释再下次更新的时候将保留代码块
DaoMaster:
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);db = helper.getWritableDatabase();// 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。daoMaster = new DaoMaster(db);daoSession = daoMaster.newSession();首先要创建daomaster,用daomaster创建session
创建daomaster要db,db可以用daomaster.DevOpenHelper生成,执行new DaoMaster.DevOpenHelper就会创建数据库,第二个参数是数据库名字,里面的版本号是Generator中写的,当然可以改。也可以自己写DbOpenHelper。
Daoseesion可以获取不同的dao,在Generator中添加了那些表,就有哪些dao。
例如StudentDao
StudentDao:
StudentDao.count 当前表中数据个数
添加:
StudentDao.insert(student)
StudentDao.insertorreplace(student) 如果有这个主键替换没有添加
查找:
查找返回格式为 List<Student>
StudentDao.loadAll() 查询全表数据
QueryBuilder builder = StudentDao.queryBuilder().where();where中为查询条件:where中参数为可变参数,可以放多个条件,多个where可以用and or 连接
Properties.列名.eq(匹配)/isNotNull(不为空)/isNull(为空)/between(在两者之间)/like(相似)/in(在什么中?)还有一些其他的。。。。QUeryBuilder:这个是管理查找的,可以将查找到的数据列出来,也可以删除
list() 返回数据 List<Student>build() 返回Query<Student>
-----Query.list 返回数据,除了这个基本的还有一些高级用法
buildDelete() 返回DeleteQuery<Student>------DeleteQuery.executeDeleteWithoutDetachingEntities 将查询到的符合条件的删除
buildCount() 返回CountQuery<Student>-------CountQuery.count 返回查询到的数量
buildCursor() 返回CursorQuery<Student>
--------CursorQuery.query 返回Cursor 返回系统的Cursor删除:
StudentDao.deleteAll 删除全表
StudentDao.delete(Student)---|
StudentDao.deleteByKey------|---这两个都要key对应才行,一般根据条件删除 用QueryBuilder
1 0
- greenDAO简单使用经验
- GreenDAO的简单使用
- greenDAO简单使用经验
- greenDao简单使用
- Greendao简单使用
- GreenDao数据库简单使用
- GreenDao使用简单封装
- greenDAO 简单使用配置
- GreenDAO的简单使用
- GreenDao简单配置使用
- GreenDao 3.0简单使用
- GreenDao的简单使用
- greendao的简单使用
- GreenDao的简单使用
- GreenDao 3.0简单使用
- GreenDao简单使用
- GreenDao的简单使用
- GreenDao的简单使用
- java之异常处理机制
- android AsyncTask介绍
- javascript属性类型
- MyBatis知识系列之二:针对MyBatis的CRUD操作
- Android-银联支付
- greenDao简单使用
- 杭电5625 Clarke and chemistry
- *的区别
- 【light-oj】-大数整除(字符串)
- ddwrt+wifidog搭建热点认证系统(无线路由器web认证)
- android studio下的NDK开发详解(一)
- 【自定义标签开发】01-标签简介和开发第一个标签
- (终极)java中自己的一写迷茫的地方(未完)
- java 中的观察者模式