OA学习笔记-009-岗位管理的CRUD

时间:2023-12-22 10:28:50

一、分析

Action->Service->Dao

CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承

二、
1.Action层

 package cn.itcast.oa.view.action;

 import java.util.List;

 import javax.annotation.Resource;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; @Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role> { private static final long serialVersionUID = 1L; @Resource
private RoleService roleService; // private Long id;
// private String name;
// private String description; private Role model = new Role(); public Role getModel() {
return model;
} /** 列表 */
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
} /** 删除 */
public String delete() throws Exception {
roleService.delete(model.getId());
return "toList";
} /** 添加页面 */
public String addUI() throws Exception {
return "saveUI";
} /** 添加 */
public String add() throws Exception {
// // 封装到对象中
// Role role = new Role();
// role.setName(model.getName());
// role.setDescription(model.getDescription());
//
// // 保存到数据库
// roleService.save(role); roleService.save(model); return "toList";
} /** 修改页面 */
public String editUI() throws Exception {
// 准备回显的数据
Role role = roleService.getById(model.getId());
ActionContext.getContext().getValueStack().push(role); return "saveUI";
} /** 修改 */
public String edit() throws Exception {
// 1,从数据库中获取原对象
Role role = roleService.getById(model.getId()); // 2,设置要修改的属性
role.setName(model.getName());
role.setDescription(model.getDescription()); // 3,更新到数据库
roleService.update(role); return "toList";
} // --- // public Long getId() {
// return id;
// }
//
// public void setId(Long id) {
// this.id = id;
// }
//
// public String getName() {
// return name;
// }
//
// public void setName(String name) {
// this.name = name;
// }
//
// public String getDescription() {
// return description;
// }
//
// public void setDescription(String description) {
// this.description = description;
// }
}

2.Service层

 package cn.itcast.oa.service.impl;

 import java.util.List;

 import javax.annotation.Resource;

 import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import cn.itcast.oa.dao.RoleDao;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService; @Service
@Transactional
public class RoleServiceImpl implements RoleService { @Resource
private RoleDao roleDao; public Role getById(Long id) {
return roleDao.getById(id);
} public void delete(Long id) {
roleDao.delete(id);
} public void save(Role role) {
roleDao.save(role);
} public void update(Role role) {
roleDao.update(role);
} public List<Role> findAll() {
return roleDao.findAll();
} }

3.Dao层

 package cn.itcast.oa.dao.impl;

 import org.springframework.stereotype.Repository;

 import cn.itcast.oa.base.BaseDaoImpl;
import cn.itcast.oa.dao.RoleDao;
import cn.itcast.oa.domain.Role; @Repository //这里写了@Repository,则父类BaseDaoImpl的sessionFactory可以注入
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao { }

BaseDaoImpl.java

 package cn.itcast.oa.base;

 import java.lang.reflect.ParameterizedType;
import java.util.List; import javax.annotation.Resource; import org.hibernate.Session;
import org.hibernate.SessionFactory; @SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> { //这里不用管事务,因为由spring做
@Resource
private SessionFactory sessionFactory;
private Class<T> clazz; public BaseDaoImpl() {
// 使用反射技术得到T的真实类型
// this表示当前new 的对象
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型,如 new Map<K,V>,此方法返回[k,v]
System.out.println("clazz ---> " + clazz);
} /**
* 获取当前可用的Session
* 给子类方便获取session
* @return
*/
protected Session getSession() {
return sessionFactory.getCurrentSession();
} public void save(T entity) {
getSession().save(entity);
} public void update(T entity) {
getSession().update(entity);
} public void delete(Long id) {
Object obj = getById(id);
if (obj != null) {
getSession().delete(obj);
}
} public T getById(Long id) {
return (T) getSession().get(clazz, id);
} public List<T> getByIds(Long[] ids) {
return getSession().createQuery(//防止被格式化
"FROM User WHERE id IN (:ids)")//
.setParameterList("ids", ids)//
.list();
} public List<T> findAll() {
return getSession().createQuery(//
"FROM " + clazz.getSimpleName())//
.list();
} }

4.View层

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body> <%--
<s:iterator value="#roleList">
<s:property value="id"/>,
<s:property value="%{name}"/>,
<s:property value="description"/>,
<s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗?')">删除</s:a>,
<s:a action="role_editUI?id=%{id}">修改</s:a>
<br/>
</s:iterator>
--%> <s:iterator value="#roleList">
${id},
${name},
${description},
<s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗?')">删除</s:a>,
<s:a action="role_editUI?id=%{id}">修改</s:a>
<br/>
</s:iterator> <br/>
<s:a action="role_addUI">添加</s:a> </body>
</html>