GreenDao 简单封装

AbstractDaoSession 提供简单的操作表的接口

     DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "arnold.db");
     DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
     AbstractDaoSession  daoSession = daoMaster.newSession();

简单封装数据查询接口 



    /**
     * 原生sql文查询
     *
     * @param sql
     * @param selectionArgs
     *
     * @return
     */
    public Cursor rawQuery(final String sql, final String[] selectionArgs) {
        return daoSession.getDatabase().rawQuery(sql, selectionArgs);
    }

    /**
     * 主键查询实体
     *
     * @param entityClass
     * @param key
     * @param 
     * @param 
     *
     * @return
     */
    public  T query(Class entityClass, final K key) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        return dao.load(key);
    }

    /**
     * 条件查询实体集合
     *
     * @param entityClass
     * @param where
     * @param selectionArgs
     * @param 
     *
     * @return
     */
    public  List query(Class entityClass, String where, Object... selectionArgs) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        QueryBuilder builder = dao.queryBuilder().where(
                new WhereCondition.StringCondition(where, selectionArgs));
        return builder.list();
    }

    /**
     * 全量查询实体集合
     *
     * @param entityClass
     * @param 
     *
     * @return
     */
    public  List queryAll(Class entityClass) {
        return daoSession.loadAll(entityClass);
    }

    /**
     * 分页查询实体集合
     *
     * @param entityClass
     * @param offset
     * @param limit
     * @param 
     *
     * @return
     */
    public  List queryPage(Class entityClass, int offset, int limit) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        QueryBuilder builder = dao.queryBuilder().offset(offset * limit).limit(limit);
        return builder.list();
    }

    /**
     * 分页条件查询实体集合
     *
     * @param entityClass
     * @param offset
     * @param limit
     * @param where
     * @param selectionArgs
     * @param 
     *
     * @return
     */
    public  List queryPage(Class entityClass, int offset, int limit, String where,
                                 Object... selectionArgs) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        QueryBuilder builder = dao.queryBuilder()
                .where(
                        new WhereCondition.StringCondition(where, selectionArgs))
                .offset(offset * limit)
                .limit(limit);
        return builder.list();
    }

    /**
     * 实体数量查询
     *
     * @param entityClass
     * @param 
     *
     * @return
     */
    public  long queryCount(Class entityClass) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        return dao.count();
    }

    /**
     * 获取 QueryBuilder 对象
     *
     * @param entityClass
     * @param 
     *
     * @return
     */
    public  QueryBuilder queryBuilder(Class entityClass) {
        return daoSession.queryBuilder(entityClass);
    }

    /**
     * 条件删除实体
     *
     * @param entityClass
     * @param where
     * @param selectionArgs
     * @param 
     */
    public  void queryDelete(Class entityClass, String where, Object... selectionArgs) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        QueryBuilder builder = dao.queryBuilder().where(
                new WhereCondition.StringCondition(where, selectionArgs));
        builder.buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 新增实体
     *
     * @param entity
     * @param 
     *
     * @return
     */
    public  long insert(final T entity) {
        return daoSession.insert(entity);
    }

    /**
     * 更新实体
     *
     * @param entity
     * @param 
     */
    public  void update(final T entity) {
        daoSession.update(entity);
    }

    /**
     * 删除实体
     *
     * @param entity
     * @param 
     */
    public  void delete(final T entity) {
        daoSession.delete(entity);
    }

    /**
     * 新增实体集合
     *
     * @param list
     * @param 
     *
     * @throws Exception
     */
    public  void insertInTx(final List list) throws Exception {
        daoSession.callInTx(new Callable() {
            @Override
            public Object call() throws Exception {
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    T entity = iterator.next();
                    daoSession.insert(entity);
                }
                return null;
            }
        });
    }

    /**
     * 更新实体集合
     *
     * @param list
     * @param 
     *
     * @throws Exception
     */
    public  void updateInTx(final List list) throws Exception {
        daoSession.callInTx(new Callable() {
            @Override
            public Object call() throws Exception {
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    T entity = iterator.next();
                    daoSession.update(entity);
                }
                return null;
            }
        });
    }

    /**
     * 删除实体集合
     *
     * @param list
     * @param 
     *
     * @throws Exception
     */
    public  void deleteInTx(final List list) throws Exception {
        daoSession.callInTx(new Callable() {
            @Override
            public Object call() throws Exception {
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    T entity = iterator.next();
                    daoSession.delete(entity);
                }
                return null;
            }
        });
    }

    /**
     * 删除所有实体集合
     *
     * @param entityClass
     * @param 
     */
    public  void deleteAll(Class entityClass) {
        daoSession.deleteAll(entityClass);
    }

    /**
     * 异步事物处理
     *
     * @param runnable
     */
    public void runInTx(Runnable runnable) {
        daoSession.runInTx(runnable);
    }

    /**
     * 同步事物处理
     *
     * @param callable
     * @param 
     *
     * @return
     *
     * @throws Exception
     */
    public  V callInTx(Callable callable) throws Exception {
        V result = daoSession.callInTx(callable);
        return result;
    }

    /**
     * 分页条件查询实体集合
     *
     * @param entityClass
     * @param offset
     * @param limit
     * @param where
     * @param selectionArgs
     * @param 
     *
     * @return
     */
    public  List queryPage(Class entityClass, int offset, int limit, String where,
                                 Property properties, Object... selectionArgs) {
        AbstractDao dao = (AbstractDao) daoSession.getDao(entityClass);
        QueryBuilder builder = dao.queryBuilder()
                .orderDesc(properties)
                .where(
                        new WhereCondition.StringCondition(where, selectionArgs))
                .offset(offset * limit)
                .limit(limit);
        return builder.list();
    } 
  

 

你可能感兴趣的:(数据库)