zoukankan      html  css  js  c++  java
  • 通用DAO和DaoImpl

      public interface GenericDao<T> {
     void create(T entity);
     void delete(T entity);
     void update(T entity);
     
     T findById(Serializable oid);
     List<T> findAll();
     PageModel<T> findByPager(int pageNo, int pageSize);
    }

    public class GenericDaoImpl<T> implements GenericDao<T> {
     
     private Class<T> entityClass;
     
     private String entityName;
     
     @SuppressWarnings("unchecked")
     public GenericDaoImpl(){
      //通过反射获取泛型的参数类型信息
      Type type = this.getClass().getGenericSuperclass();
      ParameterizedType pt = (ParameterizedType)type;
      
      Type argType = pt.getActualTypeArguments()[0];
      
      entityClass = (Class<T>)argType;
      
      this.entityName = entityClass.getName();
     }
     

     @Override
     public void create(T entity) {
      HibernateUtil.getSession().save(entity);
     }

     @Override
     public void delete(T entity) {
      HibernateUtil.getSession().delete(entity);
     }

     @SuppressWarnings("unchecked")
     @Override
     public List<T> findAll() {
    //  return HibernateUtil.getSession().createQuery("from " + entityName).list();
      return HibernateUtil.getSession()
           .createCriteria(entityClass)
           .list();
     }

     @SuppressWarnings("unchecked")
     @Override
     public T findById(Serializable oid) {
      return (T)HibernateUtil.getSession().load(entityClass, oid);
     }

     @SuppressWarnings("unchecked")
     @Override
     public PageModel<T> findByPager(int pageNo, int pageSize) {
      PageModel<T> pm = new PageModel<T>();
      
      //Integer temp = (Integer)HibernateUtil.getSession().createCriteria(entityClass).setProjection(Projections.rowCount()).uniqueResult();
      Long temp = (Long)HibernateUtil.getSession()
          .createQuery("select count(o) from " + entityName + " o")
          .uniqueResult();
      
      if(null != temp){
       pm.setRecordCount(temp.intValue());
       pm.setDatas(HibernateUtil.getSession()
         .createCriteria(entityClass)
         .setMaxResults(pageSize)
         .setFirstResult((pageNo - 1) * pageSize)
         .list());
      }
      
      return pm;
     }

     @SuppressWarnings("unchecked")
     @Override
     public void update(T entity) {
      HibernateUtil.getSession().update(entity);
     }
     
    }

    分页实体类PageModel

    public class PageModel<T> implements Serializable {


    private static final long serialVersionUID = -3461444186416298464L;

    private long recordCount; //总记录数

    private List<T> data; //分页数据列表

    private int pageNo = 1; //当前页号

    private int pageSize = 10; //每页显示的行数

    private int pageCount; //总页数

    public long getRecordCount() {

    return recordCount;

    }

    public void setRecordCount(long recordCount) {

    this.recordCount = recordCount;

    }

    public List<T> getData() {

    return data;

    }

    public void setData(List<T> data) {

    this.data = data;

    }

    public int getPageNo() {

    if(pageNo < 1){

    pageNo = 1;

    }

    if(pageNo > getPageCount()){

    pageNo = pageCount;

    }

    return pageNo;

    }

    public void setPageNo(int pageNo) {

    this.pageNo = pageNo;

    }

    public int getPageSize() {

    return pageSize;

    }

    public void setPageSize(int pageSize) {

    this.pageSize = pageSize;

    }

    public int getPageCount() {

    pageCount = (int)((recordCount + pageSize - 1) / pageSize);

    return pageCount;

    }

    public void setPageCount(int pageCount) {

    this.pageCount = pageCount;

    }

    }

    其他的类或是接口只需要extends

  • 相关阅读:
    菜鸟学python之程序初体验
    菜鸟学python之大数据的初认识
    js获取本地ip地址和外网IP地址
    Js中foreach()用法及使用的坑
    模拟实现Promise,探究Promise原理
    搞懂JS的事件循环(Event Loop)和宏任务/微任务
    NodeJS 中的 LRU 缓存(CLOCK-2-hand)实现
    设计模式在前端项目中的应用
    JS 中一些高效的魔法运算符
    Js中如何克隆对象?
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218464.html
Copyright © 2011-2022 走看看