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

你可能感兴趣的:(GreenDao实操案例)