泛型DAO模板

一、BaseDao模板

public interface BaseDao<T> {
    
    public Serializable save(T entity); 

    public void delete(T entity);

    public void update(T entity);
    
    public List<T> find(T condition);
    
    public T findById(Serializable id);

    // 参数1:hqlString = from Users u where u.name like ? and u.telephone=?
    // 参数2:new Object[]{name,telephone,...}
    public List<T> getListByHQL(String hqlString, Object... values);

    public List<T> queryListObjectAllForPage(int pageSize, int page,
            String hqlString, Object... values);

    //单值查询   select count()
    public Object uniqueResult(String sqlString, Object... values);
    
    public Object uniqueResultForPages(String hqlString,int pageSize,int page, Object... values);
}

二、BaseDaoImpl模板

@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {
    
    //@Resource   //官方的注解
    @Autowired
    private SessionFactory sessionFactory;
    
    protected Class<T> entityClass;

    public Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    protected Class getEntityClass() {
        if (entityClass == null) {
            entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
                    .getActualTypeArguments()[0];
        }
        return entityClass;
    }

    public Serializable save(T entity) {
        return this.getCurrentSession().save(entity);

    }

    public void delete(T entity) {
        this.getCurrentSession().delete(entity);
    }

    public void update(T entity) {
        this.getCurrentSession().update(entity);
    }   

    public List<T> find(T condition) {
        Criteria criteria = this.getCurrentSession().createCriteria(getEntityClass());
        criteria.add(Example.create(condition));
        return criteria.list();
    }

    public T findById(Serializable id) {
        T load = (T) this.getCurrentSession().get(getEntityClass(), id);
        return load;
    }
    

    /**
     * <根据HQL语句,得到对应的list>
     * 
     * @param hqlString
     *            HQL语句
     * @param values
     *            不定参数的Object数组
     * @return 查询多个实体的List集合
     * @see com.itv.launcher.util.IBaseDao#getListByHQL(java.lang.String,
     *      java.lang.Object[])
     */
    public List<T> getListByHQL(String hqlString, Object... values) {
        Query query = this.getCurrentSession().createQuery(hqlString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);  //设置占位符的参数
            }
        }
        return query.list();
    }

    public List<T> queryListObjectAllForPage(int pageSize, int page, String hqlString, Object... values) {

        Query query = this.getCurrentSession().createQuery(hqlString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
        }
        query.setFirstResult((page - 1) * pageSize);
        query.setMaxResults(pageSize);
        return query.list();
    }

    public Object uniqueResult(String hqlString, Object... values) {
        Query query = this.getCurrentSession().createQuery(hqlString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
        }
        return query.uniqueResult();
    }
    
    public Object uniqueResultForPages(String hqlString, int pageSize,
            int page, Object... values) {
        Query query = this.getCurrentSession().createQuery(hqlString);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i, values[i]);
            }
        }
        query.setFirstResult((page - 1) * pageSize);
        query.setMaxResults(pageSize);
        
        return query.uniqueResult();
    }
}

三、继承

public interface UserDao extends BaseDao<User> {}

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 泛型 体验泛型 没有使用泛型时,只要是对象,不管是什么类型的对象,都可以存储进同一个集合中。使用泛型集合,可以将一...
    bo客先生阅读 748评论 0 8
  • 拿来可用的模板方法   有开发过企业级java应用的肯定不会对这个模式陌生,也许正在进行着的CURD都包含着模板方...
    失心轩阅读 163评论 0 0
  • 拿来可用的模板方法   有开发过企业级java应用的肯定不会对这个模式陌生,也许正在进行着的CURD都包含着模板方...
    失心轩阅读 107评论 0 1
  • 一、概述 二、模板定义与使用  1. 函数模板的定义与使用 2. 类模板的定义与使用 三、typename 的特殊...
    从不中二的忧伤阅读 1,679评论 1 1
  • 一、可变参函数模板 二、可变参类模板 C++ 11 中引入了 可变参模板 (Variadic Template):...
    从不中二的忧伤阅读 542评论 0 0