Android Studio Giraffe | 2022.3.1 使用GreenDao

一、环境:Adroid Studio 2022.3.1 + java8+ gradle 8.0

二、配置: 

1、修改项目模块的gradle配置文件build.gradle

buildscript {//添加这个buildscript代码块
    dependencies {
        classpath("org.greenrobot:greendao-gradle-plugin:3.3.1")
    }
}
plugins {//这是原本就有的代码块
    id 'com.android.application' version '8.1.0' apply false
}

2、修改app模块的gradle配置文件build.gradle

id 'org.greenrobot.greendao'

implementation 'org.greenrobot:greendao:3.3.0'

在com.xxx.xxx.db.greendao包名目录下创建自己的Bean文件

点击这个小三角会生成对应的dao数据库文件

三、使用

创建DaoManager类操作数据库
public class DaoManager {
    private static final String TAG = DaoManager.class.getSimpleName();
    private static final String DB_NAME = "xxx_db";

    private Context context;

    //多线程中要被共享的使用volatile关键字修饰
    private volatile static DaoManager manager = new DaoManager();
    private static DaoMaster sDaoMaster;
    private static DaoMaster.DevOpenHelper sHelper;
    private static DaoSession sDaoSession;

    /**
     * 单例模式获得操作数据库对象
     *
     * @return
     */
    public static DaoManager getInstance() {
        return manager;
    }

    private DaoManager() {
        setDebug();
    }

    public void init(Context context) {
        this.context = context;
    }

    /**
     * 判断是否有存在数据库,如果没有则创建
     *
     * @return
     */
    public DaoMaster getDaoMaster() {
        if (sDaoMaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
            sDaoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return sDaoMaster;
    }

    /**
     * 完成对数据库的添加、删除、修改、查询操作,仅仅是一个接口
     *
     * @return
     */
    public DaoSession getDaoSession() {
        if (sDaoSession == null) {
            if (sDaoMaster == null) {
                sDaoMaster = getDaoMaster();
            }
            sDaoSession = sDaoMaster.newSession();
        }
        return sDaoSession;
    }

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

    /**
     * 关闭所有的操作,数据库开启后,使用完毕要关闭
     */
    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    public void closeHelper() {
        if (sHelper != null) {
            sHelper.close();
            sHelper = null;
        }
    }

    public void closeDaoSession() {
        if (sDaoSession != null) {
            sDaoSession.clear();
            sDaoSession = null;
        }
    }
}

创建DbOperationUtils类,实现增删改查等操作

public class DbOperationUtils {
    private static final String TAG = DbOperationUtils.class.getSimpleName();
    private DaoManager mManager;

    public DbOperationUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成IHistoryEntity记录的插入,如果表未创建,先创建IHistoryEntity表
     *
     * @param ident
     * @return
     */
    public boolean insertIdent(HistoryEntity ident) {
        boolean flag = false;
        flag = mManager.getDaoSession().getHistoryEntityDao().insert(ident) == -1 ? false : true;
        Log.i(TAG, "insert ident :" + flag + "-->" + ident.toString());
        return flag;
    }

    /**
     * 插入多条数据,在子线程操作,实现去重功能
     *
     * @param identList
     * @return
     */
    public boolean insertMultIdent(final List<HistoryEntity> identList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    List<HistoryEntity> allList = queryAllIdent();
                    for (HistoryEntity ident : identList) {
                        boolean isExit = false;
                        for (HistoryEntity identOld : allList){
                            if(ident.equals(identOld)){
                                isExit = true;
                            }
                        }
                        if(!isExit){
                            mManager.getDaoSession().insert(ident);
                        }
                        //mManager.getDaoSession().getHistoryEntityDao().save(ident);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param ident
     * @return
     */
    public boolean updateIdent(HistoryEntity ident) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(ident);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param ident
     * @return
     */
    public boolean deleteIdent(HistoryEntity ident) {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(ident);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(HistoryEntity.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     *
     * @return 倒序
     */
    public List<HistoryEntity> queryAllIdent() {
       // return mManager.getDaoSession().loadAll(HistoryEntity.class);
        return mManager.getDaoSession().queryBuilder(HistoryEntity.class).orderDesc(HistoryEntityDao.Properties.Date).list();
    }

    /**
     * 根据主键id查询记录
     *
     * @param key
     * @return
     */
    public HistoryEntity queryIdentById(long key) {
        return mManager.getDaoSession().load(HistoryEntity.class, key);
    }

    /**
     * 查询运行模式记录
     *
     * @param mode
     * @return 倒序
     */
    public List<HistoryEntity> queryIdentByRunMode(String mode) {
        return mManager.getDaoSession().queryBuilder(HistoryEntity.class)
                .where(HistoryEntityDao.Properties.Status.eq(mode)).orderDesc(HistoryEntityDao.Properties.Date).list();
    }


    /**
     * 使用native sql进行查询操作
     */
    public List<HistoryEntity> queryIdentByNativeSql(String sql, String[] conditions) {
        return mManager.getDaoSession().queryRaw(HistoryEntity.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     *
     * @return
     */
   /* public List<HistoryEntity> queryIdentByQueryBuilder(long id) {
        QueryBuilder<HistoryEntity> queryBuilder = mManager.getDaoSession().queryBuilder(HistoryEntity.class);
        return queryBuilder.where(HistoryEntityDao.Properties.Id.eq(id)).list();
    }*/

}

四、Tip

1、查询之后可以通过orderDesc排序 ,排序按时间

2、去重就是遍历数据库,做对比,数据不多时可以这么干

3、没有设置自增id,用不到,总有一天会超过最大值,不确定超了会怎样,以后再了解

4、超过100条数据删除最开始插入的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值