BaseDao抽取:
package com.xushuai.bos.dao;
import java.io.Serializable;
import java.util.List;
import com.xushuai.bos.utils.PageBean;
/**
* 持久层通用接口
* @author xushuai
*
* @param <T>
*/
public interface BaseDao<T> {
/**
* 添加一个entity
* @param entity
*/
public void save(T entity);
/**
* 删除一个entity
* @param entity
*/
public void delete(T entity);
/**
* 修改一个entity
* @param entity
*/
public void update(T entity);
/**
* 按ID查询
* @param id
* @return
*/
public T findById(Serializable id);
/**
* 查询所有
* @param entity
* @return
*/
public List<T> findAll(T entity);
/**
* 分页查询
* @param pageBean
*/
public void findByPage(PageBean pageBean);
/**
* 更新
* @param queryName
* @param objects
*/
public void executeUpdate(String queryName,Object...objects);
/**
* 保存或更新
* @param entity
*/
public void saveOrUpdate(T entity);
}
BaseAction抽取:
package com.xushuai.bos.web.action;
import java.lang.reflect.ParameterizedType;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
public final static String HOME = "home";
public final static String LIST = "list";
protected T model;
//获取运行时期实际类型,并对其进行实例化
public BaseAction() {
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
Class entityClass = (Class) type.getActualTypeArguments()[0];
try {
model = (T) entityClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public T getModel() {
return model;
}
}