老司机带你学习GreenDao3

我知道你们没有图是不会进来的,就是简单的greenDao3的增删改查 和完全自动化升级字段且保留之前的数据!
IMG_0049.JPG
  • 步骤
  • 1.依赖 首先在project项目中的build.gradle 中添加如下代码
   buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0'
    classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //这个是黄油刀的注解

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}
  • 2.依赖 在modele中的build.gradle 中添加如下代码
      apply plugin: 'com.android.application'
      apply plugin: 'org.greenrobot.greendao'
    android {
      compileSdkVersion 23
      buildToolsVersion "25.0.0"
      
      
        buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}
//greendao配置
greendao {
    schemaVersion 6                       //版本号,升级时可配置
    daoPackage 'star.liuwen.com.endgreendao3.Dao'     //包名
    targetGenDir 'src/main/java'                 //生成目录
  
  
  
dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.0.1'
  compile 'org.greenrobot:greendao:3.2.0'
}
  • 3.然后可以新建一个实体类 然后点击工具栏中的build-----make project 会自动生成相应的dao类

@Entity
public class Test {
    @Id
    private Long id;
    private int url;
    private String name;
    private String desc;
}
  • 切记 id一定是long型的 不然插入数据的时候会报错的
  • 4.升级不删除老数据

public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {


    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        Log.e("greenDAO",
                "Upgrading schema from version " + oldVersion + " to " + newVersion + " by migrating all tables data");
         //完全自动升级本地数据库 要升级那个bean 只需要调用下面这个方法 
        // MigrationHelper.getInstance().migrate(db, xxx.class);
        MigrationHelper.getInstance().migrate(db, TestDao.class);
    }
}
  • 上面图 我是升级了一个desc的字段 发现之前的数据依旧保存 记得 要添加某个实体的字段 schemaVersion 一定要大于当前版本 也就说当前版本是6 那么升级的需 要设置为7 才会有保存之前数据的作用 大家可以多试几次.

  • 5.具体使用方法可以看代码了。代码才是最好的老师

    下面是Demo地址

https://github.com/liuwen370494581/greenDao3

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

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,428评论 19赞 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 181,125评论 25赞 708
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,489评论 6赞 342
  • 5:00~5:30读《精进》P83~94页 5:30~6:30组织出车 返回时买颗卷心菜三个胡罗卜 6:40~7:...
    老鹰飞阅读 184评论 0赞 0
  • 今天我喜欢的一个微博博主发了一条微博,说的是关于职场上的性别歧视。她那么有能力尽责的人都因为有一个3岁的孩子被HR...
    你的喜欢很重要阅读 263评论 0赞 0

友情链接更多精彩内容