📄 abstractmanager.java
字号:
package com.bjsxt.oa.managers.impl;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.oa.PagerModel;
import com.bjsxt.oa.SystemContext;
public abstract class AbstractManager extends HibernateDaoSupport {
/**
* 根据HQL语句进行分页查询,分页查询的参数通过ThreadLocal传递
* @param hql
* @return
*/
public PagerModel searchPaginated(String hql){
//从ThreadLocal变量中取出offset和pagesize的值
return searchPaginated(hql,null,SystemContext.getOffset(),SystemContext.getPagesize());
}
/**
* 根据HQL语句进行分页查询,分页查询的参数通过ThreadLocal传递
* @param hql HQL语句
* @param obj HQL语句的参数
* @return
*/
public PagerModel searchPaginated(String hql,Object obj){
return searchPaginated(hql,new Object[]{obj},SystemContext.getOffset(),SystemContext.getPagesize());
}
/**
* 根据HQL语句进行分页查询,分页查询的参数通过ThreadLocal传递
* @param hql hql语句
* @param params HQL语句的多个参数
* @return
*/
public PagerModel searchPaginated(String hql,Object[] params){
return searchPaginated(hql,params,SystemContext.getOffset(),SystemContext.getPagesize());
}
/**
* 根据HQL语句进行分页查询
* @param hql HQL语句
* @param offset 从第几条记录开始查询
* @param pagesize 每页显示多少行
* @return
*/
public PagerModel searchPaginated(String hql,int offset,int pagesize){
return searchPaginated(hql,null,offset,pagesize);
}
/**
* 根据HQL语句进行分页查询
* @param hql hql语句
* @param obj hql语句带的参数值
* @param offset 从第几条记录开始查询
* @param pagesize 每页显示多少行
* @return
*/
public PagerModel searchPaginated(String hql,Object obj,int offset,int pagesize){
return searchPaginated(hql,new Object[]{obj},offset,pagesize);
}
/**
* 根据HQL语句进行分页查询
* @param hql hql语句
* @param params hql语句带的多个参数值
* @param offset 从第几条记录开始查询
* @param pagesize 每页显示多少行
* @return
*/
public PagerModel searchPaginated(String hql,Object[] params,int offset,int pagesize){
//获取记录总数
String countHql = getCountQuery(hql);
Query query = getSession().createQuery(countHql);
if(params != null && params.length > 0){
for(int i=0; i < params.length; i++){
query.setParameter(i, params[i]);
}
}
int total = ((Long)query.uniqueResult()).intValue();
//获取当前页的结果集
query = getSession().createQuery(hql);
if(params != null && params.length > 0){
for(int i=0; i < params.length; i++){
query.setParameter(i, params[i]);
}
}
query.setFirstResult(offset);
query.setMaxResults(pagesize);
List datas = query.list();
PagerModel pm = new PagerModel();
pm.setDatas(datas);
pm.setTotal(total);
return pm;
}
/**
* 根据HQL查询语句,获得查找总记录数的HQL语句
* 如:
* select c from Customer c where ( c.name like ? or c.shortName like ? ) and c.owner.id = ?
* 经过转换,可以得到:
* select count(*) from Customer c where ( c.name like ? or c.shortName like ? ) and c.owner.id = ?
*
* @param hql
* @return
*/
private String getCountQuery(String hql){
int index = hql.indexOf("from");
if(index != -1){
return "select count(*) "+hql.substring(index);
}
throw new RuntimeException("HQL查询语句【"+hql+"】有误,必须包含from子句");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -