greenDAO封装

EasygreenDAO

基于greenDAO的上层调用封装。

greenDAO的集成

greenDAO的集成和说明参考https://github.com/greenrobot/greenDAO。

封装

greenDAO默认生成DaoMaster、DaoSession和各个数据表Dao操作对象,各个数据表的数据操作接口都在Dao对象中。

封装简化了数据表的数据操作,无需关注具体的数据表Dao操作对象,实现外层接口的统一调用。

例如:

  1. 有User对象和数据表、Card对象和数据表,生成UserDao和CardDao两个数据库操作对象。
    User对象插入操作:
    getDaoSession().getUserDao().insert(user);
    Card对象插入操作:
    getDaoSession().getCardDao().insert(card);

  2. 新建统一数据表操作接口类 ,封装统一的插入接口:
    void insert(Object object);
    不管是User对象插入还是Card对象插入都统一调用接口:
    insert(user)或者insert(card),无需关注是UserDao和是CardDao操作对象。

具体实现如下:

  • 首先定义数据库操作接口
package vivek.wo.easygreendao;

import java.util.List;

/**
 * Created by VIVEK-WO on 2018/3/29.
 */

public interface IDataSourceOperation {

    void insert(Object object);

    void insertOrUpdate(Object object);

    void update(Object object);

    void delete(Object object);

    void deleteByKey(Class classType, String id);

    void deleteAll(Class classType);

     T quaryByKey(Class classType, String id);

     List quary(Class classType, String where, String... selectionArg);

     List quaryAll(Class classType);
}
  • 定义数据库并实现数据库操作接口
    继承DaoMaster.OpenHelper类,定义数据库名称。暂无修改数据库路径和升级方法。
package vivek.wo.easygreendao;

import android.content.Context;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.identityscope.IdentityScopeType;

import java.util.List;

import vivek.wo.easygreendao.entity.DaoMaster;
import vivek.wo.easygreendao.entity.DaoSession;

/**
 * Created by VIVEK-WO on 2018/3/29.
 */

public class TaskLocalDataSource implements IDataSourceOperation {
    private static final String DATABASE_NAME = "local.db";//数据库名称
    DaoSession mDaoSession;

    public TaskLocalDataSource(Context context) {
        DaoMaster daoMaster = new DaoMaster(new DatabaseOpenHelper(context).getWritableDb());
        mDaoSession = daoMaster.newSession(IdentityScopeType.None);
    }

    public AbstractDao getDao(Class entityClass) {
        return mDaoSession.getDao(entityClass);
    }

    @Override
    public void insert(Object object) {
        Class cls;
        if (object instanceof List) {
            List listObject = (List) object;
            if (listObject.isEmpty()) {
                throw new IllegalArgumentException("List Object Not Allow Empty!");
            }
            cls = listObject.get(0).getClass();
            getDao(cls).insertInTx(listObject);
        } else {
            cls = object.getClass();
            getDao(cls).insert(object);
        }
    }

    @Override
    public void insertOrUpdate(Object object) {
        Class cls;
        if (object instanceof List) {
            List listObject = (List) object;
            if (listObject.isEmpty()) {
                throw new IllegalArgumentException("List Object Not Allow Empty!");
            }
            cls = listObject.get(0).getClass();
            getDao(cls).insertOrReplaceInTx(listObject);
        } else {
            cls = object.getClass();
            getDao(cls).insertOrReplace(object);
        }
    }

    @Override
    public void update(Object object) {
        Class cls;
        if (object instanceof List) {
            List listObject = (List) object;
            if (listObject.isEmpty()) {
                throw new IllegalArgumentException("List Object Not Allow Empty!");
            }
            cls = listObject.get(0).getClass();
            getDao(cls).updateInTx(listObject);
        } else {
            cls = object.getClass();
            getDao(cls).update(object);
        }
    }

    @Override
    public void delete(Object object) {
        Class cls;
        if (object instanceof List) {
            List listObject = (List) object;
            if (listObject.isEmpty()) {
                throw new IllegalArgumentException("List Object Not Allow Empty!");
            }
            cls = listObject.get(0).getClass();
            getDao(cls).deleteInTx(listObject);
        } else {
            cls = object.getClass();
            getDao(cls).delete(object);
        }
    }

    @Override
    public void deleteByKey(Class cls, String key) {
        ((AbstractDao) getDao(cls)).deleteByKey(key);
    }

    @Override
    public void deleteAll(Class cls) {
        getDao(cls).deleteAll();
    }

    @Override
    public  T quaryByKey(Class cls, String key) {
        return ((AbstractDao) getDao(cls)).load(key);
    }

    @Override
    public  List quary(Class cls, String where, String... selectionArg) {
        return ((AbstractDao) getDao(cls)).queryRaw(where, selectionArg);
    }

    @Override
    public  List quaryAll(Class cls) {
        return ((AbstractDao) getDao(cls)).loadAll();
    }

    static class DatabaseOpenHelper extends DaoMaster.OpenHelper {

        public DatabaseOpenHelper(Context context) {
            super(context, DATABASE_NAME);
        }
    }
}

个人觉得这样封装的好处在与外层直接通过接口调用,无需关注数据库的实现和操作,也无需关注使用的是哪家的数据库方案。

具体项目代码GitHub:https://github.com/vivek-wo/EasygreenDAO

你可能感兴趣的:(greenDAO封装)