• DaoImpl中实现查询分页-使用HibernateCallback来做更加方便


    /**
     * 
     */
    package com.wolfgang.dao;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import com.wolfgang.VO.PageVO;
    
    /**
     * @author Administrator
     * 
     */
    public class BaseDaoImp extends HibernateDaoSupport implements IBaseDao {
    
        public Object get(Integer id) {
            return getHibernateTemplate().get(Object.class, id);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see com.wolfgang.dao.IBaseDao#save(java.lang.Object)
         */
    
        public void save(Object obj) {
            getHibernateTemplate().save(obj);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see com.wolfgang.dao.IBaseDao#update(java.lang.Object)
         */
    
        public void update(Object obj) {
            getHibernateTemplate().update(obj);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see com.wolfgang.dao.IBaseDao#delete(java.lang.Object)
         */
        public void delete(Object obj) {
            getHibernateTemplate().delete(obj);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see com.wolfgang.dao.IBaseDao#findById(int)
         */
        public <T> T findById(Class<T> className, int id) {
            return (T) getHibernateTemplate().get(className, id);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see com.wolfgang.dao.IBaseDao#getAll(java.lang.Class)
         */
        public <T> List<T> getAll(Class<T> className) {
            return getHibernateTemplate().loadAll(className);
    
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see com.wolfgang.dao.IBaseDao#findPage(java.lang.String, int, int,
         * java.lang.Object[])
         */
        public PageVO findPage(final String hql, int offset, int pageSize,
                final Object... params) {
            final String totalHql = getTotalHql(hql);
            Long total = (Long) getHibernateTemplate().execute(
                    new HibernateCallback() {
    
                        public Object doInHibernate(Session session)
                                throws HibernateException, SQLException {
                            Query query = session.createQuery(totalHql);
                            return query.uniqueResult();
                        }
                    });
    
            List data = (List) getHibernateTemplate().execute(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session)
                                throws HibernateException, SQLException {
    
                            Query query = session.createQuery(hql);
                            if (params != null) {
                                for (int i = 0; i < params.length; i++) {
                                    query.setParameter(i, params[i]);
                                }
                            }
                            
                            return query.list();
                        }
                    });
    
            PageVO pageVO = new PageVO();
            pageVO.setTotal(total);
            pageVO.setData(data);
            return pageVO;
        }
    
        private String getTotalHql(String hql) {
            int index = hql.indexOf(hql);
            if (index == -1) {
                return null;
            } else {
                return "select count(*)" + hql.substring(index);
            }
    
        }
    
    }
  • 相关阅读:
    MySQL Case When 用法
    Delphi磁性窗口
    一个灵巧的Delphi多播实事件现方案.
    Delphi bpl 插件框架
    Win7下超级管理员创建普通权限任务
    Delphi 插件(Plugins)创建、调试与使用应用程序扩展
    Dll中导出类Delphi实战
    让你的程序支持插件
    构造一个通用的回调Thunk.(把回调函数指向对象的方法的办法)
    打造类.NET带垃圾回收功能的Delphi版GDIPlus
  • 原文地址:https://www.cnblogs.com/unixshell/p/3418560.html
Copyright © 2020-2023  润新知