oa_05

从现有的分页处理方案中,抽象出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);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值