13.4.2 实现业务逻辑组件

13.4.2 实现业务逻辑组件

业务逻辑组件负责实现系统所需的业务方法,系统有多少个业务方法,业务逻辑组件就提供多少个对应方法,业务逻辑方法完全由业务逻辑组件负责实现。
业务逻辑组件只负责业务逻辑上的变化,而持久层上的变化则交给DAO层负责,因此业务逻辑组件必须依赖于DAO组件。
为了简化分页功能,设计了一个分页的**JSP标签**,只需要在页面使用分页标签,就可以完成所有页面的分页功能。下面是分页标签的源代码:

PageModel.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
package org.fkit.hrm.util.tag;
import org.fkit.hrm.util.common.HrmConstants;
/**
* 分页实体
*/
public class PageModel{
/** 分页总数据条数 */
private int recordCount;
/** 当前页面 */
private int pageIndex;
/** 每页分多少条数据 */
private int pageSize = HrmConstants.PAGE_DEFAULT_SIZE = 4;
/** 总页数 */
private int totalSize;
public int getRecordCount()
{
this.recordCount = this.recordCount <= 0 ? 0 : this.recordCount;
return recordCount;
}
public void setRecordCount(int recordCount)
{
this.recordCount = recordCount;
}
public int getPageIndex()
{
this.pageIndex = this.pageIndex <= 0 ? 1 : this.pageIndex;
/** 判断当前页面是否超过了总页数:如果超过了默认给最后一页作为当前页 */
this.pageIndex = this.pageIndex >= this.getTotalSize()
? this.getTotalSize()
: this.pageIndex;
return pageIndex;
}
public void setPageIndex(int pageIndex)
{
this.pageIndex = pageIndex;
}
public int getPageSize()
{
this.pageSize = this.pageSize <= HrmConstants.PAGE_DEFAULT_SIZE
? HrmConstants.PAGE_DEFAULT_SIZE
: this.pageSize;
return pageSize;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
public int getTotalSize()
{
if(this.getRecordCount() <= 0)
{
totalSize = 0;
} else
{
totalSize = (this.getRecordCount() - 1) / this.getPageSize() + 1;
}
return totalSize;
}
public int getFirstLimitParam()
{
return (this.getPageIndex() - 1) * this.getPageSize();
}
}

PagerTag.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
package org.fkit.hrm.util.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* 分页标签
*/
public class PagerTag
extends SimpleTagSupport
{
/** 定义请求URL中的占位符常量 */
private static final String TAG = "{0}";
/** 当前页码 */
private int pageIndex;
/** 每页显示的数量 */
private int pageSize;
/** 总记录条数 */
private int recordCount;
/** 请求URL page.action?pageIndex={0} */
private String submitUrl;
/** 样式 */
private String style = "sabrosus";
/** 定义总页数 */
private int totalPage = 0;
/** 在页面上引用自定义标签就会触发一个标签处理类 */
@Override
public void doTag() throws JspException,IOException
{
/** 定义它拼接是终的结果 */
StringBuilder res = new StringBuilder();
/** 定义它拼接中间的页码 */
StringBuilder str = new StringBuilder();
/** 判断总记录条数 */
if(recordCount > 0)
{ // 1499 / 15 = 100
/** 需要显示分页标签,计算出总页数 需要分多少页 */
totalPage = (this.recordCount - 1) / this.pageSize + 1;
/** 判断上一页或下一页需不需要加a标签 */
if(this.pageIndex == 1)
{ // 首页
str.append("<span class='disabled'>上一页</span>");
/** 计算中间的页码 */
this.calcPage(str);
/** 下一页需不需要a标签 */
if(this.pageIndex == totalPage)
{
/** 只有一页 */
str.append("<span class='disabled'>下一页</span>");
} else
{
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
str.append("<a href='" + tempUrl + "'>下一页</a>");
}
} else if(this.pageIndex == totalPage)
{ // 尾页
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
str.append("<a href='" + tempUrl + "'>上一页</a>");
/** 计算中间的页码 */
this.calcPage(str);
str.append("<span class='disabled'>下一页</span>");
} else
{ // 中间
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
str.append("<a href='" + tempUrl + "'>上一页</a>");
/** 计算中间的页码 */
this.calcPage(str);
tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
str.append("<a href='" + tempUrl + "'>下一页</a>");
}
/** 拼接其它的信息 */
res.append("<table width='100%' align='center' style='font-size:13px;' class='" + style
+ "'>");
res.append(
"<tr><td style='COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; TEXT-DECORATION: none'>"
+ str.toString());
res.append(
"&nbsp;跳转到&nbsp;&nbsp;<input style='text-align: center;BORDER-RIGHT: #aaaadd 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #aaaadd 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #aaaadd 1px solid; COLOR: #000099; PADDING-TOP: 2px; BORDER-BOTTOM: #aaaadd 1px solid; TEXT-DECORATION: none' type='text' size='2' id='pager_jump_page_size'/>");
res.append(
"&nbsp;<input type='button' style='text-align: center;BORDER-RIGHT: #dedfde 1px solid; PADDING-RIGHT: 6px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #dedfde 1px solid; PADDING-LEFT: 6px; PADDING-BOTTOM: 2px; BORDER-LEFT: #dedfde 1px solid; COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; BORDER-BOTTOM: #dedfde 1px solid; TEXT-DECORATION: none' value='确定' id='pager_jump_btn'/>");
res.append("</td></tr>");
res.append(
"<tr align='center'><td style='font-size:13px;'><tr><td style='COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; TEXT-DECORATION: none'>");
/** 开始条数 */
int startNum = (this.pageIndex - 1) * this.pageSize + 1;
/** 结束条数 */
int endNum = (this.pageIndex == this.totalPage)
? this.recordCount
: this.pageIndex * this.pageSize;
res.append("总共<font color='red'>" + this.recordCount + "</font>条记录,当前显示" + startNum
+ "-" + endNum + "条记录。");
res.append("</td></tr>");
res.append("</table>");
res.append("<script type='text/javascript'>");
res.append(" document.getElementById('pager_jump_btn').onclick = function(){");
res.append(
" var page_size = document.getElementById('pager_jump_page_size').value;");
res.append(" if (!/^[1-9]\\d*$/.test(page_size) || page_size < 1 || page_size > "
+ this.totalPage + "){");
res.append(" alert('请输入[1-" + this.totalPage + "]之间的页码!');");
res.append(" }else{");
res.append(" var submit_url = '" + this.submitUrl + "';");
res.append(" window.location = submit_url.replace('" + TAG + "', page_size);");
res.append(" }");
res.append("}");
res.append("</script>");
} else
{
res.append(
"<table align='center' style='font-size:13px;'><tr><td style='COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; TEXT-DECORATION: none'>总共<font color='red'>0</font>条记录,当前显示0-0条记录。</td></tr></table>");
}
this.getJspContext().getOut().print(res.toString());
}
/** 计算中间页码的方法 */
private void calcPage(StringBuilder str)
{
/** 判断总页数 */
if(this.totalPage <= 11)
{
/** 一次性显示全部的页码 */
for(int i = 1;i <= this.totalPage;i++)
{
if(this.pageIndex == i)
{
/** 当前页码 */
str.append("<span class='current'>" + i + "</span>");
} else
{
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<a href='" + tempUrl + "'>" + i + "</a>");
}
}
} else
{
/** 靠近首页 */
if(this.pageIndex <= 8)
{
for(int i = 1;i <= 10;i++)
{
if(this.pageIndex == i)
{
/** 当前页码 */
str.append("<span class='current'>" + i + "</span>");
} else
{
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<a href='" + tempUrl + "'>" + i + "</a>");
}
}
str.append("...");
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage));
str.append("<a href='" + tempUrl + "'>" + this.totalPage + "</a>");
}
/** 靠近尾页 */
else if(this.pageIndex + 8 >= this.totalPage)
{
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1));
str.append("<a href='" + tempUrl + "'>1</a>");
str.append("...");
for(int i = this.totalPage - 10;i <= this.totalPage;i++)
{
if(this.pageIndex == i)
{
/** 当前页码 */
str.append("<span class='current'>" + i + "</span>");
} else
{
tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<a href='" + tempUrl + "'>" + i + "</a>");
}
}
}
/** 在中间 */
else
{
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1));
str.append("<a href='" + tempUrl + "'>1</a>");
str.append("...");
for(int i = this.pageIndex - 4;i <= this.pageIndex + 4;i++)
{
if(this.pageIndex == i)
{
/** 当前页码 */
str.append("<span class='current'>" + i + "</span>");
} else
{
tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<a href='" + tempUrl + "'>" + i + "</a>");
}
}
str.append("...");
tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage));
str.append("<a href='" + tempUrl + "'>" + this.totalPage + "</a>");
}
}
}
/** setter 方法 */
public void setPageIndex(int pageIndex)
{
this.pageIndex = pageIndex;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
public void setRecordCount(int recordCount)
{
this.recordCount = recordCount;
}
public void setSubmitUrl(String submitUrl)
{
this.submitUrl = submitUrl;
}
public void setStyle(String style)
{
this.style = style;
}
}

