一、分析
Action->Service->Dao
CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承
二、
1.Action层
1 package cn.itcast.oa.view.action; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.context.annotation.Scope; 8 import org.springframework.stereotype.Controller; 9 10 import cn.itcast.oa.domain.Role; 11 import cn.itcast.oa.service.RoleService; 12 13 import com.opensymphony.xwork2.ActionContext; 14 import com.opensymphony.xwork2.ActionSupport; 15 import com.opensymphony.xwork2.ModelDriven; 16 17 @Controller 18 @Scope("prototype") 19 public class RoleAction extends ActionSupport implements ModelDriven<Role> { 20 21 private static final long serialVersionUID = 1L; 22 23 @Resource 24 private RoleService roleService; 25 26 // private Long id; 27 // private String name; 28 // private String description; 29 30 private Role model = new Role(); 31 32 public Role getModel() { 33 return model; 34 } 35 36 /** 列表 */ 37 public String list() throws Exception { 38 List<Role> roleList = roleService.findAll(); 39 ActionContext.getContext().put("roleList", roleList); 40 return "list"; 41 } 42 43 /** 删除 */ 44 public String delete() throws Exception { 45 roleService.delete(model.getId()); 46 return "toList"; 47 } 48 49 /** 添加页面 */ 50 public String addUI() throws Exception { 51 return "saveUI"; 52 } 53 54 /** 添加 */ 55 public String add() throws Exception { 56 // // 封装到对象中 57 // Role role = new Role(); 58 // role.setName(model.getName()); 59 // role.setDescription(model.getDescription()); 60 // 61 // // 保存到数据库 62 // roleService.save(role); 63 64 roleService.save(model); 65 66 return "toList"; 67 } 68 69 /** 修改页面 */ 70 public String editUI() throws Exception { 71 // 准备回显的数据 72 Role role = roleService.getById(model.getId()); 73 ActionContext.getContext().getValueStack().push(role); 74 75 return "saveUI"; 76 } 77 78 /** 修改 */ 79 public String edit() throws Exception { 80 // 1,从数据库中获取原对象 81 Role role = roleService.getById(model.getId()); 82 83 // 2,设置要修改的属性 84 role.setName(model.getName()); 85 role.setDescription(model.getDescription()); 86 87 // 3,更新到数据库 88 roleService.update(role); 89 90 return "toList"; 91 } 92 93 // --- 94 95 // public Long getId() { 96 // return id; 97 // } 98 // 99 // public void setId(Long id) { 100 // this.id = id; 101 // } 102 // 103 // public String getName() { 104 // return name; 105 // } 106 // 107 // public void setName(String name) { 108 // this.name = name; 109 // } 110 // 111 // public String getDescription() { 112 // return description; 113 // } 114 // 115 // public void setDescription(String description) { 116 // this.description = description; 117 // } 118 }
2.Service层
1 package cn.itcast.oa.service.impl; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.stereotype.Service; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import cn.itcast.oa.dao.RoleDao; 11 import cn.itcast.oa.domain.Role; 12 import cn.itcast.oa.service.RoleService; 13 14 @Service 15 @Transactional 16 public class RoleServiceImpl implements RoleService { 17 18 @Resource 19 private RoleDao roleDao; 20 21 public Role getById(Long id) { 22 return roleDao.getById(id); 23 } 24 25 public void delete(Long id) { 26 roleDao.delete(id); 27 } 28 29 public void save(Role role) { 30 roleDao.save(role); 31 } 32 33 public void update(Role role) { 34 roleDao.update(role); 35 } 36 37 public List<Role> findAll() { 38 return roleDao.findAll(); 39 } 40 41 }
3.Dao层
1 package cn.itcast.oa.dao.impl; 2 3 import org.springframework.stereotype.Repository; 4 5 import cn.itcast.oa.base.BaseDaoImpl; 6 import cn.itcast.oa.dao.RoleDao; 7 import cn.itcast.oa.domain.Role; 8 9 @Repository //这里写了@Repository,则父类BaseDaoImpl的sessionFactory可以注入 10 public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao { 11 12 }
BaseDaoImpl.java
1 package cn.itcast.oa.base; 2 3 import java.lang.reflect.ParameterizedType; 4 import java.util.List; 5 6 import javax.annotation.Resource; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 11 @SuppressWarnings("unchecked") 12 public abstract class BaseDaoImpl<T> implements BaseDao<T> { 13 14 //这里不用管事务,因为由spring做 15 @Resource 16 private SessionFactory sessionFactory; 17 private Class<T> clazz; 18 19 public BaseDaoImpl() { 20 // 使用反射技术得到T的真实类型 21 // this表示当前new 的对象 22 ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型 23 this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型,如 new Map<K,V>,此方法返回[k,v] 24 System.out.println("clazz ---> " + clazz); 25 } 26 27 /** 28 * 获取当前可用的Session 29 * 给子类方便获取session 30 * @return 31 */ 32 protected Session getSession() { 33 return sessionFactory.getCurrentSession(); 34 } 35 36 public void save(T entity) { 37 getSession().save(entity); 38 } 39 40 public void update(T entity) { 41 getSession().update(entity); 42 } 43 44 public void delete(Long id) { 45 Object obj = getById(id); 46 if (obj != null) { 47 getSession().delete(obj); 48 } 49 } 50 51 public T getById(Long id) { 52 return (T) getSession().get(clazz, id); 53 } 54 55 public List<T> getByIds(Long[] ids) { 56 return getSession().createQuery(//防止被格式化 57 "FROM User WHERE id IN (:ids)")// 58 .setParameterList("ids", ids)// 59 .list(); 60 } 61 62 public List<T> findAll() { 63 return getSession().createQuery(// 64 "FROM " + clazz.getSimpleName())// 65 .list(); 66 } 67 68 }
4.View层
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>My JSP 'index.jsp' starting page</title> 6 </head> 7 <body> 8 9 <%-- 10 <s:iterator value="#roleList"> 11 <s:property value="id"/>, 12 <s:property value="%{name}"/>, 13 <s:property value="description"/>, 14 <s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗?')">删除</s:a>, 15 <s:a action="role_editUI?id=%{id}">修改</s:a> 16 <br/> 17 </s:iterator> 18 --%> 19 20 <s:iterator value="#roleList"> 21 ${id}, 22 ${name}, 23 ${description}, 24 <s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗?')">删除</s:a>, 25 <s:a action="role_editUI?id=%{id}">修改</s:a> 26 <br/> 27 </s:iterator> 28 29 <br/> 30 <s:a action="role_addUI">添加</s:a> 31 32 </body> 33 </html>