您好,登錄后才能下訂單哦!
簡單理解:
DAO數(shù)據(jù)庫訪問對(duì)象 實(shí)現(xiàn)連接數(shù)據(jù)庫 修改、添加等細(xì)節(jié)
service服務(wù)層 面向功能 把一個(gè)整個(gè)服務(wù) 細(xì)化 調(diào)用DAO
其實(shí)service其中都是一些方法 去調(diào)用DAO 甚至方法名都和DAO中一樣的
如某個(gè)service是用作用戶注冊的
其中可能包括檢測用戶名是否存在和插入用戶數(shù)據(jù)兩部分
分別調(diào)用DAO中具體實(shí)現(xiàn) 操縱數(shù)據(jù)庫
看起來邏輯更清晰而已
進(jìn)一步說明:
Dao層實(shí)現(xiàn)是簡單的CRUD操作。相當(dāng)于sql中的單條select,insert,upate,delete語句。
而service層就是具體業(yè)務(wù)的實(shí)現(xiàn)。一般多次組合dao層的方法(dao層方法達(dá)到了重用目的),是多個(gè)數(shù)據(jù)庫操作的集合,可以看做數(shù)據(jù)庫中的存儲(chǔ)過程,而且事務(wù)一般控制在service層。這些數(shù)據(jù)庫操作一起提交或回滾。
當(dāng)然,簡單的系統(tǒng)完全可以不劃分service層,只用dao層。但那這樣的話,代碼從用性就不高。
用戶的Dao層
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
public
class
UserDaoHibernate
extends
BaseDaoHibernate
implements
IUserDao {
/**
* 增加用戶
*
* @param user
*/
public
Long addUser(User user) {
return
addEntityRetVal(user);
}
/**
* 通過id刪除用戶
*
* @param user
*/
public
void
deleteUser(Long id) {
User userPO = (User) getHibernateTemplate().load(
User.
class
, id);
deleteEntity(userPO);
}
/**
* 刪除用戶
*
* @param user
*/
public
void
deleteUser(User user) {
User userPO = (User) getHibernateTemplate().load(
User.
class
, user.getUserid());
deleteEntity(userPO);
}
/**
* 更新用戶
*
* @param user
*/
public
void
updateUser(User user) {
User userPO = (User) getHibernateTemplate().load(
User.
class
, user.getUserid());
BeanUtil.copyProperties(userPO, user);
updateEntity(userPO);
}
/**
* 通過id查詢用戶
*
* @param id
* @return
*/
public
User queryUserById(Long id) {
return
(User) getHibernateTemplate().load(User.
class
, id);
}
/**
* 通過用戶名字查詢用戶實(shí)體 -- 這個(gè)方法存在SQL注入攻擊問題
* @param usernme
* @return
*/
public
User queryUserByName(String username){
String hql =
"select u from User u where u.username = '"
+ username +
"'"
;
return
(User)
this
.queryObjectByHql(hql);
}
/**
* 查詢所有用戶
*
* @return
*/
public
List<User> queryAllUser() {
return
queryAllEntitys(User.
class
);
}
/**
* 分頁查詢用戶
*/
public
List<User> queryAllUser(String hql,
int
currentPage,
int
pageSize) {
return
queryAllEntitys(currentPage,pageSize,hql);
//調(diào)用的是有currentPage的分頁方法
}
/**
*
* 通過用戶id查詢用戶名稱,查不到返回 null
* @param id
* @return
*/
public
String queryNameById(Long id){
String hql =
" from User d where d.userId = ?"
;
List<?> users = getHibernateTemplate().find(hql,id);
if
(users.size()>
){
return
((User)users.get(
)).getUsername();
}
else
{
return
null
;
}
}
/**
* 得到用戶分頁記錄總數(shù)
* @param parentId
* @return
*/
/*
public Long queryTotalNumberOfUser(){
String hql = "select count(*) from User";
List<?> childNumber = getHibernateTemplate().find(hql);
return (Long)childNumber.get(0);
}*/
public int queryAllUserNubmer(String hql){
return queryAllEntitysNumer(hql);
}
/**
* 查詢用戶的權(quán)限
* @param userId
* @return
*/
public List<UserAuth> queryFunctionOnlyByUserId(Long userId){
String hql = "select ua from UserAuth ua where ua.userid = " + userId;
List<UserAuth> userAuths = queryAllObjectByHql(hql);
return userAuths;
}
/**
* 查詢用戶的角色
* @param userId
* @return
*/
@SuppressWarnings
(
"unchecked"
)
public
List<UserRole> queryRoleOnlyByUserId(Long userId){
String hql =
"select ur from UserRole ur where ur.userid = "
+ userId;
List<UserRole> userAuths = queryAllObjectByHql(hql);
return
userAuths;
}
}
|
service層,又可細(xì)化為查詢業(yè)務(wù)UserHelper,還有增加,更新,刪除業(yè)務(wù)集中在UserFacade中。這里貼出UserHelper.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
@WebService
public
class
UserHelper
implements
IUserHelper {
private
IUserDao userDao =
null
;
private
IDepartmentDao departDao =
null
;
private
IFunctionHelper functionHelper =
null
;
private
IRoleHelper roleHelper =
null
;
public
void
setUserDao(IUserDao userDao) {
this
.userDao = userDao;
}
public
void
setFunctionHelper(IFunctionHelper functionHelper) {
this
.functionHelper = functionHelper;
}
public
void
setDepartDao(IDepartmentDao departDao) {
this
.departDao = departDao;
}
public
void
setRoleHelper(IRoleHelper roleHelper) {
this
.roleHelper = roleHelper;
}
/**
* 通過id查詢權(quán)限,沒有則返回null
*
* @param id
* @return
*/
public
UserVO queryUserById(Long id)
throws
GeneralException {
User user =
null
;
try
{
user = userDao.queryUserById(id);
}
catch
(Exception e) {
e.printStackTrace();
throw
new
GeneralException(
"error.userDeatil"
,
"通過id查詢權(quán)限時(shí)出錯(cuò)!"
);
}
// PO 轉(zhuǎn) VO
UserVO userVO = userPoToVo(user);
return
userVO;
}
/**
* 得到所有權(quán)限的集合,沒有則返回 null
*
* @return
*/
public
List<UserVO> queryAllUser()
throws
GeneralException {
List<UserVO> allFuncVOs =
new
ArrayList<UserVO>();
List<User> allFuncs =
null
;
try
{
allFuncs = userDao.queryAllUser();
}
catch
(Exception e) {
throw
new
GeneralException(
"error.userList"
,
"得到所有權(quán)限的集合時(shí)出錯(cuò)!"
);
}
for
(Iterator<?> iterator = allFuncs.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
// PO 轉(zhuǎn) VO
UserVO userVO = userPoToVo(user);
allFuncVOs.add(userVO);
}
return
allFuncVOs;
}
/**
* 權(quán)限的PO 到 VO 轉(zhuǎn)換的方法
*
* @param user
* @return
*/
public
UserVO userPoToVo(User user)
throws
GeneralException {
UserVO userVO =
new
UserVO();
BeanUtil.copyProperties(userVO, user);
try
{
userVO.setDepartName(departDao.queryNameById(user.getDepartid()));
// 設(shè)置部門名稱
}
catch
(Exception e) {
throw
new
GeneralException(
"error.user"
,
" 權(quán)限的PO 到 VO 轉(zhuǎn)換時(shí)出錯(cuò)!"
);
}
if
(userVO.getStatus().equals(
"1"
)){
userVO.setStatus(
"可用"
);
}
else
{
userVO.setStatus(
"不可用"
);
}
userVO.setRegisterName(
"ZHANG"
);
userVO.setChangerName(
"ZHANG"
);
return
userVO;
}
/**
* 通過分頁查詢權(quán)限信息集合
*
* @param hql
* @param currentPage
* @param pageSize
* @return
* @throws GeneralException
*/
public
List<UserVO> queryUserByPage(String hql,
int
currentPage,
int
pageSize)
throws
GeneralException {
List<UserVO> allFuncVOs =
new
ArrayList<UserVO>();
List<User> allFuncs =
null
;
try
{
allFuncs = userDao.queryAllUser(hql, currentPage, pageSize);
}
catch
(Exception e) {
throw
new
GeneralException(
"error.userList"
,
"分頁得到權(quán)限的集合時(shí)出錯(cuò)!"
);
}
for
(Iterator<?> iterator = allFuncs.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
// PO 轉(zhuǎn) VO
UserVO userVO = userPoToVo(user);
allFuncVOs.add(userVO);
}
return
allFuncVOs;
}
/**
* 返回User分頁對(duì)象
*
* @param currentPage
* @return
*/
public
Pagination getPagination(
int
currentPage, String hql) {
return
new
Pagination(currentPage,
DisplayRecordCount.DISPLAY_IN_USER_LIST, userDao
.queryAllUserNubmer(hql));
}
/**
* 查到用戶的所有角色I(xiàn)D
*
* @param userId
* @return
* @throws GeneralException
*/
public
List<Long> queryAllRoleidsOfUser(Long userId)
throws
GeneralException {
List<Long> rolesOfUser =
new
ArrayList<Long>();
List<UserRole> userRoles =
null
;
try
{
userRoles = userDao.queryRoleOnlyByUserId(userId);
// 查到角色權(quán)限
}
catch
(Exception e) {
throw
new
GeneralException(
"error.userRoleidsList"
,
"得到用戶的角色I(xiàn)D集合出錯(cuò)!"
);
}
for
(Iterator<?> iterator = userRoles.iterator(); iterator.hasNext();) {
UserRole userRole = (UserRole) iterator.next();
Long roleid = userRole.getRoleid();
rolesOfUser.add(roleid);
}
return
rolesOfUser;
}
/**
* 查到用戶的所有角色
*
* @param userId
* @return
* @throws GeneralException
*/
public
List<RoleVO> queryAllRoleOfUser(Long userId)
throws
GeneralException {
List<Long> rolesOfUser =
new
ArrayList<Long>();
List<RoleVO> userRoles =
new
ArrayList<RoleVO>();
try
{
rolesOfUser = queryAllRoleidsOfUser(userId);
for
(Iterator<?> iterator = rolesOfUser.iterator(); iterator
.hasNext();) {
Long roleid = (Long) iterator.next();
RoleVO roleVO = roleHelper.queryRoleById(roleid);
userRoles.add(roleVO);
}
}
catch
(Exception e) {
e.printStackTrace();
throw
new
GeneralException(
"error.userRoleList"
,
"得到用戶的角色集合出錯(cuò)!"
);
}
return
userRoles;
}
/**
* 查詢用戶的所有權(quán)限 1.查詢所有用戶的權(quán)限 2.查詢所有用戶的角色 3.查詢所有用戶的權(quán)限+角色的權(quán)限-共同的權(quán)限
*
* @param userId
* @return
*/
public
List<FunctionVO> queryAllFunctionOfUser(Long userId,String system)
throws
GeneralException {
Set<FunctionVO> userAllFuncs =
new
HashSet<FunctionVO>();
List<FunctionVO> userAllFuncsList =
new
ArrayList<FunctionVO>();
try
{
List<UserAuth> userFuncs = userDao
.queryFunctionOnlyByUserId(userId);
// 查到權(quán)限
if
(userFuncs !=
null
) {
for
(Iterator<?> iterator = userFuncs.iterator(); iterator
.hasNext();) {
UserAuth userFunc = (UserAuth) iterator.next();
Long funcId = userFunc.getFuncid();
FunctionVO funcVO = functionHelper
.queryFunctionById(funcId);
userAllFuncs.add(funcVO);
}
}
List<UserRole> userRoles = userDao.queryRoleOnlyByUserId(userId);
// 查到角色
if
(userRoles !=
null
) {
// 查到所有角色的所有權(quán)限,將權(quán)限放入到userAllFuncs中
for
(Iterator<?> iterator = userRoles.iterator(); iterator
.hasNext();) {
UserRole userRole = (UserRole) iterator.next();
Long roleId = userRole.getRoleid();
List<FunctionVO> funcVOs = roleHelper
.queryFunctionOfRole(roleId);
for
(Iterator<?> iterator2 = funcVOs.iterator(); iterator2
.hasNext();) {
FunctionVO functionVO = (FunctionVO) iterator2.next();
userAllFuncs.add(functionVO);
}
}
}
// 將篩選了數(shù)據(jù)的無序Set集合轉(zhuǎn)換為有序的List集合.一定要從小到大,否則權(quán)限樹顯示就會(huì)有問題
for
(Iterator<?> iterator = userAllFuncs.iterator(); iterator
.hasNext();) {
FunctionVO userAllFun = (FunctionVO) iterator.next();
if
(system.equals(
"crm"
)){
if
(userAllFun.getFuncid()>=20000000l){
userAllFuncsList.add(userAllFun);
}
}
else
if
(system.equals(
"hr"
)){
if
(userAllFun.getFuncid()<20000000l){
userAllFuncsList.add(userAllFun);
}
}
}
FunctionComparator fc =
new
FunctionComparator();
Collections.sort(userAllFuncsList, fc);
}
catch
(Exception e) {
e.printStackTrace();
throw
new
GeneralException(
"error.userAllFuncsList"
,
"得到用戶的權(quán)限集合出錯(cuò)!"
);
}
return
userAllFuncsList;
}
}
|
http://shenzhen.offcn.com/
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。