要使用JSP的标签还需要在WEB-INF下增加一个tld标签文件。

page.tld

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
<?xml version="1.0" encoding="utf-8"?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 描述 自定义标签版本的一种描述 -->
<description>Pager 1.0 core library</description>
<!-- 显示的名称 导包进行的一个展示 -->
<display-name>Pager core</display-name>
<!-- 版本号 -->
<tlib-version>1.0</tlib-version>
<!-- 短名 -->
<short-name>fkjava</short-name>
<!-- uri :导包 -->
<uri>/pager-tags</uri>
<!-- 定义一个标签 -->
<tag>
<!-- 标签名 -->
<name>pager</name>
<!-- 标签处理类 -->
<tag-class>org.fkit.hrm.util.tag.PagerTag</tag-class>
<!-- 设置标签为空 -->
<body-content>empty</body-content>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 表示分页的第几页 -->
<name>pageIndex</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 表示分页标签 ,每页显示多少条数据 -->
<name>pageSize</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 记录分页的总数 -->
<name>recordCount</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 -->
<name>submitUrl</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 -->
<name>style</name>
<!-- 必须的 -->
<required>false</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

HrmService.java

下面是HrmService接口的源代码。

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
package org.fkit.hrm.service;

import java.util.List;
import org.fkit.hrm.domain.Dept;
import org.fkit.hrm.domain.Document;
import org.fkit.hrm.domain.Employee;
import org.fkit.hrm.domain.Job;
import org.fkit.hrm.domain.Notice;
import org.fkit.hrm.domain.User;
import org.fkit.hrm.util.tag.PageModel;

