系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体
系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体
1,设计实体/表
设计实体 --> JavaBean --> hbm.xml --> 建表
设计Role实体
public class Role {
private Long id;
private String name;
private String description;
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;
}
}
映射文件
<hibernate-mapping package="cn.itcast.oa.domain">
<class name="Role" table="itcast_role">
<id name="id">
<generator class="native" />
</id>
<property name="name"></property>
<property name="description"></property>
</class>
</hibernate-mapping>
加到hibernate.cfg.xml配置中,让它知道有这个映射文件才能建表
<mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />
运行测试类,创建SessionFactory时就会创建itcast_role表
//测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
2,分析有几个功能,对应几个请求。

添加、修改、删除成功后 要重定向到列表功能,这样在刷新页面时才不会出现“又做一次增、删、改”的操作。
列表与删除功能都是只有一个请求
添加与修改功能都是有两个请求
增删改查共4个功能,6个请求,需要在Action中有6个对应的处理方法。
|
作用 |
方法名 |
返回值 |
对应的JSP页面 |
|
列表 |
list() |
list |
list.jsp |
|
删除 |
delete() |
toList |
|
|
添加页面 |
addUI() |
addUI |
addUI.jsp |
|
添加 |
add() |
toList |
|
|
修改页面 |
editUI() |
editUI |
editUI.jsp |
|
修改 |
edit() |
toList |
toList的配置为:type="redirectAction" actionName=“xxAction_list”
<result name="toList" type="redirectAction">role_list</result>
===================================================================
请求数量 地址栏
转发 1 不变在一个功能之间的来回跳转
重定向 2 变化在多个功能之间的跳转
role_* ---> {1}代表第一个方法
*号代表
role_list list
role_addUI addUI
role_delete delete
3,实现功能:
1,写Action类,写Action中的方法,确定Service中的方法。
先完成列表和删除功能
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport{
//在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
@Resource
private RoleService roleService; private Long id;
/**
* 列表
*/
public String list() {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);//用ognl里的#号来获取map的东西
return "list";
}
/**
* 删除
*/
public String delete() {
roleService.delete(id);
return "toList";
}
/**
* 添加页面
*/
public String addUI() {
return "addUI";
}
/**
* 添加
*/
public String add() {
return "toList";
}
/**
* 修改页面
*/
public String editUI() {
return "editUI";
}
/**
* 修改
*/
public String edit() {
return "toList";
}
//--------------
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
在struts.xml文件中配置
<!-- 岗位管理 -->
<action name="role_*" class="roleAction" method="{1}">
<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
<result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
<result name="toList" type="redirectAction">role_list</result>
</action>
2,写Service方法,确定Dao中的方法。
先完成列表和删除功能
RoleService.java
//接口中只有方法的声明,没有方法的实现
public interface RoleService {
//查询所有
List<Role> findAll();
//删除
void delete(Long id);
}
RoleServiceImpl.java
//在Action中要调用Service,要写下面两个注解
@Service
@Transactional //业务层要管理实务,控制开关事务
public class RoleServiceImpl implements RoleService{
//Service里要调用Dao,得到它通过注入
@Resource
private RoleDao roleDao; public List<Role> findAll() {
return roleDao.findAll();
}
public void delete(Long id) {
roleDao.delete(id);
}
}
3,写Dao方法。
RoleDao.java
public interface RoleDao extends BaseDao<Role>{
}
列表与删除等公共方法都在继承的BaseDao里有
RoleDaoImpl.java
//放到容器里面,以供Service使用Dao的接口与实现类
@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{
}
4,写JSP

