1.GreenDao3介绍
(1)基本概念
(2)GreenDao3工作原理
(3)GreenDao优点
(4)GreenDao3版本的改进
2.GreenDao3的相关配置概念介绍
(1)配置项目(Project)的build.gradle
(2)配置模组(Module)的build.gradle
3.GreenDao3配置实战
(1)
(2)配置项目(Project)的build.gradle
buildscript{ //repositories相当于一个存储jar包的仓库 repositories { mavenCentral() //到网上的Maven仓库的服务器里下载库文件 } //dependencies包含所有真正依赖的库文件 dependencies{ //将GreenDao的插件库文件引入项目 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' } }
(3)配置模组(Module)的build.gradle
在文件的最后添加
//应用GreenDao插件 apply plugin: 'org.greenrobot.greendao' greendao{ //设置greendao的相关参数 schemaVersion 3 //设置数据库版本 daoPackage 'com.example.lucky.mygreendaotest.gen' //生成DAO、DAOMaster、DAOSession的包名(这句代码应根据实际project文件位置进行相应修改) targetGenDir 'src/main/java' //生成DAOs、DAOMaster、DAOSession的目录 } dependencies{ implementation fileTree(dir: 'libs', include: ['*.jar']) //用于项目编译的库文件及所在目录 implementation 'com.android.support:appcompat-v7:28.0.0' //导入greendao的相关库文件 implementation 'org.greenrobot:greendao:3.2.0' implementation 'org.greenrobot:greendao-generator:3.2.0' }
(4) 进行项目同步
点击Sync Now
可见如下图
4.实体类及其注解
(1)介绍
(2)实体类注解及其作用
5.创建实体类并完成注解
(1)新建实体类后,通过build--->make project 编译工程,会自动生成代码(实体类中有一部分、还有DaoMaster.java、DaoSession.java、UserDao.java)
以下是User实体类的代码:
package com.example.lucky.mygreendaotest; import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Index; import org.greenrobot.greendao.annotation.NotNull; import org.greenrobot.greendao.annotation.Property; import org.greenrobot.greendao.annotation.Transient; import org.greenrobot.greendao.annotation.Generated; @Entity( //设置数据库的表名、索引 nameInDb = "users", indexes = { @Index (value = "name DESC") } ) public class User { @Id(autoincrement =true ) private Long id; //设置索引 @Index(name="usercode_index",unique = true) private String usercode; //设置列名 @Property(nameInDb = "name") @NotNull private String name; private String userAddress; //属性userAddress未添加任何注解,可以为空,且在数据库中的列名为User_Address @Transient private int tempUserSign; //以下代码都是自动生成的 @Generated(hash = 264246700) public User(Long id, String usercode, @NotNull String name, String userAddress) { this.id = id; this.usercode = usercode; this.name = name; this.userAddress = userAddress; } @Generated(hash = 586692638) public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsercode() { return this.usercode; } public void setUsercode(String usercode) { this.usercode = usercode; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getUserAddress() { return this.userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } }
(2)新建2个java类(HMROpenHelper、MyGreenDaoApplication)
<1>HMROpenHelper类的源码
package com.example.lucky.mygreendaotest; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.example.lucky.mygreendaotest.gen.DaoMaster; //数据库创建工具类 public class HMROpenHelper extends DaoMaster.OpenHelper { //构造函数,参数2 name为数据库名称 public HMROpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); } }
<2>MyGreenDaoApplication类的源码
package com.example.lucky.mygreendaotest; import android.app.Application; import android.database.sqlite.SQLiteDatabase; import com.example.lucky.mygreendaotest.gen.DaoMaster; import com.example.lucky.mygreendaotest.gen.DaoSession; /*注意:MyGreenDaoApplication类继承Application,需要在AndroidManifest.xml文件中进行相关设置 android:name=".MyGreenDaoApplication" MyGreenDaoApplication类的作用是封装数据库对象*/ public class MyGreenDaoApplication extends Application { private SQLiteDatabase db; //SQLiteDatabase数据库对象 private DaoMaster daoMaster; //daoMaster对象 private DaoSession daoSession; //daoSession对象 public static MyGreenDaoApplication instances; //声明本类的实例对象 @Override public void onCreate() { super.onCreate(); instances=this; //给实例对象赋值 setDatabase(); } private void setDatabase() { //创建数据库 HMROpenHelper hmrOpenHelper=new HMROpenHelper(this,"mydb1",null); db=hmrOpenHelper.getWritableDatabase(); //获取数据库对象 daoMaster=new DaoMaster(db); //获取DaoMaster对象 daoSession=daoMaster.newSession(); } //获取instances 对象 public static MyGreenDaoApplication getInstances(){ return instances; } //获取daoSession对象 public DaoSession getDaoSession(){ return daoSession; } public SQLiteDatabase getDb(){ return db; } }
6.GreenDao3添加记录(SQLite 增)
(1)基本原理
7.GreenDao3删除记录(SQLite 删)
8.GreenDao3修改记录(SQLite 改)
参考文献:https://www.jianshu.com/p/793f77feeb89
https://www.cnblogs.com/tonycheng93/p/6295724.html(推荐)