hibernate分页

yasashikokoro 2011-10-08

package com.redbaby.dao;

import java.math.BigDecimal;

importjava.util.HashMap;

importjava.util.List;

import java.util.Map;

import javacommon.xsqlbuilder.XsqlBuilder;import javacommon.xsqlbuilder.XsqlBuilder.XsqlFilterResult;

import org.hibernate.Query;

importorg.hibernate.Session;

importorg.hibernate.transform.Transformers;

importorg.springframework.dao.DataAccessException;

importorg.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.redbaby.util.Page;

public abstract class HibernateGenericDao extends HibernateDaoSupport {

/**

*得到一条数据(整个Object对象)

*

*hql:fromObjectwhere.....

*

*@paramhql

*@paramvalues

*@return

*@throwsDataAccessException

*/

 public Object getInfo(String hql, Object... values) throws DataAccessException {

  List list = null;  list = super.getHibernateTemplate().find(hql, values);

  if (list != null && list.size() > 0) {

returnlist.get(0);

}

  return null;

 }

 /**

*得到对象Object某一个属性select中必选指定要查询字段名称(一个)

*

*hql:selectp.idfromobjectwhere1=1.....

*/

 public Object getInfoParam(final String hql, Object... values) throws DataAccessException {

  Session session = super.getSession();  Query query = session.createQuery(hql);

  if (values != null) {

for(inti=0;i<values.length;i++){

query.setParameter(i,values[i]);

}

}

Objectobj=query.uniqueResult();

returnobj;

 }

 /**

*得到List

*

*@paramhql

*@paramvalues

*@return

*@throwsDataAccessDataAccessException

*/

publicList<?>getListByParam(Stringhql,Object...values)throwsDataAccessException{

returnsuper.getHibernateTemplate().find(hql,values);

 }

 /**

*执行带参数的DMLUPDATEDELETE

*

*@paramhql

*@paramvalues

*@return

*@throwsDataAccessDataAccessException

*/

 public void updateForParam(final String hql, final Object... values) throws DataAccessException {

  super.getHibernateTemplate().execute(new HibernateCallback() {

publicObjectdoInHibernate(Sessionsession){

Queryquery=session.createQuery(hql);

if(values!=null){

for(inti=0;i<values.length;i++){

query.setParameter(i,values[i]);

}

}

returnquery.executeUpdate();

}

});

 }

 /**

*HQL分页(带查询)

*

*@paramhql

*@paramparams

*@parampage

*@return

*@throwsDataAccessDataAccessException

*/

publicList<?>getListPage(Stringhql,Map<Object,Object>params,Pagepage)throwsDataAccessException{

if(params==null){

params=newHashMap<Object,Object>();

}

intfirst=0;

intmax=0;

if(null!=page){

first=(int)((page.getCurrentPage()-1)*page.getPageSize());

max=(int)page.getPageSize();

}

finalintfirstResult=first;

  final int maxResults = max;

  XsqlFilterResult result = new XsqlBuilder().generateHql(hql, params);

Longcount=getTotalCount(result.getXsql(),1);

if(page!=null){

page.setTotalSize(count);

  }

  final Page pageFin = page;

finalStringnewHql=result.getXsql();

returnsuper.getHibernateTemplate().executeFind(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession){

Queryquery=session.createQuery(newHql);

if(pageFin!=null){

query.setFirstResult(firstResult);

query.setMaxResults(maxResults);

}

returnquery.list();

}

});

 }

 /**

*SQL分页查询

*/

 public List<?> getListPageSQL(String sql, Map<Object, Object> params, Page page, Class<?> tclass) throws DataAccessException {

  XsqlFilterResult result = new XsqlBuilder().generateHql(sql, params);

if(page!=null){

Longcount=getTotalCount(result.getXsql(),2);

page.setTotalSize(count);//

  }

  Session session = super.getSession();

QueryqueryList=session.createSQLQuery(result.getXsql()).addEntity(tclass);

if(page!=null){

queryList.setFirstResult((int)((page.getCurrentPage()-1)*page.getPageSize()));//

queryList.setMaxResults((int)page.getPageSize());//

}

List<?>tList=queryList.list();

return(List<?>)tList;

 }

 // 返回总记录数

publiclonggetTotalCount(Stringhql,longnum){

longcount=0;

intsql_from=hql.toLowerCase().indexOf("from");

intsql_orderby=hql.toLowerCase().indexOf("orderby");

StringcountStr="";

if(sql_orderby>0){

countStr="selectcount(*)"+hql.substring(sql_from,sql_orderby);

}else{

countStr="selectcount(*)"+hql.substring(sql_from);

}

Sessionsession=super.getSession();

if(num==1){//1,HQL查询

Queryquery=session.createQuery(countStr);

count=(Long)query.uniqueResult();

}elseif(num==2){//2,SQL查询

count=((BigDecimal)(session.createSQLQuery(countStr)).uniqueResult()).longValue();//

}

  return count;

 }

 /**

*通过sql查询Object...values形式且无分页

*

*@paramsql

*@paramtclass

*@paramvalues

*@return

*@throwsDataAccessException

*/

publicList<?>getListByParamSQL(Stringsql,Class<?>tclass,Object...values)throwsDataAccessException{

Sessionsession=super.getSession();

QueryqueryList=session.createSQLQuery(sql).addEntity(tclass);

if(values!=null){

for(inti=0;i<values.length;i++){

queryList.setParameter(i,values[i]);

}

}

List<?>tList=queryList.list();

return(List<?>)tList;

 }

 /**

*通过sql查询params形式且无分页

*

*@paramhql

*@paramparams

*@return

*@throwsDataAccessException

*/

publicList<?>getListByParam(Stringhql,Map<Object,Object>params)throwsDataAccessException{

if(params==null){

params=newHashMap<Object,Object>();

}

XsqlFilterResultresult=newXsqlBuilder().generateHql(hql,params);

finalStringnewHql=result.getXsql();

returnsuper.getHibernateTemplate().executeFind(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession){

Queryquery=session.createQuery(newHql);

returnquery.list();

}

});

 }

 /**

*无分页,无所属类的SQL查询

*/

 public List<Map> getListMapBySQL(String sql, Map<Object, Object> params) throws DataAccessException {

  XsqlFilterResult result = new XsqlBuilder().generateHql(sql, params);

Sessionsession=super.getSession();

QueryqueryList=session.createSQLQuery(result.getXsql());

List<Map>tListMap=queryList.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

  return tListMap;

 }

 /**

*有分页,无所属类的SQL查询

*/

 public List<Map> getListMapByPageSQL(String sql, Map<Object, Object> params, Page page) throws DataAccessException {

  XsqlFilterResult result = new XsqlBuilder().generateHql(sql, params);

if(page!=null){

Longcount=getTotalCount(result.getXsql(),2);

page.setTotalSize(count);//

}

Sessionsession=super.getSession();

QueryqueryList=session.createSQLQuery(result.getXsql());

if(page!=null){

queryList.setFirstResult((int)((page.getCurrentPage()-1)*page.getPageSize()));//

queryList.setMaxResults((int)page.getPageSize());//

}

List<Map>tListMap=queryList.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

  return tListMap;

 }

}

相关推荐

LetonLIU / 0评论 2020-05-29
东方咖啡屋 / 0评论 2020-01-06