GreenDao 优点:
性能高,号称Android最快的关系型数据库
内存占用小
库文件比较小,小于100K,编译时间低,而且可以避免65K方法限制
支持数据库加密 greendao支持SQLCipher进行数据库加密
Sqlite采用SQLCipher数据库加密实战
简洁易用的API
GreenDao 3.0使用方式 添加配置 在project build.gradle 加入
1 2 3 4 5 6 7 8 9 10 11 12 13 buildscript { repositories { mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.0.1" classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' } }
在module build.gradle中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 apply plugin: 'org.greenrobot.greendao' plugins { id 'com.android.application' id 'org.greenrobot.greendao' } greendao { schemaVersion 1 targetGenDir 'src/main/java' daoPackage 'vip.izumi.androidframework.dao.db' } implementation 'org.greenrobot:greendao:3.3.0'
Gradle 插件配置 比如上面想指定生成DaoMaster 、DaoSession、Dao位置
1 2 3 greendao { targetGenDir 'src/main/java' }
schemaVersion: 数据库schema版本,也可以理解为数据库版本号
daoPackage:设置DaoMaster 、DaoSession、Dao包名
targetGenDir:设置DaoMaster 、DaoSession、Dao目录
targetGenDirTest:设置生成单元测试目录
generateTests:设置自动生成单元测试用例
新建实体 1 2 3 4 5 6 7 8 9 @Entity public class User { @Id private Long id; private String name; private int age; }
实体@Entity注解
schema:告知GreenDao当前实体属于哪个schema
active:标记一个实体处于活动状态,活动实体有更新、删除和刷新方法
nameInDb:在数据中使用的别名,默认使用的是实体的类名
indexes:定义索引,可以跨越多个列
createInDb:标记创建数据库表
基础属性注解
@Id :主键 Long型,可以通过@Id(autoincrement = true)设置自增长
@Property:设置一个非默认关系映射所对应的列名,默认是的使用字段名 举例:@Property (nameInDb=”name”)
@NotNul:设置数据库表当前列不能为空
@Transient :添加次标记之后不会生成数据库表的列
索引注解
@Index:使用@Index作为一个属性来创建一个索引,通过name设置索引别名,也可以通过unique给索引添加约束
@Unique:向数据库列添加了一个唯一的约束
关系注解
@ToOne:定义与另一个实体(一个实体对象)的关系
@ToMany:定义与多个实体对象的关系
GreenDao 3.0简单实战: 通过上面使用方式我们可以获取DaoMaster 、DaoSession、Dao类 这里声明一个数据库管理者单例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class DBManager { private final static String dbName = "test_db" ; private static DBManager mInstance; private DaoMaster.DevOpenHelper openHelper; private Context context; public DBManager (Context context) { this .context = context; openHelper = new DaoMaster .DevOpenHelper(context, dbName, null ); } public static DBManager getInstance (Context context) { if (mInstance == null ) { synchronized (DBManager.class) { if (mInstance == null ) { mInstance = new DBManager (context); } } } return mInstance; } }
获取可读可写数据库 可读数据库
1 2 3 4 5 6 7 8 9 10 private SQLiteDatabase getReadableDatabase () { if (openHelper == null ) { openHelper = new DaoMaster .DevOpenHelper(context, dbName, null ); } SQLiteDatabase db = openHelper.getReadableDatabase(); return db; }
可写数据库
1 2 3 4 5 6 7 8 9 10 private SQLiteDatabase getWritableDatabase () { if (openHelper == null ) { openHelper = new DaoMaster .DevOpenHelper(context, dbName, null ); } SQLiteDatabase db = openHelper.getWritableDatabase(); return db; }
插入数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public void insertUser (User user) { DaoMaster daoMaster = new DaoMaster (getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.insert(user); } public void insertUserList (List<User> users) { if (users == null || users.isEmpty()) { return ; } DaoMaster daoMaster = new DaoMaster (getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.insertInTx(users); }
删除数据 1 2 3 4 5 6 7 8 9 10 11 public void deleteUser (User user) { DaoMaster daoMaster = new DaoMaster (getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.delete(user); }
更新数据 1 2 3 4 5 6 7 8 9 10 11 public void updateUser (User user) { DaoMaster daoMaster = new DaoMaster (getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.update(user); }
查询数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public List<User> queryUserList () { DaoMaster daoMaster = new DaoMaster (getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder<User> qb = userDao.queryBuilder(); List<User> list = qb.list(); return list; } public List<User> queryUserList (int age) { DaoMaster daoMaster = new DaoMaster (getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder<User> qb = userDao.queryBuilder(); qb.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age); List<User> list = qb.list(); return list; }
测试程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 DBManager dbManager = DBManager.getInstance(this ); for (int i = 0 ; i < 5 ; i++) { User user = new User (); user.setId(i); user.setAge(i * 3 ); user.setName("第" + i + "人" ); dbManager.insertUser(user); } List<User> userList = dbManager.queryUserList(); for (User user : userList) { Log.e("TAG" , "queryUserList--before-->" + user.getId() + "--" + user.getName() +"--" +user.getAge()); if (user.getId() == 0 ) { dbManager.deleteUser(user); } if (user.getId() == 3 ) { user.setAge(10 ); dbManager.updateUser(user); } } userList = dbManager.queryUserList(); for (User user : userList) { Log.e("TAG" , "queryUserList--after--->" + user.getId() + "---" + user.getName()+"--" +user.getAge()); }