• hibernate的BaseDao,带分页


    BaseDao.java

    public abstract class BaseDao<T extends Serializable> extends HibernateDaoSupport {
    
    
        protected static final Logger logger = LoggerFactory.getLogger(BaseDao.class);
    
        private Class clazz;
    
        private String clazzName;
    
        @Resource(name = "sessionFactoryOne")
        public void setMySessionFactory(SessionFactory sessionFactory) {
            super.setSessionFactory(sessionFactory);
        }
    
        public BaseDao() {
            //使用反射的方式获得类名(表名)
            Type type = this.getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) type;
                Type[] tps = pt.getActualTypeArguments();
                if (tps.length > 0) {
                    this.clazz = (Class) tps[0];
                    this.clazzName = clazz.getName();
                }
            }
        }
    
        /**
         * 插入t
         *
         * @return
         */
        public T saveOrUpdate(T t) {
            currentSession().saveOrUpdate(t);
            return t;
        }
    
        public T get(Serializable id) {
            T t = (T) currentSession().get(this.clazz, id);
            return t;
        }
    
        public List<T> getAll() {
            Query q = currentSession().createQuery("from " + clazzName);
            return q.list();
        }
    
        public int del(long id) {
            Query q = currentSession().createQuery("delete from " + clazzName + " where id = ?");
            return q.setParameter(0, id).executeUpdate();
        }
    
        /**
         * 删除对象
         *
         * @param t
         */
        public T del(T t) {
            currentSession().delete(t);
            return t;
        }
    
        /**
         * 根据HQL查询List
         *
         * @param hql
         * @return
         */
        protected List<T> find(String hql) {
            Query q = currentSession().createQuery(hql);
            return q.list();
        }
    
    
        /**
         * <执行Hql语句>
         *
         * @param hqlString hql
         * @param values    不定参数数组
         */
        public void queryHql(String hqlString, Object... values) {
            Query query = currentSession().createQuery(hqlString);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            query.executeUpdate();
        }
    
        /**
         * <执行Sql语句>
         *
         * @param sqlString sql
         * @param values    不定参数数组
         */
        public void querySql(String sqlString, Object... values) {
            Query query = currentSession().createSQLQuery(sqlString);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            query.executeUpdate();
        }
    
        /**
         * <根据HQL语句查找唯一实体>
         *
         * @param hqlString HQL
         * @param values    不定参数数组
         * @return
         */
        public T getByHQL(String hqlString, Object... values) {
            Query query = currentSession().createQuery(hqlString);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            return (T) query.uniqueResult();
        }
    
    
        /**
         * <根据SQL语句查找唯一实体>
         *
         * @param sqlString SQL语句
         * @param values    不定参数的Object数组
         * @return
         */
        public T getBySQL(String sqlString, Object... values) {
            Query query = currentSession().createSQLQuery(sqlString);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            return (T) query.uniqueResult();
        }
    
    
        /**
         * <根据HQL语句,得到对应的list>
         *
         * @param hqlString HQL语句
         * @param values
         * @return
         */
        public List<T> getListByHQL(String hqlString, Object... values) {
            Query query = currentSession().createQuery(hqlString);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            return query.list();
        }
    
        /**
         * <根据SQL语句,得到对应的list>
         *
         * @param sqlString
         * @param values
         * @return
         */
        public List<T> getListBySQL(String sqlString, Object... values) {
            Query query = currentSession().createSQLQuery(sqlString);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            return query.list();
        }
    
        /**
         * <根据HQL得到记录数>
         *
         * @param hql
         * @param values
         * @return
         */
        public Long countByHql(String hql, Object... values) {
            Query query = currentSession().createQuery(hql);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            return (Long) query.uniqueResult();
        }
    
    
        /**
         * <HQL分页查询>
         *
         * @param hql
         * @param countHql 查询记录条数的HQL语句
         * @param pageNo   下一页
         * @param pageSize 一页总条数
         * @param values
         * @return
         */
        public PageResults<T> findPageByFetchedHql(String hql, String countHql,
                                                   int pageNo, int pageSize, Object... values) {
            PageResults<T> retValue = new PageResults<T>();
            Query query = currentSession().createQuery(hql);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }
            int currentPage = pageNo > 1 ? pageNo : 1;
            retValue.setCurrentPage(currentPage);
            retValue.setPageSize(pageSize);
            if (countHql == null) {
                ScrollableResults results = query.scroll();
                results.last();
                retValue.setTotalCount(results.getRowNumber() + 1);// 设置总记录数
            } else {
                Long count = countByHql(countHql, values);
                retValue.setTotalCount(count.intValue());
            }
            retValue.resetPageNo();
            List<T> itemList = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
            if (itemList == null) {
                itemList = new ArrayList<T>();
            }
            retValue.setResults(itemList);
            return retValue;
        }
    
    }

    分页

    public class PageResults<T> {
        // 下一页
        private int pageNo;
    
        // 当前页
        private int currentPage;
    
        // 每页个个数
        private int pageSize;
    
        // 总条数
        private int totalCount;
    
        // 总页数
        private int pageCount;
    
        // 记录
        private List<T> results;
    
        public int getPageCount() {
            return pageCount;
        }
    
        public void setPageCount(int pageCount) {
            this.pageCount = pageCount;
        }
    
        public int getPageNo() {
            if (pageNo <= 0) {
                return 1;
            } else {
                return pageNo > pageCount ? pageCount : pageNo;
            }
        }
    
        public void setPageNo(int pageNo) {
            this.pageNo = pageNo;
        }
    
        public List<T> getResults() {
            return results;
        }
    
        public void setResults(List<T> results) {
            this.results = results;
        }
    
        public int getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }
    
        public int getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize <= 0 ? 10 : pageSize;
        }
    
        public int getTotalCount() {
            return totalCount;
        }
    
        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }
    
        public void resetPageNo() {
            pageNo = currentPage + 1;
            pageCount = totalCount % pageSize == 0 ? totalCount / pageSize
                    : totalCount / pageSize + 1;
        }
    }
  • 相关阅读:
    Qomolangma实现篇(二):命名空间和别名子系统的实现
    关于Borland's IDE:发生了就发生了吧!
    Qomolangma实现篇(六):Qomo的OOP框架的实现技术
    Qomolangma实现篇(八):Qomo中的AOP框架
    经典的《JavaScript 权威指南》中的“对象”不经典
    Qomo OpenProject beta1 发布!
    弹出当前索引号案例
    tab栏切换效果案例
    [USACO18DEC]Sort It Out P
    [ABC163F]path pass i
  • 原文地址:https://www.cnblogs.com/yinglunstory/p/6090227.html
Copyright © 2020-2023  润新知