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

  • 相关阅读:
    【BZOJ1006】神奇的国度(弦图)
    弦图
    【BZOJ2946】公共串(后缀数组)
    【POJ1743】Musical Theme(后缀数组)
    JAVA和Tomcat运维整理
    linux shell 之if-------用if做判断
    Linux curl命令详解
    Intel HEX文件解析
    Linux bridge-utils tunctl 使用
    怎样查询锁表的SQL
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218464.html
Copyright © 2011-2022 走看看