Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1 整合(SSH)(八)

2014-11-24 07:23:29 · 作者: · 浏览: 6
}

return query.list();
}catch(Exception e){
throw new DaoException("按HQL提供者别名与条件查询集合异常,请联系管理员!",e);
}
}

/**
*
*
Description:按HQL提供者别名、条件、分页信息查询集合
*
* @author Eric
* @param hqlProviderSet
* @param queryName
* @param paramMap
* @param page
* @return
* @throws DaoException
*/
@SuppressWarnings("rawtypes")
public List findListByHqlName(IHqlProviderSet hqlProviderSet,String queryName,Map paramMap,PageEntity page) throws DaoException{
String hql;
try{
hql = hqlProviderSet.getHqlByQryName(queryName);

Query query = sessionFactory.getCurrentSession().createQuery(hql);

if(paramMap != null){
hqlArgs(paramMap,query);
}

query.setFirstResult((page.getPageNo() - 1) * page.getPageSize());
query.setMaxResults(page.getPageSize());

return query.list();
}catch(Exception e){
throw new DaoException("按HQL提供者别名、条件、分页信息查询集合异常,请联系管理员!",e);
}
}

/**
*
*
Description:根据传入实体对象返回总记录数
*
* @author Eric
* @param cls
* @return
* @throws DaoException
*/
@SuppressWarnings("rawtypes")
public int findIntRowCountByHqlName(Class cls) throws DaoException{
try{
Query query = sessionFactory.getCurrentSession().createQuery(" select count(c.id) from " + cls.getName() + " c ");
List list = query.list();
int rowCount = ((Number) list.get(0)).intValue();
return rowCount;
}catch(Exception e){
throw new DaoException("查询记录总数异常,请联系管理员!",e);
}
}

/**
*
*
Description:根据HQL提供者别名与条件查询记录总数
*
* @author Eric
* @param hqlProviderSet
* @param queryName
* @param paramMap
* @return
* @throws DaoException
*/
@SuppressWarnings("rawtypes")
public int findIntRowCountByHqlName(IHqlProviderSet hqlProviderSet,String queryName,Map paramMap) throws DaoException{
String hql;
try{
hql = hqlProviderSet.getHqlByQryName(queryName);
Query query = sessionFactory.getCurrentSession().createQuery(hql);
if(paramMap != null){
hqlArgs(paramMap,query);
}
List list = query.list();
int rowCount = ((Number) list.get(0)).intValue();
return rowCount;
}catch(Exception e){
throw new DaoException("执行带参数的查询记录总数异常,请联系管理员!",e);
}
}

/**
*
*
Description:为Hibernate查询设置参数
*
* @author Eric
* @param argsMap
* @param query
*/
@SuppressWarnings("rawtypes")
public void hqlArgs(Map argsMap,Query query){
Iterator itKey = argsMap.keySet().iterator();
while(itKey.hasNext()){
String key = (String) itKey.next();
@SuppressWarnings("unused")
Object obj = argsMap.get(key);
if(argsMap.get(key) instanceof List){
query.setParameterList(key,(List) argsMap.get(key));
}else{
query.setParameter(key,argsMap.get(key));
}
}
}

public SessionFact