实现业务逻辑接口HrmService
业务逻辑接口HrmService中定义了系统功能实现所需要调用的方法,HrmServiceImpl类实现了该接口,对每个方法的方法体进行了具体实现:
package org.fkit.hrm.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;
/**
* 人事管理系统服务层接口实现类
* @author luliangchang
*
*/
@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 DocumentDao documentDao;
@Autowired
private EmployeeDao employeeDao;
@Autowired
private JobDao jobDao;
@Autowired
private NoticeDao noticeDao;
/*******************用户服务层接口实现************/
//实现HrmService接口的login方法
@Transactional(readOnly=true)
@Override
public User login(String loginName,String password){
System.out.println("调用HrmServiceImpl类方法login -->");
System.out.println(loginName);
System.out.println(password);
return userDao.selectUserByLoginnameAndPassword(loginName, password);
}
//实现HrmService接口的selectUserById方法
@Transactional(readOnly=true)
@Override
public User selectUserById(Integer id){
return userDao.selectUserById(id);
}
//实现HrmService接口的selectUser方法
@Transactional(readOnly=true)
public List<User> selectUser(User user,PageModel pageModel){
Map<String,Object> params=new HashMap<String,Object>();
params.put("user", user);
int recordCount=userDao.count(params);
System.out.println("调用HrmServiceImpl类方法selecctAllUser -->");
System.out.println(pageModel.getPageIndex());
System.out.println(pageModel.firstLimitParam);
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
if(recordCount>0){
/**开始分页查询数据:查询第几页的数据**/
params.put("pageModel", pageModel);
}
List<User> users=userDao.selectByPage(params);
return users;
}
//实现HrmService接口的deleteUser方法
@Transactional(readOnly=true)
@Override
public void deleteUser(Integer id){
userDao.deleteById(id);
}
//实现HrmService接口的insertUser方法
@Transactional(readOnly=true)
@Override
public void insertUser(User user){
userDao.save(user);
}
//实现HrmService接口的updateUser方法
@Transactional(readOnly=true)
@Override
public void updateUser(User user){
userDao.update(user);
}
//实现HrmService接口的selectUserByUserName方法
@Transactional(readOnly=true)
@Override
public User selectUserByUserName(String userName){
User user=userDao.selectUserByUserName(userName);
return user;
}
/*******部门服务层接口实现******/
//实现HrmService接口的selectById方法
@Transactional(readOnly=true)
@Override
public Dept selectDeptById(Integer id){
return deptDao.selectById(id);
}
//实现HrmService接口的selectAll方法
@Transactional(readOnly=true)
@Override
public List<Dept> selectDept(Dept dept,PageModel pageModel){
//当前需要分页的总记录数
Map<String,Object> params=new HashMap<String,Object>();
params.put("dept", dept);
int recordCount=deptDao.count(params);
System.out.println("调用HrmServiceImpl方法selectAll-->");
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
if(recordCount>0){
/**开始分页查询数据,查询第几页数据**/
params.put("pageModel", pageModel);
}
List<Dept> depts=deptDao.selectByPage(params);
return depts;
}
//实现HrmService接口的deleteDept方法
@Transactional(readOnly=true)
@Override
public void deleteDept(Integer id){
deptDao.deleteById(id);
}
//实现HrmService接口的insertDept方法
@Transactional(readOnly=true)
@Override
public void insertDept(Dept dept){
deptDao.save(dept);
}
//实现HrmService接口的updateDept方法
@Transactional(readOnly=true)
public void updateDept(Dept dept){
deptDao.update(dept);
}
//实现HrmService接口的selectDeptByDeptName方法
@Transactional(readOnly=true)
public Dept selectDeptByDeptName(String deptName){
Dept dept=deptDao.selectDeptByDeptName(deptName);
return dept;
}
/*******员工服务层接口实现******/
//实现HrmService接口的selectEmployee方法
@Transactional(readOnly=true)
@Override
public List<Employee> selectEmployee(Employee employee,PageModel pageModel){
/**当前需要分页的总数据条数**/
Map<String,Object> params=new HashMap<String,Object>();
params.put("employee", employee);
int recordCount=employeeDao.count(params);
System.out.println("调用HrmServieImpl方法selectEmployee-->");
pageModel.setRecordCount(recordCount);
pageModel.updatePageIndex();
pageModel.setFirstLimitParam();
if(recordCount>0){
params.put("pageModel",pageModel);
}
List<Employee> employees=employeeDao.selectByPage(params);
return employees;
}
//实现HrmService接口的selectEmployeeById方法
@Transactional(readOnly=true)
@Override
public Employee selectEmployeeById(Integer id){
return employeeDao.selectById(id);
}
//实现HrmService接口的insetEmployee方法
@Transactional(readOnly=true)
@Override
public void insertEmployee(Employee employee){
employeeDao.insertEmployee(employee);
}
//实现HrmService接口的updateEmployee方法
@Transactional(readOnly=true)
public void updateEmployee(Employee employee){
employeeDao.updateEmployee(employee);
}
//实现HrmService接口的deleteEmployee方法
@Transactional(readOnly=true)
@Override
public void deleteEmployee(Integer id){
employeeDao.deleteById(id);
}
/******职位服务层接口实现******/
//实现HrmService接口的selectAllJob方法
@Transactional(readOnly=true)
@Override
public List<Job> selectAllJob(){
return jobDao.selectAllJob();
}
//实现HrmService接口的selectJob方法
@Transactional(readOnly=true)
@Override
public List<Job> selectJob(Job job,PageModel pageModel){
Map<String,Object> params=new HashMap<String,Object>();
params.put("job",job);
int recordCount=jobDao.count(params);
System.out.println("调用HrmServiceImpl类方法selectJob-->");
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
if(recordCount>0){
params.put("pageModel", pageModel);
}
List<Job> jobs=jobDao.selectByPage(params);
return jobs;
}
//实现HrmService接口的selectJobById方法
@Transactional(readOnly=true)
@Override
public Job selectJobById(Integer id){
return jobDao.selectById(id);
}
//实现HrmService接口的updateJob方法
@Transactional(readOnly=true)
public void updateJob(Job job){
jobDao.update(job);
}
//实现HrmService接口的insertJob方法
@Transactional(readOnly=true)
@Override
public void insertJob(Job job){
jobDao.save(job);
}
//实现HrmService接口的deleteJob方法
@Transactional(readOnly=true)
@Override
public void deleteJob(Integer id){
jobDao.deleteById(id);
}
//实现HrmService接口的selectJobByJobName方法
@Transactional(readOnly=true)
public Job selectJobByJobName(String jobName){
Job job=jobDao.selectJobByJobName(jobName);
return job;
}
@Override
public void deleteDocument(Integer id) {
documentDao.deleteNotice(id);
}
@Override
public void deleteNotice(Integer id) {
noticeDao.deleteNotice(id);
}
@Override
public void insertDocument(Document document) {
documentDao.insert(document);
}
@Override
public void insertNotice(Notice notice) {
noticeDao.insertNotice(notice);
}
@Override
public List<Dept> selectAll() {
return deptDao.selectAll();
}
@Override
public List<Document> selectDocument(Document document, PageModel pageModel) {
Map<String,Object> params=new HashMap<String,Object>();
params.put("document", document);
int recordCount=documentDao.count(params);
System.out.println("调用HrmServiceImpl类方法selectDocuemnt-->");
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
if(recordCount>0){
params.put("pageModel", pageModel);
}
List<Document> documents=documentDao.selectWhitParam(params);
return documents;
}
@Override
public Document selectDovumentById(Integer id) {
return documentDao.selectDocument(id);
}
@Override
public List<Notice> selectNotice(Notice notice, PageModel pageModel) {
Map<String,Object> params=new HashMap<String,Object>();
params.put("notice", notice);
int recordCount=noticeDao.count(params);
System.out.println("调用HrmServiceImpl类方法selectNotice-->");
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
if(recordCount>0){
params.put("pageModel", pageModel);
}
List<Notice> notices=noticeDao.selectByPage(params);
return notices;
}
@Override
public Notice selectNoticeById(Integer id) {
return noticeDao.selectById(id);
}
@Override
public void updateDocument(Document document) {
documentDao.update(document);
}
@Override
public void updateNotice(Notice notice) {
noticeDao.updateNoctice(notice);
}
}
该类是对HrmService接口的具体实现,每个方法通过调用DAO组件来实现具体系统的具体功能。业务逻辑组件与DAO组件通过Spring框架的依赖注入与IOC紧密连接在一起,去除了过去那种复杂、繁琐的过程。
对其中几个地方稍作解释:
@Service("hrmService")
@Service注解的作用是将此类注释成为Spring的Bean,它与applicationContext.xml文件中的下列内容配合使用。
<!-- 扫描org.fkit.包下的java文件,若有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit.hrm"/>
这种依赖注入的思维使得在Controller中对业务逻辑组件进行使用之前只需要自动注入该bean即可。
/**
* 自动注入hrmService
*/
@Autowired
@Qualifier("hrmService")
private HrmService hrmService
同样的在HrmServiceImpl类中也有针对DAO组件的自动注入,即
/*
* 自动注入持久层dao对象
*/
@Autowired
private UserDao userDao;
@Autowired
private DeptDao deptDao;
@Autowired
private DocumentDao documentDao;
@Autowired
private EmployeeDao employeeDao;
@Autowired
private JobDao jobDao;
@Autowired
private NoticeDao noticeDao;