/**
* 人事管理服务层接口
*/
public interface HrmService {

/**
* 用户登录
* @param loginname
* @param password
* @return User对象
* */
User login(String loginname,String password);

/**
* 根据id查询用户
* @param id
* @return 用户对象
* */
User findUserById(Integer id);

/**
* 获得所有用户
* @return User对象的List集合
* */
List<User> findUser(User user,PageModel pageModel);

/**
* 根据id删除用户
* @param id
* */
void removeUserById(Integer id);

/**
* 修改用户
* @param User 用户对象
* */
void modifyUser(User user);

/**
* 添加用户
* @param User 用户对象
* */
void addUser(User user);

/**
* 获得所有员工
* @param employee 查询条件
* @param pageModel 分页对象
* @return Dept对象的List集合
* */
List<Employee> findEmployee(Employee employee,PageModel pageModel);

/**
* 根据id删除员工
* @param id
* */
void removeEmployeeById(Integer id);

/**
* 根据id查询员工
* @param id
* @return 员工对象
* */
Employee findEmployeeById(Integer id);

/**
* 添加员工
* @param employee 员工对象
* */
void addEmployee(Employee employee);

/**
* 修改员工
* @param employee 员工对象
* */
void modifyEmployee(Employee employee);

/**
* 获得所有部门,分页查询
* @return Dept对象的List集合
* */
List<Dept> findDept(Dept dept,PageModel pageModel);

/**
* 获得所有部门
* @return Dept对象的List集合
* */
List<Dept> findAllDept();

/**
* 根据id删除部门
* @param id
* */
public void removeDeptById(Integer id);

/**
* 添加部门
* @param dept 部门对象
* */
void addDept(Dept dept);

/**
* 根据id查询部门
* @param id
* @return 部门对象
* */
Dept findDeptById(Integer id);

/**
* 修改部门
* @param dept 部门对象
* */
void modifyDept(Dept dept);

/**
* 获得所有职位
* @return Job对象的List集合
* */
List<Job> findAllJob();

List<Job> findJob(Job job,PageModel pageModel);

/**
* 根据id删除职位
* @param id
* */
public void removeJobById(Integer id);

/**
* 添加职位
* @param Job 部门对象
* */
void addJob(Job job);

/**
* 根据id查询职位
* @param id
* @return 职位对象
* */
Job findJobById(Integer id);

/**
* 修改职位
* @param dept 部门对象
* */
void modifyJob(Job job);


/**
* 获得所有公告
* @return Notice对象的List集合
* */
List<Notice> findNotice(Notice notice,PageModel pageModel);

/**
* 根据id查询公告
* @param id
* @return 公告对象
* */
Notice findNoticeById(Integer id);

/**
* 根据id删除公告
* @param id
* */
public void removeNoticeById(Integer id);

/**
* 添加公告
* @param Notice 公告对象
* */
void addNotice(Notice notice);

/**
* 修改公告
* @param Notice 公告对象
* */
void modifyNotice(Notice notice);

/**
* 获得所有文档
* @return Document对象的List集合
* */
List<Document> findDocument(Document document,PageModel pageModel);

/**
* 添加文档
* @param Document 文件对象
* */
void addDocument(Document document);

/**
* 根据id查询文档
* @param id
* @return 文档对象
* */
Document findDocumentById(Integer id);

/**
* 根据id删除文档
* @param id
* */
public void removeDocumentById(Integer id);

/**
* 修改文档
* @param Document 公告对象
* */
void modifyDocument(Document document);
}

