范型Servic跟Action

范型Servic和Action

范型Service

package com.founder.bbc.generic;

import java.io.Serializable;
import java.util.List;

/*范型service
 *本框架下的service层已经过简化,在一般单表应用中,不需要写任何代码。
 *但,保留sevice层的目的在于,数据库事务的处理一般放于service层,一个service方法的调用过程,认为是一个事务处理过程,其中可以包含多个相同或者不同的dao的调用。
* 作者:北京师范大学 计算机系 张人杰
* 转载、使用,请保留作者信息
* 此范型已应用于方正某项目
*/
public abstract class AbstractGenericService <PK extends Serializable,T extends GenericEntity<PK>> implements GenericService<PK,T> {

 abstract protected GenericDao<PK,T> getDao();
 
 @Override
 public Integer count(T condition) {
  return getDao().count(condition);
 }

 @Override
 public List<T> listAll() {
  return getDao().listAll();
 }

 @Override
 public DataPage<T> pagedListAll(Integer pageNo, Integer pageSize) {
  List<T> list = getDao().pagedListAll(pageNo,pageSize);
  Integer total = getDao().count(null);
  DataPage<T> page = new DataPage<T>(pageNo,pageSize,total,list);
  return page;
 }

 @Override
 public DataPage<T> pagedListQuery(T condition,Integer pageNo, Integer pageSize) {
  condition.setPageNo(pageNo);
  condition.setPageSize(pageSize);
  List<T> list = getDao().pagedListQuery(condition);
  Integer total = getDao().count(condition);
  DataPage<T> page = new DataPage<T>(pageNo,pageSize,total,list);
  return page;
 }

 @Override
 public DataPage<T> pagedListQuery(T condition) {
  List<T> list = getDao().pagedListQuery(condition);
  Integer total = getDao().count(condition);
  DataPage<T> page = new DataPage<T>(condition.getPageNo(),condition.getPageSize(),total,list);
  return page;
 }

 @Override
 public List<T> queryAll(T condition) {
  return getDao().queryAll(condition);
 }

 @Override
 public T queryObject(T condition) {
  return (T)getDao().queryObject(condition);
 }

 @Override
 public void remove(T condition) {
  getDao().remove(condition);
 }
 @Override
 public void multiPKRemove(T condition) {
  getDao().multiPKRemove(condition);
 }
 @Override
 public PK saveOrUpdate(T object) {
  return getDao().saveOrUpdate(object);
 }
 @Override
 public PK insert(T object) {
  return getDao().insert(object);
 }
 @Override
 public Integer update(T object) {
  return getDao().update(object);
 }
}

范型Service接口

package com.founder.bbc.generic;

import java.io.Serializable;
import java.util.List;

public interface GenericService<PK extends Serializable,T extends GenericEntity<PK>> {
 /**
  * 列出所有内容
  * @return
  */
 public List<T> listAll();
 /**
  * 根据条件查询并返回所有查询结果
  * @param condition
  * @return
  */
 public List<T> queryAll(T condition);
 /**
  * 根据条件查询对象
  * @param condition
  * @return
  */
 public T queryObject(T condition);
 /**
  * 查询所有内容,并分页返回结果
  * @param start
  * @param end
  * @return
  */
 public DataPage<T> pagedListAll(Integer pageNo, Integer pageSize);
 /**
  * 根据条件查询所有内容,并使用condition对象中的分页参数分页返回结果
  * @param condition
  * @return
  */
 public DataPage<T> pagedListQuery(T condition);
 /**
  * 根据条件查询所有内容,并使用传入分页参数分页返回结果
  * @param condition
  * @param pageNo
  * @param pageSize
  * @return
  */
 public DataPage<T> pagedListQuery(T condition,Integer pageNo, Integer pageSize);
 /**
  * 保存或更新实体
  * Oracle,此处采用存储过程完成,以保证id序列的正确,并将新插入的id返回给插入对象
  * @param template
  * @return
  */
 public PK saveOrUpdate(T object);
 /**
  * 插入实体
  * @param template
  * @return
  */
 public PK insert(T object);
 /**
  * 更新实体(使用特殊语句)
  * @param template
  * @return
  */
 public Integer update(T object);
 /**
  * 删除实体,或根据条件删除列表
  * @param condition
  * @return
  */
 public void remove(T condition);
 /**
  * 根据条件多选删除
  * @param condition
  * @return
  */
 public void multiPKRemove(T condition);
 /**
  * 计数
  * @return
  */
 public Integer count(T condition);
}

范型Action

package com.founder.bbc.generic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;
/**
 * 范型Action类,自动调用Service对应方法
 * 需要定义model成员变量,并应用于getModel方法
 * 需要注入对应service并应用于getService方法
 * 需要特殊权限判断的方法,重写此方法
 * 注:不用到的方法,需要重写此方法,去掉方法体
 * 作者:北京师范大学 计算机系 张人杰
 * 转载、使用,请保留作者信息
 * 此范型已应用于方正某项目
 * @param <T>
 * @param <PK>
 */
