概述
GreenDao是一个对象关系映射(ORM)的框架,能够提供一个接口通过操作对象的方式去操作关系型数据库,如下:
关于greenDao的优势,官网如下:
- Maximum performance (probably the fastest ORM for Android); our benchmarks are open sourced too
- Easy to use powerful APIs covering relations and joins
- Minimal memory consumption
- Small library size (<100KB) to keep your build times low and to avoid the 65k method limit
- Database encryption: greenDAO supports SQLCipher to keep your user’s data safe
- Strong community: More than 5.000 GitHub stars show there is a strong and active community
如何添加greenDao到AS?
- 在project的 build.gradle中引入greenDao插件;
- 在module的build.gradle中添加greenDao插件、引入类库、设置greenDao等等。
如何使用greenDao?
(1) 定义实体:如下是定义的一个User实体
/**
* 实体类中使用greendao的注解可以把对应的实体类生成同名的表,同时生成相关操作类和getter setter方法(因此定义类时不需要写)
* Created by hzyanglili1 on 2016/11/24.
*/
@Entity
public class User {
//@Id主键id 可设置是否自增(注意这里的主键id一定是Long类型)
@Id(autoincrement = true)
private Long id;
//@Property中可设置数据库中列的名称 否则为默认
//@Index设置为索引列(当有大量查询操作时设置索引列加速查询,但同时也会有额外的空间消耗)
@Index(unique = true)
@Property(nameInDb = "USERNAME")
private String name;
//设置此属性非空
@NotNull
private int age;
//@Transient 表示数据库中不生成此列
@Transient
private String info;
}
greenDao常见注解:
- @Entity 把java中的实体类转化为数据库中的表
- @Id 设置对应java类的属性为数据库的主键ID,必须为Long类型
- @Property 可以定义属性在数据库中的column名称
- @NotNull 设置属性非空
- @Transient 用来防止属性自动生成数据库的列
(2)编译实体类,生成相关的dao文件。
如下图所示,根据daoPackage ‘com.hzyanglili1.greendaodemo.gen’的设置,下面的gen文件夹下的全是自动生成的文件,我们主要使用这三个类进行增删改查操作。
(3) 使用数据库
定义如下的数据库管理类,封装了CURD操作。
package com.hzyanglili1.greendaodemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.hzyanglili1.greendaodemo.entity.User;
import com.hzyanglili1.greendaodemo.gen.DaoMaster;
import com.hzyanglili1.greendaodemo.gen.DaoSession;
import com.hzyanglili1.greendaodemo.gen.UserDao;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
/**
* Created by hzyanglili1 on 2016/11/24.
*/
public class DBManager {
private Context context;
private DaoMaster.DevOpenHelper openHelper;
private static DBManager instance;
private final static String dbName = "test_db";
private DBManager(Context context) {
this.context = context;
openHelper = new DaoMaster.DevOpenHelper(context,dbName);
}
/**
* 获取单例引用
* @param context
* @return
*/
public static DBManager getInstance(Context context){
if (instance == null){
synchronized (DBManager.class){
if (instance == null){
instance = new DBManager(context);
}
}
}
return instance;
}
/**
* 获取可读数据库
* @return
*/
private SQLiteDatabase getReadableDatabase(){
if (openHelper == null){
openHelper = new DaoMaster.DevOpenHelper(context,dbName);
}
SQLiteDatabase db = openHelper.getReadableDatabase();
return db;
}
/**
* 获取可写数据库
* @return
*/
private SQLiteDatabase getWritableDatabase(){
if (openHelper == null){
openHelper = new DaoMaster.DevOpenHelper(context,dbName);
}
SQLiteDatabase db = openHelper.getWritableDatabase();
return db;
}
/**
* 插入一条记录
* @param user
*/
public void insertUser(User user){
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
UserDao userDao = daoMaster.newSession().getUserDao();
userDao.insert(user);
}
/**
* 插入用户集合
* @param users
*/
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);
}
/**
* 删除一条记录
* @param user
*/
public void deleteUser(User user){
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
UserDao userDao = daoMaster.newSession().getUserDao();
userDao.delete(user);
}
/**
* 更新一条数据
* @param user
*/
public void updateUser(User user){
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
daoMaster.newSession().getUserDao().update(user);
}
/**
* 查询用户列表
* @return
*/
public List<User> queryUserlist(){
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
UserDao userDao = daoMaster.newSession().getUserDao();
QueryBuilder<User> queryBuilder = userDao.queryBuilder();
return queryBuilder.list();
}
public List<User> queryUserList(int age){
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
UserDao userDao = daoMaster.newSession().getUserDao();
QueryBuilder<User> queryBuilder = userDao.queryBuilder();
queryBuilder.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age);
List<User> list = queryBuilder.list();
return list;
}
public User queryUserById(long id){
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
UserDao userDao = daoMaster.newSession().getUserDao();
QueryBuilder<User> queryBuilder = userDao.queryBuilder();
queryBuilder.where(UserDao.Properties.Id.eq(id));
return queryBuilder.unique();
}
}