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(); } }
|
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 { private static final String TAG = "{0}"; private int pageIndex; private int pageSize; private int recordCount; 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) { totalPage = (this.recordCount - 1) / this.pageSize + 1; if(this.pageIndex == 1) { str.append("<span class='disabled'>上一页</span>"); this.calcPage(str); 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( " 跳转到 <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( " <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>"); } } } 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 {
User login(String loginname,String password);
User findUserById(Integer id);
List<User> findUser(User user,PageModel pageModel);
void removeUserById(Integer id);
void modifyUser(User user);
void addUser(User user);
List<Employee> findEmployee(Employee employee,PageModel pageModel);
void removeEmployeeById(Integer id);
Employee findEmployeeById(Integer id);
void addEmployee(Employee employee);
void modifyEmployee(Employee employee);
List<Dept> findDept(Dept dept,PageModel pageModel);
List<Dept> findAllDept();
public void removeDeptById(Integer id);
void addDept(Dept dept);
Dept findDeptById(Integer id);
void modifyDept(Dept dept);
List<Job> findAllJob(); List<Job> findJob(Job job,PageModel pageModel);
public void removeJobById(Integer id);
void addJob(Job job);
Job findJobById(Integer id);
void modifyJob(Job job);
List<Notice> findNotice(Notice notice,PageModel pageModel);
Notice findNoticeById(Integer id);
public void removeNoticeById(Integer id);
void addNotice(Notice notice);
void modifyNotice(Notice notice);
List<Document> findDocument(Document document,PageModel pageModel);
void addDocument(Document document);
Document findDocumentById(Integer id);
public void removeDocumentById(Integer id);
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 {
@Autowired private UserDao userDao; @Autowired private DeptDao deptDao; @Autowired private EmployeeDao employeeDao; @Autowired private JobDao jobDao; @Autowired private NoticeDao noticeDao; @Autowired private DocumentDao documentDao;
@Transactional(readOnly = true) @Override public User login(String loginname, String password) { return userDao.selectByLoginnameAndPassword(loginname, password); }
@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; }
@Transactional(readOnly = true) @Override public User findUserById(Integer id) { return userDao.selectById(id); }
@Override public void removeUserById(Integer id) { userDao.deleteById(id);
}
@Override public void modifyUser(User user) { userDao.update(user);
}
@Override public void addUser(User user) { userDao.save(user);
}
@Transactional(readOnly = true) @Override public List<Dept> findAllDept() {
return deptDao.selectAllDept(); }
@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; }
@Override public void removeDeptById(Integer id) { deptDao.deleteById(id);
}
@Override public void addDept(Dept dept) { deptDao.save(dept);
}
@Override public Dept findDeptById(Integer id) {
return deptDao.selectById(id); }
@Override public void modifyDept(Dept dept) { deptDao.update(dept);
}
@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; }
@Override public void removeEmployeeById(Integer id) { employeeDao.deleteById(id);
}
@Transactional(readOnly = true) @Override public Employee findEmployeeById(Integer id) {
return employeeDao.selectById(id); }
@Override public void addEmployee(Employee employee) { employeeDao.save(employee); }
@Override public void modifyEmployee(Employee employee) { employeeDao.update(employee); }
@Transactional(readOnly = true) @Override public List<Job> findAllJob() {
return jobDao.selectAllJob(); }
@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; }
@Override public void removeJobById(Integer id) { jobDao.deleteById(id);
}
@Override public void addJob(Job job) { jobDao.save(job);
}
@Transactional(readOnly = true) @Override public Job findJobById(Integer id) {
return jobDao.selectById(id); }
@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; }
@Transactional(readOnly = true) @Override public Notice findNoticeById(Integer id) {
return noticeDao.selectById(id); }
@Override public void removeNoticeById(Integer id) { noticeDao.deleteById(id);
}
@Override public void addNotice(Notice notice) { noticeDao.save(notice);
}
@Override public void modifyNotice(Notice notice) { noticeDao.update(notice);
}
@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; }
@Override public void addDocument(Document document) { documentDao.save(document);
}
@Override public void removeDocumentById(Integer id) { documentDao.deleteById(id);
}
@Override public void modifyDocument(Document document) { documentDao.update(document);
}
@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")
注解表示将该类配置成一个Spring
的Bean
,标识符是hrmService
。
Autowired注解说明
@Autowired
。在HrmServiceImpl
类中业务方法的实现依赖于DAO
组件,在配置文件中使用的XML:<mybatis:scan base-package="org.fkit.hrm.dao"/>
会将orgfkit.hrm.dao
文件夹下的所有MyBatis
文件配置成Spring
的Bean
,Bean
的id
就是文件的类的名称。
@Autowired
注解默认使用byType
自动装配将6
个持久层的DAO
注入给HrmServiceImpl
类对应依赖的DAO
组件。
原文链接: 13.4.2 实现业务逻辑组件