HrmService接口中是本系统所有业务逻辑方法的定义,下面是这些业务逻辑方法的实现:

HrmServiceImpl.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package org.fkit.hrm.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fkit.hrm.dao.DeptDao;
import org.fkit.hrm.dao.DocumentDao;
import org.fkit.hrm.dao.EmployeeDao;
import org.fkit.hrm.dao.JobDao;
import org.fkit.hrm.dao.NoticeDao;
import org.fkit.hrm.dao.UserDao;
import org.fkit.hrm.domain.Dept;
import org.fkit.hrm.domain.Document;
import org.fkit.hrm.domain.Employee;
import org.fkit.hrm.domain.Job;
import org.fkit.hrm.domain.Notice;
import org.fkit.hrm.domain.User;
import org.fkit.hrm.service.HrmService;
import org.fkit.hrm.util.tag.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* 人事管理系统服务层接口实现类
*/
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
@Service("hrmService")
public class HrmServiceImpl implements HrmService
{

/** 自动注入持久层Dao对象 */
@Autowired
private UserDao userDao;
@Autowired
private DeptDao deptDao;
@Autowired
private EmployeeDao employeeDao;
@Autowired
private JobDao jobDao;
@Autowired
private NoticeDao noticeDao;
@Autowired
private DocumentDao documentDao;

/***************** 用户服务接口实现 *************************************/
/**
* HrmServiceImpl接口login方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public User login(String loginname, String password)
{
// System.out.println("HrmServiceImpl login -- >>");
return userDao.selectByLoginnameAndPassword(loginname, password);
}

/**
* HrmServiceImpl接口findUser方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public List<User> findUser(User user, PageModel pageModel)
{
/** 当前需要分页的总数据条数 */
Map<String, Object> params = new HashMap<>();
params.put("user", user);
int recordCount = userDao.count(params);
pageModel.setRecordCount(recordCount);
if (recordCount > 0)
{
/** 开始分页查询数据:查询第几页的数据 */
params.put("pageModel", pageModel);
}
List<User> users = userDao.selectByPage(params);

return users;
}

