GreenDao实操案例

GreenDao实操案例

GreenDao实际项目使用

  • 自动生成数据对象
  • 自带版本更新机制(数据自动迁移),可为一键升级
  • 自带FaceBook stetho的数据库查询调试工具

一.构建项目(已有的请开车到下面👇)

// 添加依赖到主项目gradle

compile'org.greenrobot:greendao:3.2.0'

compile 'com.facebook.stetho:stetho:1.3.1'

初始化 stetho 在Applcation中进行初始化的操作
Stetho.initializeWithDefaults(this)

二.自动生成所有的对象

greendao-generato 是一个 javalib moudle
右键run ~

  • 自动生成引用的Bean对象

  • 自动生成引用的Dao

  • 自动生成DaoMaster DaoSession管理类并且能想到的都给添加好了(只能看着它静静的帮你生成)

  • 不行我要敲代码,好的。

    DatabaseManager管理类做统一管理
    最后Applcation初始化

`

public static void main(String[] args) throws Exception {
    Schema schema = new Schema(2, "com.classliu.greendao.db.bean"); //bean对象的路劲
    schema.setDefaultJavaPackageDao("com.classliu.greendao.db.dao"); //dao对象路径
    addTest(schema);
    addTest2(schema);
    addTest3(schema);
    addTest4(schema);
    addTest5(schema);

    //H:\demo\GreenDao\app\src
    //H:\demo\GreenDao\app\src\main\java-gen
    new DaoGenerator().generateAll(schema, "\\H:\\demo\\GreenDao\\app\\src\\main\\java-gen");
}



//生成bean对象
private static void addTest(Schema schema) {
    Entity testData = schema.addEntity("TestData"); //
    testData.addIdProperty().primaryKey().autoincrement();//primaryKey
    testData.addStringProperty("reader"); //reader 是 string 类型
    testData.addLongProperty("testLong"); //testLong 是 long 类型
    testData.addDateProperty("testDate");//testDate 是long 类型
    testData.addIntProperty("testInt");// testInt 是int 类型
    testData.addBooleanProperty("testBoolean"); //testBoolean 是布尔类型
    testData.addIntProperty("creatId"); 
}
private static void addTest2(Schema schema) {
    Entity testData = schema.addEntity("TestData2");
    testData.addIdProperty().primaryKey().autoincrement();
    testData.addStringProperty("testString");
    testData.addLongProperty("testLong");
    testData.addDateProperty("testDate");
    testData.addIntProperty("testInt");
    testData.addBooleanProperty("testBoolean");
}
private static void addTest3(Schema schema) {
    Entity testData = schema.addEntity("TestData3");
    testData.addIdProperty().primaryKey().autoincrement();
    testData.addStringProperty("testString");
    testData.addLongProperty("testLong");
    testData.addDateProperty("testDate");
    testData.addIntProperty("testInt");
    testData.addBooleanProperty("testBoolean");
}

private static void addTest4( Schema schema) {
    Entity testData = schema.addEntity("TestData4");
    testData.addIdProperty().primaryKey().autoincrement();
    testData.addStringProperty("reader");
    testData.addStringProperty("readerString");
    testData.addIntProperty("testInt");
    testData.addBooleanProperty("testBoolean");
}


private static void addTest5(Schema schema) {
    Entity testData = schema.addEntity("TestData5");
    testData.addIdProperty().primaryKey().autoincrement();
    testData.addStringProperty("testString");
    testData.addLongProperty("testLong");
    testData.addDateProperty("testDate");
    testData.addIntProperty("testInt");
    testData.addBooleanProperty("testBoolean");
}

`

三.数据库升级(数据迁移)

MySQLiteOpenHelper extends DaoMaster.OpenHelper 重写DaoMaster.OpenHelper
在onUpgrade方法中写出符合项目需求的升级操作,因为项目各异所有工具类的使用需要根据自身的要求量身需改。

  • 增加删table 只需要删除 添加class文件(可变参数的个数)
  • 增加删table 中的列只需要修改对应table的变量
    一句话总结:修改你的Bean对象,就欧了

四.结合标准MVP结构

  • 模拟网络数据的请求,可结合至实际项目
    数据库的数据操作,可与实际的网络请求相对接,方便易用,真实可靠。(具体的操作)
    Demo中主要提供一些关于数据库的操作
    增加,删除所有 ,
    具体的增删改查的操作

  • 大批量的更新数据库耗时优化体验

    项目中提供增加10000条数据、异步增加10000条数据
    异步多线程操作方式,开启三个线程池同时进行数据库的操作

五.总结

经实际测试:

  • 数据迁移的时间在 25——30ms之间(1000条)

  • 直接插入10000条数据大约耗时 900--950ms 异步工具耗时是 1/100(运行Demo效果更感人)
    你需要亲自运行一下代码~

    拥抱开源,感谢GreenDao !!!我不是代码的创造者,我是工具宣传员

    Demo源码地址


感谢:如果喜欢本文加个收藏关注, Github star一下 Orz

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、关于greenDAO greenDAO应该算是当前最火的数据库开源框架了,它是一个将对象映射到SQLite数据...
    当幸福来敲门58阅读 14,714评论 3 19
  • 前段时间工作中接触到了数据库greendao,将项目中所有原生sqlite替换成为了greendao数据库封装框架...
    ya_nn阅读 15,183评论 6 33
  • (一)GreenDao简介 GreenDao是一个对象关系映射(ORM)的开源框架,目前最主流的安卓数据库操作框架...
    miss2008阅读 10,795评论 4 18
  • 浮生无常 红尘似锦随风葬 玉骨石心终要丧 千万悲伤 明月锁永长 莫道共赏 寒梅疏影不弄墙 云淡星暗舞未央 抹去轻狂...
    君兮阅读 2,828评论 6 7
  • 我小时候的东北秋天,就像是定好的时钟。到了某一天,家家户户都开始买几十斤的大葱,白菜,准备过冬。那时候没有四季大棚...
    樱桃小独角阅读 3,424评论 2 4

友情链接更多精彩内容