一.抽取dao层
一.定义通用接口
public interface BaseDao<T> {
public void save(T t);
public void update(T t);
public void delete(T t);
public T findById(Serializable id);
public List<T> findAll();
public Integer findCount(DetachedCriteria detachedCriteria);
public List<T> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize);
}
二.定义实现类
方式一:在实现类的构造方法中传入一个Class
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
private Class clazz;
/*在父类中提供了有参数的构造方法,在子类的构造中,调用父类的有参数的构造*/
public BaseDaoImpl(Class clazz){
this.clazz = clazz;
}
@Override
public void save(T t) {
this.getHibernateTemplate().save(t);
}
@Override
public void update(T t) {
this.getHibernateTemplate().update(t);
}
@Override
public void delete(T t) {
this.getHibernateTemplate().delete(t);
}
@Override
public T findById(Serializable id) {
return (T) this.getHibernateTemplate().get(clazz, id);
}
@Override
public List<T> findAll() {
return (List<T>) this.getHibernateTemplate().find("from "+clazz.getSimpleName());
}
@Override
public Integer findCount(DetachedCriteria detachedCriteria) {
detachedCriteria.setProjection(Projections.rowCount());
List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
if(list.size()>0){
return list.get(0).intValue();
}
return null;
}
@Override
public List<T> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
detachedCriteria.setProjection(null);
return (List<T>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
}
}
这样抽取完成以后,那么在编写dao的时候如果里面都是一些crud的操作,在dao中只需要提供构造方法即可
- 在客户的dao的实现类中
-
public class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao { public CustomerDaoImpl(){ super(Customer.class); } }
-
- 在联系人的dao的实现类中
-
public class LinkManDaoImpl extends BaseDaoImpl<LinkMan> implements LinkManDao { public LinkManDaoImpl(){ super(LinkMan.class); } }
-
注意:子类接口也要继承父类接口
public interface CustomerDao extends BaseDao<Customer>{
}
public interface LinkManDao extends BaseDao<LinkMan>{
}
方式二:通过泛型的反射抽取通用的dao
定义实现类
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
private Class clazz;
public BaseDaoImpl(){
//反射:第一步获得class
Class clazz = this.getClass();
Type type = clazz.getGenericSuperclass();
//得到这个type就是一个参数化的类型,将type强换成参数化类型
ParameterizedType pType = (ParameterizedType)type;
//通过参数化类型获得实际类型:得到一个实际类型参数的数组
Type[] types = pType.getActualTypeArguments();
//只获得第一个实际类型参数
this.clazz = (Class) types[0];
}
@Override
public void save(T t) {
this.getHibernateTemplate().save(t);
}
@Override
public void update(T t) {
this.getHibernateTemplate().update(t);
}
@Override
public void delete(T t) {
this.getHibernateTemplate().delete(t);
}
@Override
public T findById(Serializable id) {
return (T) this.getHibernateTemplate().get(clazz, id);
}
@Override
public List<T> findAll() {
return (List<T>) this.getHibernateTemplate().find("from "+clazz.getSimpleName());
}
@Override
public Integer findCount(DetachedCriteria detachedCriteria) {
detachedCriteria.setProjection(Projections.rowCount());
List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
if(list.size()>0){
return list.get(0).intValue();
}
return null;
}
@Override
public List<T> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
detachedCriteria.setProjection(null);
return (List<T>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
}
}
这样一来使用时只需定义子类继承父类即可
public class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao {
}
public class LinkManDaoImpl extends BaseDaoImpl<LinkMan> implements LinkManDao {
}
子类接口的定义同上!
二.抽取Biz层
2.1 定义通用接口和实现类
public interface IBaseBiz<T> {
/**
* 通过编号查询对象
* @param uuid
* @return
*/
T get(Long uuid);
/**
* 更新
*/
void update(T t);
/**
* 删除
*/
void delete(Long uuid);
/**
* 新增
* @param t
*/
void add(T t);
/**
* 查询所有部门
* @return
*/
List<T> getList();
/**
* 条件查询部门信息
* @param t1
* @return
*/
List<T> getList(T t1,T t2,Object param,int firstResult, int maxResults);
/**
* 获取记录总数
* @param t1
* @param t2
* @param param
* @return
*/
long getCount(T t1,T t2,Object param);
}
public class BaseBiz<T> implements IBaseBiz<T> {
//dao层注入
private IBaseDao<T> baseDao;
public IBaseDao<T> getBaseDao() {
return baseDao;
}
public void setBaseDao(IBaseDao<T> baseDao) {
this.baseDao = baseDao;
}
/**
* 获取所有部门
*/
public List<T> getList() {
return baseDao.getList();
}
/**
* 条件查询部门信息
*/
public List<T> getList(T t1,T t2,Object param,int firstResult, int maxResults) {
return baseDao.getList(t1,t2,param,firstResult, maxResults);
}
/**
* 查询总记录数
*/
public long getCount(T t1,T t2,Object param) {
return baseDao.getCount(t1,t2,param);
}
/**
* 新增部门
*/
public void add(T t) {
baseDao.add(t);
}
/**
* 删除
*/
public void delete(Long uuid){
baseDao.delete(uuid);
}
/**
* 通过编号查询对象
* @param uuid
* @return
*/
public T get(Long uuid){
return baseDao.get(uuid);
}
/**
* 更新
*/
public void update(T t){
baseDao.update(t);
}
}
2.2 使用案例
public interface IDepBiz extends IBaseBiz<Dep> {
}
public class DepBiz extends BaseBiz<Dep> implements IDepBiz {
//数据访问注入
private IDepDao depDao;
public IDepDao getDepDao() {
return depDao;
}
public void setDepDao(IDepDao depDao) {
this.depDao = depDao;
super.setBaseDao(depDao);
}
}
三.抽取Action
3.1 抽取通用action
public class BaseAction<T> {
private T t1;
private T t2;
private Object param;
public T getT2() {
return t2;
}
public void setT2(T t2) {
this.t2 = t2;
}
public Object getParam() {
return param;
}
public void setParam(Object param) {
this.param = param;
}
public T getT1() {
return t1;
}
public void setT1(T t1) {
this.t1 = t1;
}
//分页参数
private int page;//页码
private int rows;//每页的记录数
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
//注入Biz
private IBaseBiz<T> baseBiz;
public IBaseBiz<T> getBaseBiz() {
return baseBiz;
}
public void setBaseBiz(IBaseBiz<T> baseBiz) {
this.baseBiz = baseBiz;
}
/**
* 条件查询部门信息
*/
public void listByPage(){
int firstResult = (page -1) * rows;
List<T> list = baseBiz.getList(t1,t2,param,firstResult,rows);
long total = baseBiz.getCount(t1,t2,param);
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put("total", total);
mapData.put("rows", list);
String listString = JSON.toJSONString(mapData);
write(listString);
}
/**
* 查询所有部门信息
*/
public void list(){
try {
List<T> list = baseBiz.getList();
String listString = JSON.toJSONString(list);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(listString);
} catch (IOException e) {
e.printStackTrace();
};
}
/**
* 转换json数据并返回到页面
* @param jsonString
*/
public void write(String jsonString){
try {
//响应对象
HttpServletResponse response = ServletActionContext.getResponse();
//设置编码
response.setContentType("text/html;charset=utf-8");
//输出给页面
response.getWriter().write(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
}
/**新增。。。修改*/
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
/**
* 增加一个部门
*/
public void add(){
//{"success":true,"message":""}
//返回前端的JSON数据
System.out.println(t);
try {
baseBiz.add(t);
ajaxReturn(true, "新增成功");
} catch (Exception e) {
e.printStackTrace();
ajaxReturn(false, "新增失败");
}
}
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/**
* 删除
* @param jsonString
*/
public void delete(){
try {
baseBiz.delete(id);
ajaxReturn(true, "删除成功");
} catch (Exception e) {
e.printStackTrace();
ajaxReturn(false, "删除失败");
}
}
/**
* 通过编辑查询对象
*/
public void get(){
T t = baseBiz.get(id);
String jsonString = JSON.toJSONString(t);
System.out.println("转换前:" + jsonString);
//{"name":"管理员组","tele":"000011","uuid":1}
String jsonStringAfter = mapData(jsonString, "t");
System.out.println("转换后:" + jsonStringAfter);
write(jsonStringAfter);
}
/**
* 修改原json中的key名称与页面name值相同
* //{"name":"管理员组","tele":"000011","uuid":1}
* @param jsonString JSON数据字符串
* @param prefix 要加上的前缀
* @return {"t.name":"管理员组","t.tele":"000011","t.uuid":1}
*/
public String mapData(String jsonString, String prefix){
Map<String, Object> map = JSON.parseObject(jsonString);
//存储key加上前缀后的值
Map<String, Object> dataMap = new HashMap<String, Object>();
//给每key值加上前缀
for(String key : map.keySet()){
dataMap.put(prefix + "." + key, map.get(key));
}
return JSON.toJSONString(dataMap);
}
/**
* 修改
*/
public void update(){
try {
baseBiz.update(t);
ajaxReturn(true, "修改成功");
} catch (Exception e) {
e.printStackTrace();
ajaxReturn(false, "修改失败");
}
}
/**
* 返回操作结果
* @param success
* @param message
*/
public void ajaxReturn(boolean success, String message){
//返回前端的JSON数据
Map<String, Object> rtn = new HashMap<String, Object>();
rtn.put("success",success);
rtn.put("message",message);
write(JSON.toJSONString(rtn));
}
}
3.2 使用案例
public class DepAction extends BaseAction<Dep> {
//注入Biz
private IDepBiz depBiz;
public IDepBiz getDepBiz() {
return depBiz;
}
public void setDepBiz(IDepBiz depBiz) {
this.depBiz = depBiz;
super.setBaseBiz(this.depBiz);
}
}