从现有的分页处理方案中,抽象出AbstractManager,以便将分页逻辑进行封装处理,
使得分页处理更加简单(不需要拷贝分页逻辑)
- 重点理解抽象的概念(如何抽象?抽象哪些内容?)
使得分页处理更加简单(不需要拷贝分页逻辑)
- 重点理解抽象的概念(如何抽象?抽象哪些内容?)
package com.bjsxt.oa.manager.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.manager.SystemException;
public abstract class AbstractManager extends HibernateDaoSupport {
public PagerModel searchPaginated(String hql,int offset,int pagesize){
return searchPaginated(hql, null, offset, pagesize);
}
public PagerModel searchPaginated(String hql,Object value,int offset,int pagesize){
return searchPaginated(hql, new Object[]{value}, offset, pagesize);
}
public PagerModel searchPaginated(String hql,Object[] values,int offset,int pagesize){
//获得总记录数
String countHql = getCountQuery(hql);
Query query = getSession().createQuery(countHql);
if(values != null && values.length > 0){
for(int i=0; i<values.length; i++){
query.setParameter(i, values[i]);
}
}
int total = ((Long)query.uniqueResult()).intValue();
//获得当前页的数据
query = getSession().createQuery(hql);
if(values != null && values.length > 0){
for(int i=0; i<values.length; i++){
query.setParameter(i, values[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 o from Organization o where o.parent is null
* 经过转换,得到
* select count(*) from Organization o where o.parent is null
* @param hql
* @return
*/
private String getCountQuery(String hql){
int index = hql.indexOf("from");
if(index != -1){
return "select count(*) " + hql.substring(index);
}
throw new SystemException("无效的HQL查询语句【"+hql+"】");
}
}
package com.bjsxt.oa.manager.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.oa.PagerModel;
import com.bjsxt.oa.manager.OrgManager;
import com.bjsxt.oa.manager.SystemException;
import com.bjsxt.oa.model.Organization;
public class OrgManagerImpl extends AbstractManager implements OrgManager {
public void addOrg(Organization org, int parentId) {
if(parentId != 0){
org.setParent(findOrg(parentId));
}
getHibernateTemplate().save(org);
//自动生成机构编号
org.setSn(
(org.getParent() == null ? "" : org.getParent().getSn() + "_")
+ org.getId()
);
getHibernateTemplate().update(org);
}
public void delOrg(int orgId) {
Organization org = findOrg(orgId);
//判断子机构列表是否为空
if(org.getChildren().size() > 0){
//throw new RuntimeException("存在子机构信息,不允许删除");
// SystemException se = new SystemException("");
// se.setKey();
// se.setValues(..);
throw new SystemException("errors.org.hassuborg",new Object[]{org.getName(),org.getChildren().size()},"存在子机构信息,不允许删除");
}
//判断人员是否非空
String hql = "select count(*) from Person p where p.org.id = ?";
Long personSize = (Long)getSession().createQuery(hql).setParameter(0, orgId).uniqueResult();
if(personSize > 0){
throw new RuntimeException("机构下面有人员信息,不允许删除");
}
getHibernateTemplate().delete(org);
}
public Organization findOrg(int orgId) {
return (Organization)getHibernateTemplate().load(Organization.class, orgId);
}
public PagerModel searchOrgs(int parentId,int offset,int pagesize) {
String hql = "select o from Organization o where o.parent is null";
if(parentId != 0){
hql = "select o from Organization o where o.parent.id = "+parentId;
}
return searchPaginated(hql,offset, pagesize);
}
public void updateOrg(Organization org, int parentId) {
if(parentId != 0){
org.setParent(findOrg(parentId));
}
getHibernateTemplate().update(org);
}
}