SSH 泛型的实现
BaseAction.java
package org.onlinecourse.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.onlinecourse.dao.Page;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public abstract class BaseAction<T> extends ActionSupport implements
SessionAware, ModelDriven<T> {
private static final long serialVersionUID = 1L;
// 注意,使用 protected
// 将 model以及分页封装到 BaseAction层
protected T entity;
protected Page<T> pageType;
@Resource(name="gson")
protected Gson gson;
protected int page=1;
protected int pagesize=5;
protected Map<String, Object> session;
protected String result;
// private String prefix="/WEB-INF/view/";
protected String prefix = "/view/";
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getResult() {
return prefix + result;
}
public void setResult(String result) {
this.result = result;
}
public Map<String, Object> getSession() {
return session;
}
public HttpServletResponse getResponse(String contentType) {
HttpServletResponse response =ServletActionContext.getResponse();
response.setContentType(contentType);
return response;
}
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
public void responseJson(String data) throws IOException{
// 指定返回数据的格式为json
HttpServletResponse response=getResponse("application/json;charset=UTF-8");
PrintWriter printWriter = response.getWriter();
printWriter.print(data);
//输出 json数据
System.out.println(data);
printWriter.flush();
printWriter.close();
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public T getModel() {
return entity;
}
public T getEntity() {
return entity;
}
public abstract void setEntity(T entity);
public Page<T> newPage(int pageNo,int pageSize){
Page<T> page=new Page<T>();
page.setPageNo(pageNo);
page.setPageSize(pageSize);
return page;
}
public Map<String,Object> getMap(Object obj,int size){
Map<String, Object> map = new HashMap<String, Object>();
// map中必须包含 Rows和Total属性
map.put("Rows", obj);
map.put("Total", size);
return map;
}
public Page<T> getPageType() {
return pageType;
}
public void setPageType(Page<T> pageType) {
this.pageType = pageType;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public Gson getGson() {
return gson;
}
public void setGson(Gson gson) {
this.gson = gson;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
}
IBaseService.java
package org.onlinecourse.service;
import java.io.Serializable;
import java.util.List;
public interface IBaseService<T , ID extends Serializable>{
/**
* 保存指定实体类
* @param entityobj
*/
public abstract void save(T entity);
/**
* 删除指定实体
*
* @param entityobj
* 实体类
*/
public abstract void delete(T entity);
public void deleteInMany(final Class<T> entityClass, final List<Integer> ids) ;
/**
* 获取所有实体集合
*
* @param entityClass
* 实体
* @return 集合
*/
public abstract List<T> findAll(Class<T> entityClass);
/**
* 更新或保存指定实体
*
* @param entity
* 实体类
*/
public abstract void saveOrUpdate(T entity);
/**
* 查找指定ID实体类对象
*
* @param entityClass
* 实体Class
* @param id
* 实体ID
* @return 实体对象
*/
public abstract T findById(Class<T> entityClass, ID id);
/**
* 获取指定实体Class的记录总数
*
* @param entityClass
* 实体Class
* @return 记录总数
*/
public abstract int findTotalCount(Class<T> entityClass);
/**
* 查找指定属性的实体集合
*
* @param entityClass
* 实体
* @param propertyName
* 属性名
* @param value
* 条件
* @return 实体集合
*/
public abstract List<T> findByProperty(Class<T> entityClass,
String propertyName, Object value);
/**
* 模糊查询指定条件对象集合 <br>
* 用法:可以实例化一个空的T对象,需要查询某个字段,就set该字段的条件然后调用本方法<br>
* 缺点:目前测试貌似只能支持String的模糊查询,虽然有办法重写,但没必要,其他用HQL<br>
*
* @param entity
* 条件实体
* @return 结合
*/
public abstract List<T> findByExample(T entity);
/**
* 补充方法(未测) 据说可以无视session的状态持久化对象
*
* @param entity
* 实体类
* @return 持久后的实体类
*/
public abstract T merge(T entity);
/**
* 清除实体的锁定状态<br>
* 方法未测
*
* @param entity
* 实体
*/
public abstract void attachClean(T entity);
}
BaseServiceImpl.java
package org.onlinecourse.service.impl;
import java.io.Serializable;
import java.util.List;
import org.onlinecourse.dao.IBaseHibernateDao;
import org.onlinecourse.service.IBaseService;
public abstract class BaseServiceImpl<T, ID extends Serializable> implements IBaseService<T, ID> {
protected IBaseHibernateDao<T, ID> baseHibernateDao;
@Override
public void save(T entity) {
baseHibernateDao.save(entity);
}
@Override
public void delete(T entity) {
baseHibernateDao.delete(entity);
}
public void deleteInMany(final Class<T> entityClass, final List<Integer> ids) {
baseHibernateDao.deleteInMany(entityClass, ids);
}
@Override
public List<T> findAll(Class<T> entityClass) {
return baseHibernateDao.findAll(entityClass);
}
@Override
public void saveOrUpdate(T entity) {
baseHibernateDao.saveOrUpdate(entity);
}
@Override
public T findById(Class<T> entityClass, ID id) {
return baseHibernateDao.findById(entityClass, id);
}
@Override
public int findTotalCount(Class<T> entityClass) {
return baseHibernateDao.findTotalCount(entityClass);
}
@Override
public List<T> findByExample(T entity) {
return baseHibernateDao.findByExample(entity);
}
public List<T> findByProperty(Class<T> entityClass,
String propertyName, Object value){
return baseHibernateDao.findByProperty(entityClass, propertyName, value);
};
@Override
public T merge(T entity) {
return baseHibernateDao.merge(entity);
}
@Override
public void attachClean(T entity) {
baseHibernateDao.attachClean(entity);
}
public IBaseHibernateDao<T, ID> getBaseHibernateDao() {
return baseHibernateDao;
}
/*
* setBaseHibernateDao
*/
public abstract void setBaseHibernateDao(IBaseHibernateDao<T, ID> baseHibernateDao);
}
[解决办法]
一连贴那么多代码,看着晕啊
[解决办法]
又一条汉子。提示一下,你这样估计没人看。好待你用代码(就是表情的左边第一个)框一下呀