Package com.lgx8.common.dao.impl

Source Code of com.lgx8.common.dao.impl.BaseDao

/**
*
*/
package com.lgx8.common.dao.impl;

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.lgx8.common.PageList;

/**
* @author lihui
*
*/
public class BaseDao extends HibernateDaoSupport {

  /**
   * 分页查询
   * @param hql hql语句
   * @param values 参数数组
   * @param offset 起始索引
   * @param pageSize 每页显示的记录数
   * @return
   */
  @SuppressWarnings("unchecked")
  public List findByPage(final String hql, final Object[] values,
      final int offset, final int pageSize) {
    List list = getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
        Query query = session.createQuery(hql);
        for (int i = 0; i < values.length; i++) {
          query.setParameter(i, values[i]);
        }
        List result = query.setFirstResult(offset).setMaxResults(
            pageSize).list();
        return result;
      }
    });
    return list;
  }

 
  /**
   * 分页查询(用于gt-grid表格组件)
   * @param hql hql语句
   * @param values 参数数组
   * @param pageNum 页码
   * @param pageSize 每页显示的记录数
   * @return PageList分页查询结果的包装类
   */
  @SuppressWarnings("unchecked")
  public PageList findByPage4Report(final String hql, final Object[] values,
      final int pageNum, final int pageSize) {
   
    String counthql = hql.substring(hql.toLowerCase().indexOf("from "));
        counthql = "select count(*) "+counthql;
        final String queryStr = counthql;
    List list = getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
        Query query = session.createQuery(queryStr);
        for (int i = 0; i < values.length; i++) {
          query.setParameter(i, values[i]);
        }
        List result = query.list();
        return result;
      }
    });
    int totalRecordNum = Integer.parseInt(list.get(0).toString());
   
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
        Query query = session.createQuery(hql);
        for (int i = 0; i < values.length; i++) {
          query.setParameter(i, values[i]);
        }
        List result = query.setFirstResult((pageNum-1)*pageSize).setMaxResults(
            pageSize).list();
        return result;
      }
    });
   
    int totalPageNum = totalRecordNum/pageSize;
    if(totalRecordNum%pageSize!=0) {
      totalPageNum+=1;
    }
    PageList pageList = new PageList();
    pageList.setPageSize(pageSize);
    pageList.setCurrentPageNum(pageNum);
    pageList.setDataList(data);
    pageList.setTotalPageNum(totalPageNum);
    pageList.setTotalRecordNum(totalRecordNum);
    return pageList;
  }
}
TOP

Related Classes of com.lgx8.common.dao.impl.BaseDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.