用户模块新增修改删除
部门删除涉及操作细节
执行操作时执行的hql语句
1
2
3
4
5
6
7
8
9
10//查询所有部门信息
Collection<Department> departmentList = this.departmentService.getAllDepartment();//1
//删除部门信息
Department department = this.departmentService.getDepartmentById(this.getModel().getDid());//2
//提取该部门的用户
Set<User> users = department.getUsers();//3
for(User user:users){
user.setDepartment(null);//解除user和department之间的关系 //4
}
this.departmentService.deleteDepartment(this.getModel().getDid()); //51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65Hibernate: //1
select
department0_.did as did1_,
department0_.name as name1_,
department0_.description as descript3_1_
from
Department department0_
Hibernate: //2
select
department0_.did as did1_,
department0_.name as name1_,
department0_.description as descript3_1_
from
Department department0_
where
department0_.did=?
Hibernate: //3
select
users0_.did as did1_1_,
users0_.uid as uid1_,
users0_.uid as uid4_0_,
users0_.username as username4_0_,
users0_.password as password4_0_,
users0_.email as email4_0_,
users0_.phone as phone4_0_,
users0_.sex as sex4_0_,
users0_.did as did4_0_
from
User users0_
where
users0_.did=?
Hibernate: //5
select
department0_.did as did1_,
department0_.name as name1_,
department0_.description as descript3_1_
from
Department department0_
where
department0_.did=?
Hibernate: // 4
update
User
set
username=?,
password=?,
email=?,
phone=?,
sex=?,
did=?
where
uid=?
Hibernate: //5
delete
from
Department
where
did=?
Hibernate: //1
select
department0_.did as did1_,
department0_.name as name1_,
department0_.description as descript3_1_
from
Department department0_session.flush
检查hibernate一级缓存中所有的持久化状态的对象
把对象和快照进行对比,如果没有发生变化,则什么都不做
如果发生变化,则发出update语句
可以发生save的操作
检查关联对象
看是否维护关系
看是否有级联操作
一级缓存
1
2
3
4
5Person p = new Person();
p.setName("aa");
Person p1 = new Person();
p1.setName("bbbb");
session.save(p1);当事务提交的时候,p1在session的一级缓存中,p不在
所以当事务提交的时候,只会保存p1,和p没有关系
关于session的产生方式
sessionFactory.opensession
只有调用该api就会创建一个新的session
sessionFactory.getCurrentSession
第一次当执行getCurrentSession的时候,会判断当前线程中是否有session,如果有,则从当前线程中把session提取出来,如果没有则调用sessionFactory.openSession方法创建一个session,把创建后的session放入threadlocal中
这样可以保证在一个线程中始终只用一个session
spring容器启动事务用的session从threadlocal中取出,程序员进行crud操作的session也从threadlocal中取出,
所以程序员操作的session和spring容器操作的session是同一个session
ormapping框架
是一个面向对象的数据库的操作框架,其底层是用jdbc实现的
如何把一个面向对象的操作变成sql语句
任何一个ormapping框架都应该有缓存
缓存有私有数据和共有数据
该框架是怎么样保证数据的私有性
该框架是怎样把数据进行公开化的
怎么样保证缓存和数据库的同步
怎样对事务进行处理
怎样去提高数据库的访问效率
用户模块
跳转到用户的添加页面
UserAction
1
2
3
4
5
6
7
8
9
10
11public String addUI() {
/*
* 把部门的信息和岗位的信息提取出来
* 加入两个service
*/
Collection<Department> dList = this.departmentService.getAllDepartments();
Collection<Role> rList = this.roleService.getAllRole();
ActionContext.getContext().put("dList", dList);
ActionContext.getContext().put("rList", rList);
return addUI;
}add.jsp-WEB-INF/jsp/system/user/add.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp"%>
<html>
<head>
<title>用户信息</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/css/images/title_arrow.gif"/> 用户信息
</div>
<div id="Title_End"></div>
</div>
</div>
<!--显示表单内容-->
<div id=MainArea>
<s:form action="userAction_add.action">
<div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 用户信息 </div>
</div>
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr><td width="100">所属部门</td>
<!--
list属性的值指向要迭代的集合的位置
name:后台利用该属性的值获取选中的下拉列表框的值
listKey代表option中的value
listValue代表显示出来的值
-->
<td>
<s:select list="#dList" listKey="did" listValue="name" name="did" cssClass="SelectStyle"></s:select>
</td>
</tr>
<tr><td>登录名</td>
<td>
<s:textfield name="username" cssClass="InputStyle"></s:textfield>
</td>
</tr>
<tr><td>性别</td>
<td>
<s:radio name="sex" list="{'男','女'}"/>
</td>
</tr>
<tr><td>联系电话</td>
<td><s:textfield name="phone" cssClass="InputStyle"></s:textfield></td>
</tr>
<tr><td>E-mail</td>
<td><s:textfield name="email" cssClass="InputStyle"></s:textfield></td>
</tr>
</table>
</div>
</div>
<div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 岗位设置 </div>
</div>
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位</td>
<td><s:select list="#rList" listKey="rid" listValue="name" name="rids" multiple="true" size="10" cssClass="SelectStyle"></s:select>
</td>
</tr>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}/css/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/css/images/goBack.png"/></a>
</div>
</s:form>
</div>
<div class="Description">
说明:<br />
1,用户的登录名要唯一,在填写时要同时检测是否可用。<br />
2,新建用户后,密码被初始化为"1234"。<br />
3,密码在数据库中存储的是MD5摘要(不是存储明文密码)。<br />
4,用户登录系统后可以使用“个人设置→修改密码”功能修改密码。<br />
5,新建用户后,会自动指定默认的头像。用户可以使用“个人设置→个人信息”功能修改自已的头像<br />
6,修改用户信息时,登录名不可修改。
</div>
</body>
</html>
用户的增加
BaseDao
1
2
3
4/*
* 根据一个或者一个以上的id,返回一个Set<E>
*/
public Set<E> getEntrysByIDS(Serializable[] ids);BaseDaoImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108package cn.itcast.shoa.dao.base.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.hibernate.metadata.ClassMetadata;
import org.springframework.orm.hibernate3.HibernateTemplate;
import cn.itcast.shoa.dao.base.BaseDao;
public class BaseDaoImpl<E> implements BaseDao<E>{
private Class classt;
/*
* 获取到E代表的持久化类的数据字典-->当前实体bean的标识符名称
*/
private ClassMetadata classMetadata;
public BaseDaoImpl() {
/**
* 1.this可以代表子类或本类
* 2.不能把BaseDaoImpl在spring容器中实例化,因为如果在Spring容器中实例化,就不是泛型了
* 3.所以根据以上两点可以得出this应该代表子类
* 4.this.getClass().getGenericSuperclass()代表当前类:就是泛型
* 5.注意:不能把BaseDaoImpl让spring容器实例化
*/
// TODO Auto-generated constructor stub
ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
//因为将来E代表的是某一个持久化类,而该类型是class
this.classt = (Class)type.getActualTypeArguments()[0];
}
/*
* 当调用完构造方法后,立即执行该init方法
*/
private void init() {
this.classMetadata= this.hibernatetemplate.getSessionFactory().getClassMetadata(this.classt);
}
public HibernateTemplate hibernatetemplate;
public Collection<E> getAllEntry() {
// TODO Auto-generated method stub
return this.hibernatetemplate.find( "from "+ this.classt.getName() );
}
public E getEntryById(Serializable id) {
// TODO Auto-generated method stub
/**
* classMetadata是持久化类的数据字典
*/
return (E)this.hibernatetemplate.
find("from "+this.classt.getName()
+
" where "
+classMetadata.getIdentifierPropertyName()
+"=?",
id).get(0);
}
public void saveEntry(E e) {
// TODO Auto-generated method stub
this.hibernatetemplate.save(e);
}
public void deleteEntry(Serializable id) {
// TODO Auto-generated method stub
E e = this.getEntryById(id);
this.hibernatetemplate.delete(e);
}
public void updateEntry(E e) {
// TODO Auto-generated method stub
this.hibernatetemplate.update(e);
}
public Set<E> getEntrysByIDS(Serializable[] ids) {
/**
* 1、如果数组只有一个元素
* 2、如果数组中有两个或者两个以上的元素
* from Person where pid in(1,2,3,4)
*/
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("from "+this.classt.getName());//this.classt.getName()持久化类的名字
stringBuffer.append(" where "+this.classMetadata.getIdentifierPropertyName());
stringBuffer.append(" in (");
for(int i=0;i<ids.length;i++){
if(i==ids.length-1){
stringBuffer.append(ids[i]);
}else{
stringBuffer.append(ids[i]+",");
}
}
stringBuffer.append(")");
List<E> list = this.hibernatetemplate.find(stringBuffer.toString());
return new HashSet<E>(list);
}
}UserService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package cn.itcast.shoa.service;
import java.io.Serializable;
import java.util.Collection;
import cn.itcast.shoa.domain.system.User;
public interface UserService {
/*
* 查找所有用户
*/
public Collection<User> getAllUser();
/*
* 查找单个用户
*/
public User getUserById(Serializable id);
/*
* 保存用户
*/
public void saveUser(Long did,Long[] rids,User user);
/*
* 删除用户
*/
public void deleteUser(Serializable id);
/*
* 修改用户
*/
public void updateUser(User user);
}UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66package cn.itcast.shoa.service.impl;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.annotation.Resource;
import org.hibernate.annotations.Source;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.shoa.dao.DepartmentDao;
import cn.itcast.shoa.dao.RoleDao;
import cn.itcast.shoa.dao.UserDao;
import cn.itcast.shoa.domain.system.Department;
import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.domain.system.User;
import cn.itcast.shoa.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
private DepartmentDao departmetDao;
private RoleDao roleDao;
public Collection<User> getAllUser() {
// TODO Auto-generated method stub
return this.userDao.getAllEntry();
}
public User getUserById(Serializable id) {
// TODO Auto-generated method stub
return this.userDao.getEntryById(id);
}
public void saveUser(Long did,Long[] rids,User user) {
/**
* 1、建立用户和部门之间的关系
* 2、建立用户和岗位之间的关系
* 用户来维护用户和部门之间的关系,用户维护用户和岗位之间的关系
*/
Department department = this.departmetDao.getEntryById(did);
//建立用户和部门之间的关系
user.setDepartment(department);
Set<Role> roles = this.roleDao.getEntrysByIDS(rids);
//建立用户和岗位之间的关系
user.setRoles(roles);
this.userDao.saveEntry(user);
}
public void deleteUser(Serializable id) {
// TODO Auto-generated method stub
this.userDao.deleteEntry(id);
}
}UserAction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94package cn.itcast.shoa.struts.action;
import java.util.Collection;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.itcast.shoa.domain.system.Department;
import cn.itcast.shoa.domain.system.Role;
import cn.itcast.shoa.domain.system.User;
import cn.itcast.shoa.service.DepartmentService;
import cn.itcast.shoa.service.RoleService;
import cn.itcast.shoa.service.UserService;
import cn.itcast.shoa.struts.action.base.BaseAction;
public class UserAction extends BaseAction<User>{
private UserService userService;
private RoleService roleService;
private Long did;//部门id
private Long[] rids;//被选中的岗位的id
public Long getDid() {
return did;
}
public void setDid(Long did) {
this.did = did;
}
public Long[] getRids() {
return rids;
}
public void setRids(Long[] rids) {
this.rids = rids;
}
private DepartmentService departmentService ;
public String showAllUser() {
Collection<User> userList = this.userService.getAllUser();
ActionContext.getContext().put("userList", userList);
return listAction;
}
public String addUI() {
/*
* 把部门的信息和岗位的信息提取出来
* 加入两个service
*/
Collection<Department> dList = this.departmentService.getAllDepartments();
Collection<Role> rList = this.roleService.getAllRole();
ActionContext.getContext().put("dList", dList);
ActionContext.getContext().put("rList", rList);
return addUI;
}
public String add(){
/**
* 1、获取用户的一般属性
* 利用模型驱动可以获取
* 2、获取页面上选中的部门ID的值
* 3、获取岗位的IDS的值(该值有可能有多个)
* 4、在保存用户的时候,建立用户和部门之间的关系
* 5、在保存用户的时候,建立用户和岗位之间的关系
*/
User user = new User();
//user获取一般的数据
BeanUtils.copyProperties(this.getModel(), user);
this.userService.saveUser(did, rids, user);
return action2action;
}
public String deleteUser() {
this.userService.deleteUser(this.getModel().getUid());
return action2action;
}
}
跳转到用户修改页面
UserAction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31public String updateUI() {
/*
* 1. 用户的一般属性回显
* 2. 部门信息的回显
* 3. 岗位信息的回显
* 4. 把部门信息和岗位信息全部提取出来
*/
User user = this.userService.getUserById(this.getModel().getUid());
ActionContext.getContext().getValueStack().push(user);
/*
* 对应部门和岗位的回显,应是部门did和岗位rids的赋值
*/
this.did = user.getDepartment().getDid();
Set<Role> roles = user.getRoles();
//对rids进行创建对象及初始化操作
this.rids = new Long[roles.size()];
int index = 0;
for (Role role : roles) {
this.rids[index] = role.getRid();
index++;
}
//list集合必须放入值栈中才能用ognl表达式获取数据 两种做法:对象栈与map栈
Collection<Department> departmentsList = this.departmentService.getAllDepartments();
//list集合必须放入值栈中才能用ognl表达式获取数据 两种做法:对象栈与map栈
Collection<Role> roleList = this.roleService.getAllRole();
//放入map
ActionContext.getContext().put("dList", departmentsList);
//放入map
ActionContext.getContext().put("rList", roleList);
return updateUI;
}list.jsp
1
2
3<s:a action="userAction_deleteUser.action?uid=%{uid}">删除</s:a>
<s:a action="userAction_updateUI.action?uid=%{uid}">修改</s:a>
<a href="javascript:privilegeclick();">设置权限</a>update.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp"%>
<html>
<head>
<title>用户信息</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/css/images/title_arrow.gif"/> 用户信息
</div>
<div id="Title_End"></div>
</div>
</div>
<!--显示表单内容-->
<div id=MainArea>
<s:form action="userAction_updateUser.action">
<s:hidden name="uid"></s:hidden>
<div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 用户信息 </div>
</div>
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr><td width="100">所属部门</td>
<!--
list属性的值指向要迭代的集合的位置
name:后台利用该属性的值获取选中的下拉列表框的值
listKey代表option中的value
listValue代表显示出来的值
-->
<td>
<s:select list="#dList" listKey="did" listValue="name" name="did" cssClass="SelectStyle"></s:select>
</td>
</tr>
<tr><td>登录名</td>
<td>
<s:textfield name="username" cssClass="InputStyle"></s:textfield>
</td>
</tr>
<tr><td>性别</td>
<td>
<s:radio name="sex" list="{'男','女'}"/>
</td>
</tr>
<tr><td>联系电话</td>
<td><s:textfield name="phone" cssClass="InputStyle"></s:textfield></td>
</tr>
<tr><td>E-mail</td>
<td><s:textfield name="email" cssClass="InputStyle"></s:textfield></td>
</tr>
</table>
</div>
</div>
<div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 岗位设置 </div>
</div>
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位</td>
<td><s:select list="#rList" listKey="rid" listValue="name" name="rids" multiple="true" size="10" cssClass="SelectStyle"></s:select>
</td>
</tr>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}/css/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/css/images/goBack.png"/></a>
</div>
</s:form>
</div>
<div class="Description">
说明:<br />
1,用户的登录名要唯一,在填写时要同时检测是否可用。<br />
2,新建用户后,密码被初始化为"1234"。<br />
3,密码在数据库中存储的是MD5摘要(不是存储明文密码)。<br />
4,用户登录系统后可以使用“个人设置→修改密码”功能修改密码。<br />
5,新建用户后,会自动指定默认的头像。用户可以使用“个人设置→个人信息”功能修改自已的头像<br />
6,修改用户信息时,登录名不可修改。
</div>
</body>
</html>
用户的修改
UserService
1
2
3
4/*
* 修改用户
*/
public void updateUser(Long did,Long[] rids,User user);UserServiceImpl
1
2
3
4
5
6
7
8
9
10
public void updateUser(Long did,Long[] rids,User user) {
Department department = this.departmetDao.getEntryById(did);
//重新建立用户和部门之间的关系
user.setDepartment(department);
Set<Role> roles = this.roleDao.getEntrysByIDS(rids);
//重新建立用户和岗位之间的关系
user.setRoles(roles);
this.userDao.updateEntry(user);
}UserAction
1
2
3
4
5
6
7
8
9
10
11public String updateUser() {
/*
* 1.修改用户的一般属性
* 2.重新建立用户和部门之间的关系
* 3.重新建立用户和岗位之间的关系
*/
User user = this.userService.getUserById(this.getModel().getUid());
BeanUtils.copyProperties(this.getModel(), user);
this.userService.updateUser(did,rids,user);
return action2action;
}