基本DAO

/**
 * 数据库映射接口,用于模型对象的转换
 * 
 */
public interface BaseTransition<T>
{

    /**
     * 将模型对象转换成ContentValues
     * 
     * @param t 模型对象
     * @return ContentValues
     */
    public ContentValues fromModelToContentValues(T t);

    /**
     * 将Cursor转换成模型对象
     * 
     * @param cursor 查询游标
     * @return 模型对象
     */
    public List<T> fromCursorToModel(Cursor cursor);

    /**
     * 从模型对象中取出ID
     * @param t 模型对象
     * @return ID
     */
    public int getIdFromModel(T t);

}

/**
 * 数据库访问层通用接口,该接口规定了数据库访问层的参数风格,屏蔽了实现细节
 * 
 */
public interface IBaseDAO<T>
{

    /**
     * 插入新记录
     * 
     * @param t 要插入的数据实体
     */
    long insert(T t);

    /**
     * 根据ID查询一条记录
     * 
     * @param id 主键
     * @return 查询结果
     */
    T query(int id);

    /**
     * 更新一条记录
     * 
     * @param t 要更新的数据实体
     */
    int update(T t);

    /**
     * 删除一条记录
     * 
     * @param t 要删除的数据实体
     */
    int delete(T t);

    /**
     * 查询所有记录
     * 
     * @return 记录列表
     */
    List<T> queryAll();

    

}

/**
 * 数据库访问层基础实现
 */
public abstract class BaseDAO<T> implements IBaseDAO<T>
{

    private static final String WHERE_CLAUSE_ID = "_ID = ?";
    private String tableName;
    private String[] columns;// 列名,在实现类中指定
    private BaseTransition<T> dbAdapter;// 模型映射组件,在实现类中实例化
    protected SQLiteDatabase db;// 数据库访问组件,类似于HibernateTemplate

    /**
     * 构造方法,在具体的实现类中调用
     * 
     * @param db SQLiteDatabase组件
     * @param tableName 表名
     * @param cols 列名
     * @param mapper 模型映射组件
     */
    public BaseDAO(SQLiteDatabase db, String tableName, String[] cols, BaseTransition<T> mapper)
    {
        this.db = db;
        this.tableName = tableName;
        this.columns = cols;
        this.dbAdapter = mapper;
    }

    @Override
    public long insert(T t)
    {
        long createResult = db.insert(tableName, null, dbAdapter.fromModelToContentValues(t));
        if (createResult == -1)
        {
            throw new RuntimeException();
        }
        return createResult;
    }

    @Override
    public T query(int id)
    {
        return queryForObject(WHERE_CLAUSE_ID, new String[]{String.valueOf(id)});
    }

    @Override
    public int update(T t)
    {
        String[] whereArgs = new String[]{String.valueOf(dbAdapter.getIdFromModel(t))};
        return db.update(tableName, dbAdapter.fromModelToContentValues(t), WHERE_CLAUSE_ID, whereArgs);
    }

    @Override
    public int delete(T t)
    {
        String[] whereArgs = new String[]{String.valueOf(dbAdapter.getIdFromModel(t))};
        return db.delete(tableName, WHERE_CLAUSE_ID, whereArgs);
    }

    @Override
    public List<T> queryAll()
    {
        return queryForList(null, null);
    }

    /**
     * 查询返回唯一结果,该方法声明为protected,给具体实现类提供便利
     * 
     * @param whereClause where限定子句
     * @param whereValues where限定子句的条件
     * @return 查询结果,如果没有找到记录,则返回null
     */
    protected T queryForObject(String whereClause, String[] whereValues)
    {
        List<T> result = doQueryWithWhereClause(whereClause, whereValues);
        if (!result.isEmpty())
        {
            return result.get(0);// 查询结果不为空,则返回第一个结果
        }
        else
        {
            return null;// 查询结果为空,返回null
        }
    }

    /**
     * 查询返回结果列表,该方法声明为protected,给具体实现类提供便利
     * 
     * @param whereClause where限定子句
     * @param whereValues where限定子句的条件
     * @return 查询结果,如果没有查询到记录,则返回空List
     */
    protected List<T> queryForList(String whereClause, String[] whereValues)
    {
        return doQueryWithWhereClause(whereClause, whereValues);
    }

    /**
     * 从数据库查询,支持where语句
     * 
     * @param whereClause where限定子句
     * @param whereValues where限定子句的条件
     * @return 查询结果
     */
    private List<T> doQueryWithWhereClause(String whereClause, String[] whereValues)
    {
        Cursor cursor = db.query(tableName, columns, whereClause, whereValues, null, null, null);
        List<T> result = dbAdapter.fromCursorToModel(cursor);
        cursor.close();// 游标用完要关闭
        return result;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值