/**
* HrmServiceImpl接口findUserById方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public User findUserById(Integer id)
{
return userDao.selectById(id);
}

/**
* HrmServiceImpl接口removeUserById方法实现
* @see { HrmService }
*/
@Override
public void removeUserById(Integer id)
{
userDao.deleteById(id);

}

/**
* HrmServiceImpl接口addUser方法实现
* @see { HrmService }
*/
@Override
public void modifyUser(User user)
{
userDao.update(user);

}

/**
* HrmServiceImpl接口modifyUser方法实现
* @see { HrmService }
*/
@Override
public void addUser(User user)
{
userDao.save(user);

}

/***************** 部门服务接口实现 *************************************/
@Transactional(readOnly = true)
@Override
public List<Dept> findAllDept()
{

return deptDao.selectAllDept();
}

/**
* HrmServiceImpl接口findDept方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public List<Dept> findDept(Dept dept, PageModel pageModel)
{
/** 当前需要分页的总数据条数 */
Map<String, Object> params = new HashMap<>();
params.put("dept", dept);
int recordCount = deptDao.count(params);
pageModel.setRecordCount(recordCount);

if (recordCount > 0)
{
/** 开始分页查询数据:查询第几页的数据 */
params.put("pageModel", pageModel);
}

List<Dept> depts = deptDao.selectByPage(params);

return depts;
}

/**
* HrmServiceImpl接口removeUserById方法实现
* @see { HrmService }
*/
@Override
public void removeDeptById(Integer id)
{
deptDao.deleteById(id);

}

/**
* HrmServiceImpl接口addDept方法实现
* @see { HrmService }
*/
@Override
public void addDept(Dept dept)
{
deptDao.save(dept);

}

/**
* HrmServiceImpl接口findDeptById方法实现
* @see { HrmService }
*/
@Override
public Dept findDeptById(Integer id)
{

return deptDao.selectById(id);
}

/**
* HrmServiceImpl接口modifyDept方法实现
* @see { HrmService }
*/
@Override
public void modifyDept(Dept dept)
{
deptDao.update(dept);

}
/***************** 员工服务接口实现 *************************************/
/**
* HrmService接口findEmployee方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public List<Employee> findEmployee(Employee employee, PageModel pageModel)
{
/** 当前需要分页的总数据条数 */
Map<String, Object> params = new HashMap<>();
params.put("employee", employee);

int recordCount = employeeDao.count(params);
pageModel.setRecordCount(recordCount);

if (recordCount > 0)
{
/** 开始分页查询数据:查询第几页的数据 */
params.put("pageModel", pageModel);
}
List<Employee> employees = employeeDao.selectByPage(params);
return employees;
}
/**
* HrmService接口removeEmployeeById方法实现
* @see { HrmService }
*/
@Override
public void removeEmployeeById(Integer id)
{
employeeDao.deleteById(id);

}
/**
* HrmService接口findEmployeeById方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public Employee findEmployeeById(Integer id)
{

return employeeDao.selectById(id);
}

/**
* HrmService接口addEmployee方法实现
* @see { HrmService }
*/
@Override
public void addEmployee(Employee employee)
{
employeeDao.save(employee);
}

/**
* HrmService接口modifyEmployee方法实现
* @see { HrmService }
*/
@Override
public void modifyEmployee(Employee employee)
{
employeeDao.update(employee);
}

/***************** 职位接口实现 *************************************/

/**
* HrmService接口findAllJob方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public List<Job> findAllJob()
{

return jobDao.selectAllJob();
}

/**
* HrmService接口findJob方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public List<Job> findJob(Job job, PageModel pageModel)
{
/** 当前需要分页的总数据条数 */
Map<String, Object> params = new HashMap<>();
params.put("job", job);
int recordCount = jobDao.count(params);
pageModel.setRecordCount(recordCount);

if (recordCount > 0)
{
/** 开始分页查询数据:查询第几页的数据 */
params.put("pageModel", pageModel);
}

List<Job> jobs = jobDao.selectByPage(params);

return jobs;
}

/**
* HrmService接口removeJobById方法实现
* @see { HrmService }
*/
@Override
public void removeJobById(Integer id)
{
jobDao.deleteById(id);

}

