记录从greendao2.1升级到greendao3.2!

来源:互联网 发布:小学生上网数据 编辑:程序博客网 时间:2024/05/01 11:23

才来公司上班,需求少!时间充裕些,所以寻思把项目使用第三方升级一下!首先是GreenDao

在greendao2.1时代,我们需要在build.gradle中添加以下依赖:

  compile 'de.greenrobot:greendao:2.1.0'

然后新建module,并在module中build.gradle添加greendao生成器

 compile 'de.greenrobot:greendao-generator:2.1.0'
然后新建自己的generator生成器类!例如:

public class MainDaoGenerator {    public static void main(String[] args) throws Exception {        Schema schema = new Schema(1, "放置生成文件的包名");//        addTeacher(schema);        addUser(schema);//用户表        addFile(schema);        addSchedule(schema);        addTeacherCourse(schema);        addTeacherClassRoom(schema);        new DaoGenerator().generateAll(schema, "放置生成文件的位置");    }
每次要新建实体类的时候直接添加,最后选中这个类右键  选择run生成相对应的bean 、beanDao!

最后我们在新建DBHelper帮助类,取得DaoMaster

  public static DaoMaster getDaoMaster(Context context) {        if (daoMaster == null) {            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "my_db.db", null);            daoMaster = new DaoMaster(helper.getWritableDatabase());        }        return daoMaster;    }

取得DaoSession

 /**     * 取得DaoSession     *     * @param context     * @return     */    public static DaoSession getDaoSession(Context context) {        if (daoSession == null) {                if (daoMaster == null) {                    daoMaster = getDaoMaster(context);                }                daoSession = daoMaster.newSession();        }        return daoSession;    }


初始化我们greendao

public static void init(Context context) {        mContext = context;        instance = new DBHelper();        // 数据库对象        daoSession = getDaoSession(mContext);    }
在application中初始化
 public void onCreate() {        super.onCreate();        Logger.init("my");        DBHelper.init(getApplicationContext());         }

在这个BDHelper编写增删查改方法就OK!


升级到3.2!使用的方法就多了一种

首先就是替换module中的

 compile 'de.greenrobot:greendao-generator:2.1.0'
换成
compile 'org.greenrobot:greendao-generator:3.2.0'

同步后在类中替换导入greendao的新包名!编译后运行生成新的注解

最后再主工程build.gradle中替换成

 compile 'org.greenrobot:greendao:3.2.0'
从新替换导入greendao的包名,最后重新编译project,

至此升级可以算是完成了!

但是在greendao3.2中推荐使用以下插件

classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'

有了我们就可以不使用generator了,

使用步揍,按照官方的:

在build.gradle中添加依赖

compile 'org.greenrobot:greendao-generator:3.2.0'
然后在android{

......


    //greendao配置    greendao {        schemaVersion 1                             //版本号,升级时可配置//        daoPackage'com.example.admin.mydaggerdemo.dao'     //包名//        targetGenDir'src/main/java'                 //生成目录           }
greendao默认目录在build-->generated-->source-->greendao中!

........


}

在主project的build.gradle中添加

buildscript {    repositories {        jcenter()    }    dependencies {        .....        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}
然后我们编写我们需要的实体类,并在实体中加入注解@Entity!在随意写入某些属性
@Entitypublic class Test {       @Id   private int id;}
直接运行project就OK,就能自动生成beanDao,并补齐bean!这样加入插件,就可以省去使用genreator了,并且可以学到新的注解姿势!

注解:

@Entity   标识实体类,greenDAO会映射成sqlite的一个表,表名为实体类名的大写形式

@Id 标识主键,该字段的类型为long或Long类型,autoincrement设置是否自动增长

@Property       标识该属性在表中对应的列名称, nameInDb设置名称

@Transient      标识该属性将不会映射到表中,也就是没有这列

@NotNull         设置表中当前列的值不可为空

@Convert         指定自定义类型(@linkPropertyConverter)

@Generated   greenDAO运行所产生的构造函数或者方法,被此标注的代码可以变更或者下次运行时清除

@Index    使用@Index作为一个属性来创建一个索引;定义多列索引(@link Entity#indexes())

@JoinEntity     定义表连接关系

@JoinProperty         定义名称和引用名称属性关系

@Keep     注解的代码段在GreenDao下次运行时保持不变

         1.注解实体类:默认禁止修改此类
          2.注解其他代码段,默认禁止修改注解的代码段

@OrderBy        指定排序

@ToMany         定义与多个实体对象的关系

@ToOne  定义与另一个实体(一个实体对象)的关系

@Unique 向数据库列添加了一个唯一的约束


记录完成整个greendao升级过程,及学习新的使用!由于使用GreenDao毕竟时间不长,肯定有很多不足的地方!欢迎大神们敲砖!


0 0