list.jsp
<%@ taglib prefix="s" uri="/struts-tags" %><!-- 引入struts标签 -->
<body>
<s:iterator value="#roleList"><!-- 得到里面的集合 -->
<s:property value="id"/>,
<s:property value="name"/>,
<s:property value="description"/>,
<s:a action="role_delete?id=%{id}">删除</s:a>
</s:iterator>
</body>
访问:http://localhost:8080/ItcastOA/role_list.action即可看到列表
实现添加和修改功能
1,写Action类,写Action中的方法,确定Service中的方法。
RoleAction.java
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport{
//在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
@Resource
private RoleService roleService; private Long id;
private String name;
private String description;
/**
* 列表
*/
public String list() {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);//用ognl里的#号来获取map的东西
return "list";
} /**
* 删除
*/
public String delete() {
roleService.delete(id);
return "toList";
}
/**
* 添加页面
*/
public String addUI() {
return "addUI";
}
/**
* 添加
*/
public String add() {
//封装到对象中
Role role = new Role();
role.setName(name);//名称和说明怎么得到,跟id一样声明字段,setget方法
role.setDescription(description); //保存到数据库中
roleService.save(role);
return "toList";
}
/**
* 修改页面
*/
public String editUI() {
//准备回显的数据
Role role =roleService.getById(id);
//ActionContext.getContext().getValueStack().push(role);//放到栈顶
this.name=role.getName();
this.description =role.getDescription();
return "editUI";
}
/**
* 修改
*/
public String edit() {
//1.从数据库中获取原对象
Role role = roleService.getById(id);//role是根据id来的 //2.设置要修改的属性
role.setName(name);
role.setDescription(description);
//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方法,确定Dao中的方法。
RoleService.java
//接口中只有方法的声明,没有方法的实现
public interface RoleService {
//查询所有
List<Role> findAll();
//删除
void delete(Long id);
//保存
void save(Role role);
Role getById(Long id);
//更新
void update(Role role);
}
3,写Dao方法。
RoleDao.java
public interface RoleDao extends BaseDao<Role>{
}
4,写JSP
addUI.jsp
<body>
<s:form action="role_add"><!-- 提交的地址 -->
<s:textfield name="name"></s:textfield>
<s:textarea name="description"></s:textarea>
<s:submit value="提交"></s:submit>
</s:form>
</body>
editUI.jsp
<s:form action="role_edit"><!-- 提交的地址 -->
<s:hidden name="id"></s:hidden><!-- 修改要给出隐藏的id -->
<s:textfield name="name"></s:textfield>
<s:textarea name="description"></s:textarea>
<s:submit value="提交"></s:submit>
</s:form>
访问:http://localhost:8080/ItcastOA/role_list.action验证即可


系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体的更多相关文章
- 系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面
改进_使用ModelDroven方案 @Controller @Scope("prototype") public class RoleAction extends ActionS ...
- 系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层
系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="j ...
- 用户管理_组管理_权限管理.ziw
2017年1月10日, 星期二 用户管理_组管理_权限管理 用户管理: useradd, userdel, usermod, passwd, chsh, chfn, finger, id, chage ...
- 论坛模块_版块管理_增删改查&实现上下移动
论坛模块_版块管理1_增删改查 设计实体Forum.java public class Forum { private Long id; private String name; private St ...
- 用户管理_组管理_设置主机名_UGO_文件高级权限_ACL权限
用户管理: 添加用户:useradd tom 设置密码:passwd tom 切换账户: su - tom (不加-也能切换,但是 -会有两点不同 1.有-会切换到该用户的主目录 2.会切换到该用户 ...
- react_app 项目开发 (8)_角色管理_用户管理----权限管理 ---- shouldComponentUpdate
角色管理 性能优化(前端面试) 需求:只要执行 setState(), 就会调用 render 重新渲染.由于有时调用了 setState,但是并没有发生状态的改变,以致于不必要的刷新 解决: 重写 ...
- 操作系统(5)_内存管理_李善平ppt
i386先通过段是管理,在通过页是管理
- 操作系统(2)_进程管理_李善平ppt
所有程序都有CPU和io这两部分,即使没有用户输入也有输出. CPU最好特别忙,io空闲无所谓. 程序/数据/状态 三个维度来看进程. 等待的资源可能是io资源或者通信资源(别的进程的答复). 一个进 ...
- 二、linux基础-路径和目录_用户管理_组_权限
2.1路径和目录1.相对路径:参照当前目录进行查找. 如:[root@localhost ~]# cd ../opt/hosts/备注:相对路径是从你的当前目录开始为基点,去寻找另外一个目录(或者 ...
随机推荐
- 一对一关系数据库表 java类描述
一对一关系中 从表的主键是 主表的外键 sql语句 create table person( id int primary key, name varchar(100) ); create table ...
- Linux-软件包管理-yum在线管理-光盘yum源
mount /dev/cdrom /mnt/cdrom 将设备名/dev/cdrom安装到mnt/cdrom挂载点下面mount 查看当前所有挂载信息 cd /etc/yum.repos.d 切换到e ...
- CMD查看进程ID并查杀进程
开始-运行,输入CMD打开命令行界面,输入命令netstat -ano 结束该进程C:\>taskkill /f /t /im Wiz.exe 根据进程ID杀 >taskkill /F / ...
- GraphicsMagick 学习笔记
两种最常用的图片处理工具:GraphicsMagick 或 ImageMagick,GM是IM的分支,这两个图片处理工具功能基本相同,各有特色.但他们并不是nodejs的插件,它们都是客户端软件,li ...
- mysql date and time type ---- mysql 时间&日期 类型详解
mysql 中支持用多种方式来表示时间与日期 一.mysql 中能表示时间与日期的数据类型: 1.表示年 ) -- 最好不要用这个数据类型.对于年份的取值中有[1901 --> 2155] + ...
- atitit.提升兼容性最佳实践 o9o
atitit.提升兼容性最佳实践 o9o.doc 1. Atitit.兼容性的"一加三"策略 1 2. 扩展表模式 1 3. 同时运行模式 1 3.1. 完美的后向兼容性 2 3. ...
- [转]html5调用摄像头实例
原文:https://blog.csdn.net/binquan_liang/article/details/79489989 最近在学习在做HTML5的项目,看了博客上html5调用摄像头拍照的文章 ...
- 把一张图片 转成二进制流 用AFNetworking POST 上传到服务器.
把一张图片 转成二进制流 用AFNetworking POST 上传到服务器. AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOper ...
- Nginx - Windows下Nginx初入门,附CentOS下Nginx的安装
公司刚使用nginx,预先学习下.鉴于机器没有Linux环境,在Windows熟悉下. 下载 目前(2015-07-11),nginx的稳定版本是1.8.0,在官网下载先,windows版的nginx ...
- Hystrix的原理与使用
转载自:https://segmentfault.com/a/1190000005988895 http://blog.csdn.net/xiaoyu411502/artic ...