/**
* HrmService接口addJob方法实现
* @see { HrmService }
*/
@Override
public void addJob(Job job)
{
jobDao.save(job);

}

/**
* HrmService接口findJobById方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public Job findJobById(Integer id)
{

return jobDao.selectById(id);
}

/**
* HrmService接口modifyJob方法实现
* @see { HrmService }
*/
@Override
public void modifyJob(Job job)
{
jobDao.update(job);

}

/***************** 公告接口实现 *************************************/
@Transactional(readOnly = true)
@Override
public List<Notice> findNotice(Notice notice, PageModel pageModel)
{
/** 当前需要分页的总数据条数 */
Map<String, Object> params = new HashMap<>();
params.put("notice", notice);
int recordCount = noticeDao.count(params);
pageModel.setRecordCount(recordCount);

if (recordCount > 0)
{
/** 开始分页查询数据:查询第几页的数据 */
params.put("pageModel", pageModel);
}

List<Notice> notices = noticeDao.selectByPage(params);

return notices;
}

/**
* HrmService接口findNoticeById方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public Notice findNoticeById(Integer id)
{

return noticeDao.selectById(id);
}

/**
* HrmService接口removeNoticeById方法实现
* @see { HrmService }
*/
@Override
public void removeNoticeById(Integer id)
{
noticeDao.deleteById(id);

}

/**
* HrmService接口addNotice方法实现
* @see { HrmService }
*/
@Override
public void addNotice(Notice notice)
{
noticeDao.save(notice);

}
/**
* HrmService接口modifyNotice方法实现
* @see { HrmService }
*/
@Override
public void modifyNotice(Notice notice)
{
noticeDao.update(notice);

}
/***************** 文件接口实现 *************************************/
/**
* HrmService接口findDocument方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public List<Document> findDocument(Document document, PageModel pageModel)
{
/** 当前需要分页的总数据条数 */
Map<String, Object> params = new HashMap<>();
params.put("document", document);
int recordCount = documentDao.count(params);
pageModel.setRecordCount(recordCount);

if (recordCount > 0)
{
/** 开始分页查询数据:查询第几页的数据 */
params.put("pageModel", pageModel);
}

List<Document> documents = documentDao.selectByPage(params);

return documents;
}

/**
* HrmService接口addDocument方法实现
* @see { HrmService }
*/
@Override
public void addDocument(Document document)
{
documentDao.save(document);

}
/**
* HrmService接口removeDocumentById方法实现
* @see { HrmService }
*/
@Override
public void removeDocumentById(Integer id)
{
documentDao.deleteById(id);

}
/**
* HrmService接口modifyDocument方法实现
* @see { HrmService }
*/
@Override
public void modifyDocument(Document document)
{
documentDao.update(document);

}
/**
* HrmService接口findDocumentById方法实现
* @see { HrmService }
*/
@Transactional(readOnly = true)
@Override
public Document findDocumentById(Integer id)
{
return documentDao.selectById(id);
}
}

HrmServiceImpl类中实现了服务接口HrmService中定义的所有业务逻辑方法并且在HrmServiceImpl类上使用了两个注解

1
2
3
4
5
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT
)
@Service("hrmService")

Transactional注解说明

1
2
3
4
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT
)

表示该类需要Spring加入事务,

  • propagation = Propagation.REQUIRED表示:有事务就处于当前事务中,没有事务就创建一个事务
  • isolation = Isolation.DEFAULT表示:使用事务数据库的默认隔离级别

Service注解说明

@Service("hrmService")注解表示将该类配置成一个SpringBean,标识符是hrmService

Autowired注解说明

@Autowired。在HrmServiceImpl类中业务方法的实现依赖于DAO组件,在配置文件中使用的XML:<mybatis:scan base-package="org.fkit.hrm.dao"/>会将orgfkit.hrm.dao文件夹下的所有MyBatis文件配置成SpringBean,Beanid就是文件的类的名称。
@Autowired注解默认使用byType自动装配将6个持久层的DAO注入给HrmServiceImpl类对应依赖的DAO组件。