GreenDao 地址
Demo包下载地址
环境集成比较简单,如下
首先在项目下的build.gradle文件中:
//GreenDao3依赖
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
模块的build.gradle文件中:
apply plugin: 'org.greenrobot.greendao'
//greenDao配置
greendao {
//指定数据库schema版本号,迁移等操作会用到;
schemaVersion 1
//dao的包名,包名默认是entity所在的包;
daoPackage 'com.gz.greendao'
//生成数据库文件的目录;
targetGenDir 'src/main/java'
}
//greendao依赖
implementation 'org.greenrobot:greendao:3.2.2'
定义数据库的实体类
@Entity
public class StudentEntry {
@Id(autoincrement = true) //自增使用Long类型,不能用long
private Long id;
@Property
private String name;
@Property
private String sex;
@Property
private String age;
@Generated(hash = 1678909457) //点击make project自动生成
public StudentEntry(Long id, String name, String sex, String age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
@Generated(hash = 2045720414)//点击make project自动生成
public StudentEntry() {
}
}
点击build下的Make Project后:
会在com.gz.greendao下生成DaoMaster.java、DaoSession.java、以及对应类的Dao文件(StudentEntryDao.java)
1. 首先封装一个DaoManager文件,进行管理数据库
public class DaoManager {
private static final String DB_NAME = "student.db";//数据库名称
private static DaoManager mDaoManager;
private DaoMaster.DevOpenHelper mHelper;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
private Context context;
public DaoManager(Context context) {
this.context = context;
}
/**
* 获得操作数据库的对象
* @return
*/
public static DaoManager getInstance(Context context) {
if (mDaoManager == null) {
synchronized (DaoManager.class) {
if (mDaoManager == null) {
mDaoManager = new DaoManager(context);
}
}
}
return mDaoManager;
}
/**
* 获取DaoSession
*
* @return
*/
public synchronized DaoSession getDaoSession() {
if (null == mDaoSession) {
mDaoSession = getDaoMaster().newSession();
}
return mDaoSession;
}
/**
* 设置debug模式开启或关闭,默认关闭
*
* @param flag
*/
public void setDebug(boolean flag) {
QueryBuilder.LOG_SQL = flag;
QueryBuilder.LOG_VALUES = flag;
}
/**
* 关闭数据库
*/
public synchronized void closeDataBase() {
closeHelper();
closeDaoSession();
}
/**
* 判断数据库是否存在,如果不存在则创建
*
* @return
*/
private DaoMaster getDaoMaster() {
if (null == mDaoMaster) {
mHelper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
mDaoMaster = new DaoMaster(mHelper.getWritableDb());
}
return mDaoMaster;
}
private void closeDaoSession() {
if (null != mDaoSession) {
mDaoSession.clear();
mDaoSession = null;
}
}
private void closeHelper() {
if (mHelper != null) {
mHelper.close();
mHelper = null;
}
}
}
2. 封装常用的数据库操作方法BaseBeanManager
包含基本的增删改查操作
public class BaseBeanManager<T,K> {
private AbstractDao<T,K> mDao;
public BaseBeanManager(AbstractDao<T, K> mDao) {
this.mDao = mDao;
}
public void save(T item){
mDao.save(item);
}
public void save(T... items){
mDao.insertInTx(items);
}
public void save(List<T> items){
mDao.insertInTx(items);
}
public void update(T item) {
mDao.update(item);
}
public void update(T... items) {
mDao.updateInTx(items);
}
public void update(List<T> items) {
mDao.updateInTx(items);
}
public void saveOrUpdate(T t){
mDao.insertOrReplace(t);
}
public void saveOrUpdate(T... items) {
mDao.insertOrReplaceInTx(items);
}
public void saveOrUpdate(List<T> items) {
mDao.insertOrReplaceInTx(items);
}
public T query(K key){
return mDao.load(key);
}
public List<T> queryAll() {
return mDao.loadAll();
}
public List<T> query(String where, String... params) {
return mDao.queryRaw(where, params);
}
public QueryBuilder<T> queryBuilder() {
return mDao.queryBuilder();
}
public long count() {
return mDao.count();
}
public void refresh(T item) {
mDao.refresh(item);
}
public void detach(T item) {
mDao.detach(item);
}
public void deleteByKey(K key) {
mDao.deleteByKey(key);
}
public void delete(T item) {
mDao.delete(item);
}
public void delete(T... items) {
mDao.deleteInTx(items);
}
public void delete(List<T> items) {
mDao.deleteInTx(items);
}
public void deleteAll() {
mDao.deleteAll();
}
}
3. 创建一个数据库实体类,就需要创建一个Manager进行管理
public class StudentManager extends BaseBeanManager<StudentEntry,Long> {
public StudentManager(AbstractDao mDao) {
super(mDao);
}
@Override
public QueryBuilder<StudentEntry> queryBuilder() {
return super.queryBuilder();
}
}
4. 创建ManagerFactory,将所有的管理类放在一起
public class ManagerFactory {
/**
* 每一个BeanManager都管理着数据库中的一个表,我将这些管理者在ManagerFactory中进行统一管理
*/
StudentManager studentManager;
TeacherManager teacherManager;
private static ManagerFactory mInstance = null;
/**
* 获取DaoFactory的实例
*
* @return
*/
public static ManagerFactory getInstance() {
if (mInstance == null) {
synchronized (ManagerFactory.class) {
if (mInstance == null) {
mInstance = new ManagerFactory();
}
}
}
return mInstance;
}
public synchronized StudentManager getStudentManager() {
if (studentManager == null){
studentManager = new StudentManager(DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getStudentEntryDao());
}
return studentManager;
}
public synchronized TeacherManager getTeacherManager(){
if (teacherManager == null){
teacherManager = new TeacherManager(DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getTeacherEntryDao());
}
return teacherManager;
}
}
5. 简单使用:
对于具体的查询,需要在相关的父类进行重写该方法,这里StudentManager中重写了queryBuilder的方法。
//根据名字进行查询
StudentEntry stu = factory.getStudentManager().queryBuilder()
.where(StudentEntryDao.Properties.Name.eq("张三")).build().unique();