public abstract class AbstractGenericAction<PK extends Serializable,T extends GenericEntity<PK>,S extends GenericService<PK,T>> implements ModelDriven<T> {
 private Class<PK> pkClazz;
 protected DataPage<T> dataPage;//用于列表页返回至jsp
 protected ArrayList<String> idsStrings;//用于多表删除
 /**
  * 使用此范型类需要实现的方法
  */
 @Override
 abstract public T getModel();
 /**
  * 使用此范型类需要实现的方法
  */
 abstract public void setModel(T model);
 /**
  * 使用此范型类需要实现的方法
  */
 abstract protected S getService();
 /**
  * 得到当前操作者id,用于减少编辑、保存数据前是否可保存,是否保存自身数据的数据库权限判断,即:当前操作者是否可以修改当前记录在sql的where语句中加入判断
  * 注:当前仅允许用户操作自己的数据。如需扩展,扩展此方法为return 0,且在ibatis中做sql的判断
  * @return
  */
 abstract public Long getCurrentOperatorId();
 /**
  * 能够直接访问的条件。用于子类的重写,以用于类似下拉菜单等不需要数据库权限验证的列表的读取
  * @return
  */
 public Boolean accessableCondition(){
  if("listAll".equals(ServletActionContext.getActionMapping().getMethod()))return true;
  return false;
 }
 /**
  * 列出所有数据
  * @return
  */
 public String listAll(){
  List<T> list = getService().listAll();
  ServletActionContext.getRequest().setAttribute("list", list);
  return "listAll";
 }
 /**
  * 查询所有数据
  * @return
  */
 public String queryAll(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  List<T> list = getService().queryAll(getModel());
  ServletActionContext.getRequest().setAttribute("list", list);
  return "queryAll";
 }
 
 /**
  * 分页查询
  * @return
  */
 public String pageList(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  if(getModel().getPageNo()==null){
   getModel().setPageNo(1);
  }
  if(getModel().getPageSize()==null||getModel().getPageSize()>100){
   getModel().setPageSize(20);
  }
  String orderByColumn = (String)getModel().getOtherParams().get("orderByColumn");
  String orderByColumnOrder = (String)getModel().getOtherParams().get("orderByColumnOrder");
  if(orderByColumn!=null){
   getModel().getOtherParams().put("orderByColumn", orderByColumn.replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", ""));
   if(orderByColumnOrder!=null){
    getModel().getOtherParams().put("orderByColumnOrder", orderByColumnOrder.replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", ""));
   }else{
    getModel().getOtherParams().put("orderByColumnOrder","desc");
   }
  }
  dataPage = getService().pagedListQuery(getModel());
  dataPage.setOrderByColumn((String)getModel().getOtherParams().get("orderByColumn"));
  dataPage.setOrderByColumnOrder((String)getModel().getOtherParams().get("orderByColumnOrder"));
  return "pageList";
 }
 /**
  * 编辑或修改
  * @return
  */
 public String toEdit(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  if(getModel().getPK()!=null){
   setModel(getService().queryObject(getModel()));
  }
  return "toEdit";
 }
 /**
  * 添加或保存修改操作
  * 注:主键为空则为插入操作,主键不为空则为更新操作,需要配合对应的存储过程
  */
 public String save(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  getService().saveOrUpdate(getModel());
  ServletActionContext.getRequest().setAttribute("saved", true);
  return "saved";
 }
 /**
  * 删除单条记录
  * @return
  */
 public String delete(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  if(getModel().getPK()!=null){
   getService().queryObject(getModel());
  }
  ServletActionContext.getRequest().setAttribute("deleted", true);
  return "deleted";
 }
 /**
  * 用于多表删除
  * 页面以ids=...&ids=...类似方式传入参数
  * @param ids
  */
 public void SetIds(String id){
  idsStrings.add(id);
 }
 /**
  * 删除多条记录
  * 需要主键为Integer或Long或String
  * 页面以ids=...&ids=...类似方式传入参数
  * @return
  */
 public String MultiPKDelete(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  try{
   if(pkClazz.newInstance() instanceof Integer){
    ArrayList<PK> ids=new ArrayList<PK>();
    for(String idString:idsStrings){
     try{
      ids.add((PK) Integer.valueOf(idString));
     }catch(Exception e){
     }
    }
    if(ids.size()>0){
     getModel().getOtherParams().put("ids", ids);
     getService().multiPKRemove(getModel());
    }
   }else if(pkClazz.newInstance() instanceof Long){
    ArrayList<PK> ids=new ArrayList<PK>();
    for(String idString:idsStrings){
     try{
      ids.add((PK)Long.valueOf(idString));
     }catch(Exception e){
     }
    }
    if(ids.size()>0){
     getModel().getOtherParams().put("ids", ids);
     getService().multiPKRemove(getModel());
    }
   }else if(pkClazz.newInstance() instanceof String){
    if(idsStrings.size()>0){
     getModel().getOtherParams().put("ids", idsStrings);
     getService().multiPKRemove(getModel());
    }
   }
  }catch(Exception e){
  }
  ServletActionContext.getRequest().setAttribute("multiDeleted", true);
  return "multiDeleted";
 }
 /**
  * 显示详细内容
  */
 public String display(){
  if(accessableCondition()!=true){
   getModel().getOtherParams().put("currentOperatorId", getCurrentOperatorId());
  }
  setModel(getService().queryObject(getModel()));
  return "display";
 }
 public DataPage<T> getDataPage() {
  return dataPage;
 }
 public void setDataPage(DataPage<T> dataPage) {
  this.dataPage = dataPage;
 }
}