一、分析

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>

OA学习笔记-009-岗位管理的CRUD的更多相关文章

  1. OA学习笔记-008-岗位管理Action层实现

    一.分析 1,设计实体/表 设计实体 --> JavaBean --> hbm.xml --> 建表 2,分析有几个功能,对应几个请求. 3,实现功能: 1,写Action类,写Ac ...

  2. Linux内核学习笔记-2.进程管理

    原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  3. linux kernel学习笔记-5内存管理_转

    void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...

  4. Linux学习笔记(五) 账号管理

    1.用户与组账号 用户账号:包括实际人员和逻辑性对象(例如应用程序执行特定工作的账号) 每一个用户账号包含一个唯一的用户 ID 和组 ID 标准用户是系统安装过程中自动创建的用户账号,其中除 root ...

  5. Linux学习笔记(六) 进程管理

    1.进程基础 当输入一个命令时,shell 会同时启动一个进程,这种任务与进程分离的方式是 Linux 系统上重要的概念 每个执行的任务都称为进程,在每个进程启动时,系统都会给它指定一个唯一的 ID, ...

  6. Qt学习笔记-Widget布局管理

    Qt学习笔记4-Widget布局管理       以<C++ GUI Programming with Qt 4, Second Edition>为参考 实例:查找对话框 包含三个文件,f ...

  7. XV6学习笔记(2) :内存管理

    XV6学习笔记(2) :内存管理 在学习笔记1中,完成了对于pc启动和加载的过程.目前已经可以开始在c语言代码中运行了,而当前已经开启了分页模式,不过是两个4mb的大的内存页,而没有开启小的内存页.接 ...

  8. 操作系统学习笔记4 | CPU管理 && 多进程图像

    操作系统的核心功能就是管理计算机硬件,而CPU就是计算机中最核心的硬件.而通过学习笔记3的简史回顾,操作系统通过多进程图像实现对CPU的管理.所以多进程图像是操作系统的核心图像. 参考资料: 课程:哈 ...

  9. OA学习笔记-006-SPRING2.5与hibernate3.5整合

    一.为什么要整合 1,管理SessionFactory实例(只需要一个) 2,声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二.整合步骤 1.整合sessionFac ...

随机推荐

  1. 使用Azure Redis Cache

    通过上一篇博客<Redis Cache 简介>我们已经简单了解了Azure Redis Cache,这里就不过多赘述了. 1.创建Redis Cache 创建Redis Cache之前,我 ...

  2. C++ 变量转换

    atoi,atol,strtod,strtol,strtoul实现类型转换2006-02-13 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://ivanvic.blogb ...

  3. Runtime运行时学习(一)

    其实Runtime已经开源: 下载objc4-437.1.tar.gz来看看源码: 参考: http://blog.cocoabit.com/2014-10-06-yi-li-jie-objctive ...

  4. OS X环境下SVN回滚工程到指定版本,回滚指定文件到指定版本

    1.打开命令行终端 2.cd + 工程或文件目录 3.svn update 工程目录或文件目录 -r 版本号 在Xcode中选中文件,右键选择''show in finder''(也可以用快捷键,不过 ...

  5. 国庆第七日(2014年10月7日17:55:56),随手记,一些关注的OSC软件,花生壳

    (1)最难过的是今天. (2)随手记:001.002. (3)htmlunit.joda-time.date4j.jdao.BeanGenerator.JavaScript秘密花园(开源图书)  OS ...

  6. HDU 1176 免费馅饼(数字三角形)

    免费馅饼 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉 ...

  7. Flash Professional CS6 安装zxp插件

    说明 头两天因工作原因需要使用DragonBones,他的工作方式是的Flash Professional CS5.5以上的环境. DragonBones提供的是一个文件名为:xzp的文件,在Wind ...

  8. Headfirst设计模式的C++实现——命令模式(Command)

    先看如果不用命令模式的实现: light.h #ifndef _LIGHT_H_ #define _LIGHT_H #include <iostream> class LIGHT { pu ...

  9. 实体框架(Entity Framework)简介

    实体框架(Entity Framework)简介 简称EF,与ADO.NET关系 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R ...

  10. javascript 对象的创建,引用,释放,删除方法

    1.用函数构造 A.声明时同时设置属性和方法 function func(){  this.name = "myname";  this.say = function(){aler ...