GreenDao集成教程(二) DB基础操作

自定义包装DBManager工具类

package com.cheung.android.fullstackandroid.manager;

import android.content.Context;

import com.cheung.android.fullstackandroid.entity.DaoMaster;
import com.cheung.android.fullstackandroid.entity.DaoSession;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * author: C_CHEUNG
 * created on: 2017/11/27
 * description: 封装DB操作类
 */
public class DBManager {
    private final String TAG = this.getClass().getSimpleName();
    private final String DB_NAME = "db.sqlite";
    public volatile static DBManager manager;
    private static DaoMaster.DevOpenHelper helper;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private Context context;


    private DBManager() {
    }

    public void init(Context context){
        this.context = context;
    }
    /**
     * 使用单例获取操作数据库对象
     *
     * @return
     */
    public static DBManager getInstance() {
        DBManager instance = null;
        if (manager == null) {
            synchronized (DBManager.class) {
                if (instance == null) {
                    instance = new DBManager();
                    manager = instance;
                }
            }
        }
        return instance;
    }

    /**
     * 判断是否存在数据库,若不存在则创建
     *
     * @return
     */
    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
            daoMaster = new DaoMaster(helper.getWritableDb());
        }
        return daoMaster;
    }

    /**
     * 完成对数据库的增删改查操作接口定义
     *
     * @return
     */
    public DaoSession getDaoSession() {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    /**
     * 关闭所有的数据库操作,关闭链接
     */
    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    private void closeHelper() {
        if (helper != null) {
            helper.close();
            helper = null;
        }
    }

    private void closeDaoSession() {
        if (daoSession != null) {
            daoSession.clear();
            daoSession = null;
        }
    }

    /**
     * 打开输出日志,默认关闭
     */
    public void setDeBug() {
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }
}

封装CommonDaoUtils增删改查操作实现类

package com.cheung.android.fullstackandroid.manager;

import android.content.Context;
import android.database.sqlite.SQLiteException;

import com.cheung.android.fullstackandroid.entity.Member;

import java.util.List;

/**
 * author: C_CHEUNG
 * created on: 2017/11/27
 * description: 增删改查操作实现类
 */
public class CommonDaoUtils {
    private final String TAG = this.getClass().getSimpleName();

    private DBManager manager;

    public CommonDaoUtils(Context context) {
        manager = DBManager.getInstance();
        manager.init(context);
    }

    /**
     * 完成对象Member的Insert操作
     *
     * @param member
     * @return
     */
    public boolean insertMember(Member member) throws SQLiteException {
        boolean flag = false;
        long secCode = manager.getDaoSession().insert(member);
        if (secCode > 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * 批量插入Member对象
     *
     * @param members
     * @return
     * @throws SQLiteException
     */
    public boolean insertMemberAry(final List<Member> members) throws SQLiteException {
        boolean flag = false;
        try {
            manager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Member member : members) {
                        manager.getDaoSession().insertOrReplace(member);
                    }
                }
            });
            flag = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改对象Member
     * @param member
     * @return
     * @throws SQLiteException
     */
    public boolean updateMember(Member member){
        boolean flag = false;
        try {
            manager.getDaoSession().update(member);
            flag = true;
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return flag;
    }

    /**
     * 删除指定Member
     * @param member
     * @return
     */
    public boolean deleteMember(Member member){
        boolean flag = false;
        try {
            manager.getDaoSession().delete(member);
            flag = true;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return flag;
    }

}

DEMO入口

    原文作者:冷寒
    原文地址: https://www.jianshu.com/p/e7855ac400b6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