首页 > 开发 > Java > 正文

详解使用SSM实现简单工作流系统之实现篇

2019-10-21 18:42:16
字体:
来源:转载
供稿:网友

项目说明

本项目是依据《轻量级 Java EE 企业应用实战 第4版》的最后一章中的项目实现的,原本项目使用的框架是Struts2 + Spring 4 + Hibernate,因为本人在学习Spring MVC + Spring + Mybatis,所以将它重写了一次,使用了Spring MVC + Spring + Mybatis项目。在正文中介绍项目的时候,也将主要依据原书的叙述。

因为项目是根据原始项目实现的,实现过程中可能出现有Bug,如果发现问题可以与我联系,或在评论区中留言评论。

零,准备工作

首先根据参考链接建立SSM基本项目;也可以直接从github上直接下载, 下载之后, 在目录initProject下面有一个HRSystem项目,这个项目就是初始项目了。这个项目是根据前面提供的链接实现的,整合了基本的SSM框架,直接部署Tomcat就可以运行了,部署方式可以直接参考前面的链接。该项目下还有一个目录就是originalProject,该目录存放的是原书中的项目代码,也是本文代码的参考实现。

Github地址

HRSystem: https://github.com/sysuKinthon/HRSystem (包含项目初始项目和原始SSH项目)
HRSystem_Dev: https://github.com/sysuKinthon/HRSystem_Dev (包含完整的SSM项目代码) 

上面的项目都是idea项目,下载到相应的项目后,运行步骤如下:

  • 在IDEA中打开
  • 建立下数据库,数据库使用MySQL,sql文件在src/main/resource目录下,叫做schema.sql。
  • 修改下项目中的spring-mybatis.xml里面关于数据库的配置,主要修改用户名和密码。
  • 上述都配置好后,再配置一下tomcat就可以了。

一,项目背景及系统结构

1)应用背景

项目实现的是一个简单的工作流系统,该系统包含公司日常的事务:日常考勤,工资结算及签核申请等。该签核系统完成了考勤改变申请的送签,以及对申请的签核,这是一种简单的工作流,,可以提高企业的生产效率,另外,本应用额外的打卡系统,自动工资结算等,也可以在一定程度上提高企业的生产效率。

2)系统功能介绍

系统的用户主要分为两种:普通员工和经理。

普通员工的功能包括

  • 员工通过打卡完成每天上下班的考勤记录,考勤记录包括迟到,早退,旷工等;
  • 员工也可以查看本人最近3天的考勤情况;
  • 如果发现考勤情况与实际不符,可提出申请,申请将由系统自动转发给员工经理,如果经理通过核准,则此申请自动生效,系统将考勤必为实际的情况;
  • 员工也可以查看自己的工资记录

经理的功能包括

  • 包含上述普通员工的功能,但是经理的考勤不能提出申请;
  • 签核员工申请
  • 新增员工
  • 查看管理的全部员工
  • 查看员工的上月工资

3)系统结构

  • 表现层:由JSP页面组成
  • MVC层:使用MVC框架技术(Spring MVC)
  • 业务逻辑层:主要由Spring IoC容器管理的业务逻辑组件组成(门面模式)
  • DAO层:由6个DAO组件组成
  • 领域对象层:由7个持久化对象组成(使用贫血模式设计)
  • 数据库服务层:使用MySQL数据库存储持久化数据

4)系统功能模块

本系统可以大致分为两个模块:经理模块和员工模块,其主要业务逻辑通过EmpManagerService和MgrManagerService两个业务逻辑组件来实现,因此可以使用这两个业务组件来封装DAO组件。

系统以业务逻辑组件作为DAO组件的门面,封装这些DAO组件,业务逻辑组件底层依赖于这些DAO组件,向上实现系统的业务逻辑功能

本系统主要有如下6个DAO对象

  • ApplicationDao:提供对application_inf表的基本操作
  • AttendDao:提供对attend_inf表的基本操作
  • AttendTypeDao:提供对attend_type_inf表的基本操作
  • CheckbackDao:提供对checkback_inf表的基本操作
  • EmployeeDao:提供对employee_inf表的基本操作
  • PaymentDao:提供对payment_inf表的基本操作

系统提供了两个业务逻辑组件:

  • EmpManagerSerivce:提供Employee角色所需业务逻辑功能的实现
  • MgrManagerService:提供Manager角色所需业务逻辑功能的实现

二,持久层设计

1)设计持久化实体

面向对象分析,是指根据系统需求提取应用中的对象,将这些对象抽象成类,再抽取出需需要持久化保存的类,这些需要持久化保持的类就是持久化对象(PO)。

本项目一共设计了7个持久化类

  • Application: 对应普通员工的考勤提出申请,包含申请理由,是否被批复及申请改变的类型等属性
  • Attend: 对应每天的考勤,包含考勤时间,考勤员工,是否上班及考勤类别等信息
  • AttendType: 对应考勤的类别,包含考勤的名称,如迟到,早退等
  • Checkback: 对应批复,包含该批复对应的申请,是否通过申请,由哪个经理完成批复等属性
  • Employee: 对应系统的员工信息,包含员工的用户名,密码,工资以及对应的经理等属性
  • Manager: 对应系统的经理信息,公包含经理管理的部门名。实际上,Manager继承了Employee类,因此该类同样包含Employee的所有属性
  • Payment: 对应每月所发的薪水信息,包含发薪月份,领薪员工和薪资数等信息

本应用采用贫血模式来设计它们,所以持久化类中不提供业务逻辑方法,而是将所有的业务逻辑方法放到业务逻辑组件中实现。

当采用贫血模式的架构模型时,系统中的领域对象十分简洁,它们都是单纯的数据类,不需要考虑到底应该包含哪些业务逻辑方法,因此开发起来非常便捷;而系统的所有的业务逻辑都由业务逻辑组件负责实现,可以将业务逻辑的变化限制在业务逻辑层内,从而避免扩散到两个层,因此降低了系统的开发难度。

7个PO的关系如下:

  • Employee是Manager的父类,同时Manager和Employee之间存在 1-N的关系,即一个Manager对应多个Employee,但每个Employee只能对应一个Manager
  • Employee和Payment之间是1-N的关系,即每个员工可以多次领取薪水
  • Employee和Attend之间存在1-N的关系,即每个员工可以参与多次考勤,但每次考勤只对应一个员工
  • Manager继承了Employee类,因此具有Employee的全部属性,另外Manager还不慌不忙 Checkback之间存在1-N的关系
  • Application与Attend之间存在N-1的关系,即每个Attend可以被对应多次申请。
  • Application与AttendType之间存在N-1的关系,即每次申请都有明确的考勤类型,而一个考勤类型可以对应多个申请
  • Attend与AttendType之间存在N-1的关系,即每个Attend只属于一个AttendType。

根据上面书写如下的schema.sql,也就是数据库文件

CREATE DATABASE IF NOT EXISTS hrSystem COLLATE = 'utf8_general_ci' CHARACTER SET = 'utf8'; use hrSystem;create table attend_type_inf( type_id int auto_increment, amerce_amount double not null, type_name varchar(50) not null, primary key(type_id));  create table employee_inf( emp_id int auto_increment, emp_type int, emp_name varchar(50) not null, emp_pass varchar(50) not null, emp_salary double not null, mgr_id int, dept_name varchar(50), primary key(emp_id), unique key(emp_name), foreign key(mgr_id) references employee_inf(emp_id));  create table attend_inf( attend_id int auto_increment, duty_day varchar(50) not null, punch_time datetime, is_come boolean not null, type_id int not null, emp_id int not null, primary key(attend_id), foreign key(type_id) references attend_type_inf(type_id), foreign key(emp_id) references employee_inf(emp_id));  create table application_inf( app_id int auto_increment, attend_id int not null, app_reason varchar(255), app_result boolean, type_id int not null, primary key(app_id), foreign key(type_id) references attend_type_inf(type_id), foreign key(attend_id) references attend_inf(attend_id));  create table payment_inf( pay_id int auto_increment, pay_month varchar(50) not null, pay_amount double not null, emp_id int not null, primary key(pay_id), foreign key(emp_id) references employee_inf(emp_id));  create table checkback_inf( check_id int auto_increment, app_id int not null, check_result boolean not null, check_reason varchar(255), mgr_id int not null, primary key(check_id), foreign key(app_id) references application_inf(app_id), foreign key(mgr_id) references employee_inf(emp_id));  INSERT INTO `test_user` VALUES ('1', '123455@qq.com','12345', 'test');  insert into attend_type_inf ( type_name , amerce_amount) values ( '正常', 0);insert into attend_type_inf ( type_name , amerce_amount) values ( '事假', -20);insert into attend_type_inf ( type_name , amerce_amount) values ( '病假', -10);insert into attend_type_inf ( type_name , amerce_amount) values ( '迟到', -10);insert into attend_type_inf ( type_name , amerce_amount) values ( '早退', -10);insert into attend_type_inf ( type_name , amerce_amount) values ( '旷工', -30);insert into attend_type_inf ( type_name , amerce_amount) values ( '出差', 10);  # 插入经理insert into employee_inf (emp_type , emp_name , emp_pass , emp_salary , mgr_id , dept_name) values (2, 'oracle', 'oracle' , 5000 , null , 'DB部');insert into employee_inf (emp_type , emp_name , emp_pass , emp_salary , mgr_id , dept_name) values (2, 'weblogic', 'weblogic' , 6000 , null , 'Server部');# 员工insert into employee_inf (emp_type , emp_name , emp_pass , emp_salary , mgr_id) values (1 , 'mysql', 'mysql' , 3000 , 1);insert into employee_inf (emp_type , emp_name , emp_pass , emp_salary , mgr_id) values (1 , 'hsql', 'hsql' , 3200 , 1);insert into employee_inf (emp_type , emp_name , emp_pass , emp_salary , mgr_id) values (1 , 'tomcat', 'tomcat' , 2800 , 2);insert into employee_inf (emp_type , emp_name , emp_pass , emp_salary , mgr_id) values (1 , 'jetty', 'jetty' , 2560 , 2);

上面已经陈述了全部的设计,剩下的一些细节将在下面的代码实现中说明。

三,Model层

为了节约篇幅,下面的所有实现都只针对Employee类来说明,如有必要会针对其他进行说明

持久化实体类的存放目录是com.kevin.HRSystem.model,下面是Employee持久化类的源码:

package com.kevin.HRSystem.model; public class Employee { private long id; private int type; private String name; private String password; private double salary; private Manager manager;  public long getId() {  return id; }  public void setId(long id) {  this.id = id; }  public int getType() {  return type; }  public void setType(int type) {  this.type = type; }  public String getName() {  return name; }  public void setName(String name) {  this.name = name; }  public String getPassword() {  return password; }  public void setPassword(String password) {  this.password = password; }  public double getSalary() {  return salary; }  public void setSalary(double salary) {  this.salary = salary; }  public Manager getManager() {  return manager; }  public void setManager(Manager manager) {  this.manager = manager; }  @Override public String toString() {  String rsl = "Employee [id=" + this.id + ", name=" + this.name + ", password=" + this.password + ", type=" + this.type + ", salary=" + this.salary;  if(this.manager != null) {   rsl += ", managerId=" + this.manager.getId() + ", managerName=" + this.manager.getName() + "]";  }  return rsl; }}

在这里针对Employee.java和Manager.java进行一点说明,这两个的数据库表都是employee_inf,但是Employee没有dept_name(部门名称),而Manager没有mgr_id(所属部门经理id)

其他的model对象这里就不多说,直接参考源码即可。

四,DAO(Data Access Object)层

1)DAO组件的定义

在持久层之上,可以使用DAO组件再次封装数据库操作,这也是Java EE应用里常用的DAO模式。当使用DAO模式时,既体现了业务逻辑组件封装DAO组件的门面模式,也可分离业务逻辑组件和DAO组件的功能:业务逻辑组件负责业务逻辑的变化,而DAO组件负责持久化技术的变化,这也正是桥接模式的使用。

引入DAO模式后,每个DAO组件包含了数据库的访问逻辑;每个DAO组件可对一个数据库表完成基本的CURD等操作。

DAO模式是一种更符合软件工程的开发方式,使用DAO模式有如下理由:

  • DAO模式抽象出数据访问方式,业务逻辑组件无须理会底层的数据库访问细节,而只专注于业务逻辑的实现,业务逻辑组件只负责业务功能的变化
  • DAO将数据访问集中在独立的一层,所有的数据访问都由DAO对象完成,这层独立的DAO分离了数据访问的实现与其他业务逻辑,使得系统更具可维护性
  • DAO还有助于提升系统的可移植性,独立的DAO层使得系统能在不同的数据库之间轻易切换,底层的数据库实现对于业务逻辑组件是透明的。数据库移植时仅仅影响DAO层,不同数据库的切换也不会影响业务逻辑组件,因此提供了系统的可复用性

DAO组件提供了各持久化对象的基本的CRUD操作,而在DAO接口里则对DAO组件包含的各种CRUD方法提供了声明。使用DAO接口的原因是:避免业务逻辑组件与特定的DAO组件耦合

尽管很多的DAO组件方法需要根据业务逻辑需求的变化而变化,但是还是有一些通用的方法,在代码实现中,BaseDao接口包含了几个通用的方法,其定义如下:

package com.kevin.HRSystem.dao; import java.util.List; public interface BaseDao<T> {  void save(T entity); //保持持久化实例  void delete(long id); //根据主键删除持久化实例  void update(T entity); //更新持久化实例  T findById(long id); //根据主键加载持久化实例  List<T> findAll(); //获取数据表中全部的持久化实例}

DAO接口无须给出任何实现,仅仅是DAO组件包含的CRUD方法的定义,这些方法定义的实现取决于底层的持久化技术,DAO组件的实现既可以使用传统的JDBC,也可以采用Hibernate,MyBatis等技术。

如下是我们的EmployeeDao组件接口的源码

package com.kevin.HRSystem.dao; import com.kevin.HRSystem.model.Employee;import com.kevin.HRSystem.model.Manager;import org.apache.ibatis.annotations.Param; import java.util.List; public interface EmployeeDao extends BaseDao<Employee>{ /**  * 根据用户名和密码查询员工  * @param name 用户名  *  password 密码  * @return 返回员工  */ public Employee findByNameAndPass(@Param("name")String name, @Param("password") String password);  public Employee findByName(@Param("name")String name);  public List<Employee> findEmployeesByMgrId(@Param("id") long id);  public void saveAsEmployee(Employee employee);  public void saveAsManager(Manager manager);}

其他的DAO组件接口的源码请直接查看代码;这里有一个要明确的,就是持久化对象里面有Employee和Manager,但是DAO组件里面只有EmployeeDAO,因为这两个持久化对象对应的其实是同一个数据库表,在实现的过程中,我将两个持化对象的CRUD整合到同一个DAO组件里面了。

DAO接口只定义了DAO组件应该实现的方法,但如何实现这些DAO方法则没有任何限制,程序可以使用任何持久化技术来实现它们,这样就可以让DAO组件来负责持久化技术这个维度的变化,当系统需要在不同的持久化技术之间迁移时,应用只需要提供不同的DAO实现类即可,程序的其他部分无须进行任何改变,这就很好地提高了系统的可扩展性。

2)DAO组件的实现

在本项目中,DAO层的实现是使用了MyBatis技术,在基础项目中, 我们已经在Spring中引入了MyBatis,其配置文件(spring-mybatis.xml)如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">  <!--扫描service包下所有注解的类型--> <context:component-scan base-package="com.kevin.HRSystem.service"/>  <!--配置数据库相关参数properties的属性--> <context:property-placeholder location="classpath:jdbc.properties"/>  <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  <property name="driverClass" value="${jdbc.driver}"/>  <property name="jdbcUrl" value="${jdbc.url}"/>  <property name="user" value="${jdbc.username}"/>  <property name="password" value="${jdbc.password}"/>  <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>  <property name="minPoolSize" value="${c3p0.minPoolSize}"/>  <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>  <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>  <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/> </bean>  <!--配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource"/>  <!--扫描Model包,使用别名-->  <property name="typeAliasesPackage" value="com.kevin.HRSystem.model"/>  <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean>  <!--配置扫描DAO接口包,动态实现Dao接口,注入到spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  <property name="basePackage" value="com.kevin.HRSystem.dao"/> </bean>  <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <!--注入数据库连接池-->  <property name="dataSource" ref="dataSource"/> </bean>  <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

根据上面的配置文件可以看出,我们的DAO层实现类是放在了classpath:mapper路径下面的,这里我们一样只给出EmployeeDao组件的实现,

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  <mapper namespace="com.kevin.HRSystem.dao.EmployeeDao"> <insert id="save" parameterType="Employee" useGeneratedKeys="true" keyProperty="id">  INSERT INTO employee_inf(emp_type, emp_name, emp_pass, emp_salary, mgr_id, dept_name) VALUES (#{type}, #{name}, #{password}, #{salary}, #{manager.id}, #{departmentName}) </insert>  <insert id="saveAsEmployee" parameterType="Employee" useGeneratedKeys="true" keyProperty="id">  INSERT INTO employee_inf(emp_type, emp_name, emp_pass, emp_salary, mgr_id) VALUES (#{type}, #{name}, #{password}, #{salary}, #{manager.id}) </insert>  <insert id="saveAsManager" parameterType="Manager" useGeneratedKeys="true" keyProperty="id">  INSERT INTO employee_inf(emp_type, emp_name, emp_pass, emp_salary, dept_name) VALUES (#{type}, #{name}, #{password}, #{salary}, #{departmentName}) </insert>   <delete id="delete" parameterType="long">  DELETE FROM employee_inf WHERE emp_id=#{id} </delete>  <update id="update" parameterType="Employee">  UPDATE employee_inf SET emp_name=#{name}, emp_pass=#{password}, emp_salary=#{saraly}, emp_type=#{type}, mgr_id=#{manager.id} WHERE emp_id=#{id} </update>  <!--查询语句 start-->  <select id="findAll" resultMap="employeeResultMap">  SELECT * FROM employee_inf </select>  <select id="findById" parameterType="long" resultMap="employeeResultMap">  SELECT * FROM employee_inf WHERE emp_id=#{id} </select>  <select id="findByNameAndPass" resultMap="employeeResultMap">  SELECT * FROM employee_inf WHERE emp_name=#{name} and emp_pass=#{password} </select>  <select id="findByName" resultMap="employeeResultMap">  SELECT * FROM employee_inf WHERE emp_name=#{name} </select>  <!--根据mgr_id查找员工--> <select id="findEmployeesByMgrId" parameterType="long" resultMap="employeeResultMap">  SELECT * FROM employee_inf WHERE mgr_id=#{id} </select>  <!--查询语句 end -->  <!-- resultMap设置,使用鉴别器来区分employee和manager--> <resultMap id ="employeeResultMap" type="com.kevin.HRSystem.model.Employee">  <id property="id" column="emp_id"/>  <result property="type" column="emp_type"/>  <result property="name" column="emp_name"/>  <result property="password" column="emp_pass"/>  <result property="salary" column="emp_salary"/>  <discriminator javaType="int" column="emp_type">   <case value="1" resultMap="originalEmployeeResultMap"/>   <case value="2" resultMap="managerResultMap"/>  </discriminator> </resultMap>   <resultMap id="originalEmployeeResultMap" type="com.kevin.HRSystem.model.Employee" extends="employeeResultMap">  <association property="manager" javaType="com.kevin.HRSystem.model.Manager">   <id property="id" column="mgr_id"/>  </association> </resultMap>  <resultMap id="managerResultMap" type="com.kevin.HRSystem.model.Manager" extends="employeeResultMap">  <result property="departmentName" column="dept_name"/>  <collection property="employees" column="emp_id" ofType="com.kevin.HRSystem.model.Employee" select="com.kevin.HRSystem.dao.EmployeeDao.findEmployeesByMgrId"/> </resultMap>  <!--基础resultMap end-->   <select id="selectByIdWithForeign" parameterType="long" resultMap="employeeWithForeignResultMap">  SELECT e.emp_id, e.emp_name, e.emp_pass, e.emp_type, e.emp_salary, m.emp_id mgr_id, m.emp_name mgr_name, m.dept_name FROM employee_inf e, employee_inf m WHERE e.mgr_id = m.emp_id and e.emp_id = #{id} </select>  <resultMap id ="employeeWithForeignResultMap" type="com.kevin.HRSystem.model.Employee">  <id property="id" column="emp_id"/>  <result property="type" column="emp_type"/>  <result property="name" column="emp_name"/>  <result property="password" column="emp_pass"/>  <result property="salary" column="emp_salary"/>  <association property="manager" javaType="com.kevin.HRSystem.model.Manager">   <id property="id" column="mgr_id"/>   <result property="name" column="mgr_name"/>   <result property="departmentName" column="dept_name"/>  </association> </resultMap> </mapper>

这个xml文件就是我们EmployeeDao组件的实现类了。关于这个类的鉴别器使用可以参考 SSM项目问题与解决(还没有出) 

四,Service层

1)业务逻辑组件的设计

业务逻辑组件是DAO组件的门面,所以可以理解为业务逻辑组件需要依赖于DAO组件。EmpManagerService接口(业务逻辑组件之一)里定义了大量的业务方法,这些方法的实现依赖于DAO组件,由于每个业务都要涉及多个DAO操作,其DAO操作是单条数据记录的操作,而业务逻辑方法的访问,则需要设计多个DAO操作,因此每个业务逻辑方法可能需要涉及多条记录的访问

业务逻辑组件面向DAO接口编程,可以让业务逻辑组件从DAO组件的实现中分离,因此业务逻辑组件只关系业务逻辑的实现,无须关心数据访问逻辑的实现

EmpManagerService接口的代码实现如下:

package com.kevin.HRSystem.service; import com.kevin.HRSystem.model.AttendType;import com.kevin.HRSystem.model.Employee;import com.kevin.HRSystem.model.Manager;import com.kevin.HRSystem.vo.AttendVo;import com.kevin.HRSystem.vo.PaymentVo; import java.util.List; public interface EmpManagerService {  /**  * 验证登录  * @param employee 登录的身份  * @return  * 登录后的身份确认:0为登录失败,1为登录emp,2为登录mgr  */ int validLogin(Employee employee);   /**  * 自动打卡,周一到周五,早上7点为每个员工插入旷工记录  */ void autoPunch();  /**  * 自动结算工资,每月1号,结算上个月工资  */ void autoPay();  /**  * 验证某个员工是否可以打卡,以及打卡的类型,上班打卡,还是下班打卡  * @param user 员工名  * @param dutyDay 日期  * @return 可打卡的类别  */ int validPunch(String user, String dutyDay);   /**  * 实现普通员工的打卡  * @param user 员工名  * @param dutyDay 打卡日期  * @param isCome 是否是上班打卡  * @return 打卡结果  */ int punch(String user, String dutyDay, boolean isCome);  /**  * 根据员工浏览自己的工资  * @param employeeName 员工名  * @return 该员工的工资列表  */ List<PaymentVo> employeeSalary(String employeeName);   /**  * 员工查看自己的最近三天的非正常打卡  * @param employeeName 员工名  * @return 该员工最近三天的非正常打卡  */ List<AttendVo> getUnAttend(String employeeName);   /**  * 返回全部的出勤类别  * @return 全部的出勤类别  */ List<AttendType> getAllType();  /**  * 添加申请  * @param attId 申请的出勤ID  * @param typeId 申请的类型ID  * @param reason 申请的理由  * @return 添加的结果  */ boolean addApplication(int attId, int typeId, String reason); }

2)实现业务逻辑组件

业务逻辑组件负责实现系统所需的业务方法,系统有多少个业务需求,业务逻辑组件就提供多少个对应方法,本应用采用的贫血模式的架构模型,因此业务逻辑方法完全由业务逻辑组件负责实现。

业务逻辑组件只负责业务逻辑上的变化,而持久层的变化则交给DAO层负责,因此业务逻辑组件必须依赖于DAO组件。

下面是EmpManagerServiceImpl的代码实现:

package com.kevin.HRSystem.service.impl; import com.kevin.HRSystem.dao.*;import com.kevin.HRSystem.model.*;import com.kevin.HRSystem.service.EmpManagerService;import com.kevin.HRSystem.vo.AttendVo;import com.kevin.HRSystem.vo.PaymentVo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import javax.annotation.Resource;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List; import static com.kevin.HRSystem.constant.ConstantManager.*; @Service("EmpManagerService")public class EmpManagerServiceImpl implements EmpManagerService { @Resource private ApplicationDao applicationDao; @Resource private AttendDao attendDao; @Resource private AttendTypeDao attendTypeDao; @Resource private CheckbackDao checkbackDao; @Resource private EmployeeDao employeeDao; @Resource private PaymentDao paymentDao;  public int validLogin(Employee employee) {   Employee employee1 = employeeDao.findByNameAndPass(employee.getName(), employee.getPassword());  if(null != employee1) {   if(employee1 instanceof Manager) {    System.out.println("EmpManagerService: manager");    System.out.println(employee);    return LOGIN_MGR;   } else {    System.out.println("EmpManagerService:employee");    System.out.println(employee);    return LOGIN_EMP;   }  }   return LOGIN_FALT; }  public void autoPunch() {  List<Employee> employees = employeeDao.findAll();  System.out.println(employees.size());  //获取当前时间  String dutyDay = new java.sql.Date(System.currentTimeMillis()).toString();  for(Employee employee : employees) {   System.out.println(employee);   //先设置出勤类型为旷工,然后真正出勤的时候由员工去修改出勤的状态   //6表示旷工;已经插入数据库了; 这里最好弄一个常量管理类;但为了方便先这样吧   AttendType attendType = attendTypeDao.findById(6);   Attend a = new Attend();   a.setAttendType(attendType);   a.setDutyDay(dutyDay);   //如果是早上,则对应上班打卡   if(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < AM_LIMIT) {    a.setCome(true);   } else {    //下班打卡    a.setCome(false);   }   a.setEmployee(employee);   attendDao.save(a);  } }  public void autoPay() {  List<Employee> employees = employeeDao.findAll();  //获取上个月时间  Calendar c = Calendar.getInstance();  c.add(Calendar.DAY_OF_MONTH, -15);  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  String payMonth = sdf.format(c.getTime());   for(Employee employee : employees) {   Payment pay = new Payment();   double amount = employee.getSalary();   //获取出勤记录   List<Attend> attends = attendDao.findByEmpAndMonth(employee, payMonth);   //计算工资   for(Attend attend : attends) {    amount += attend.getAttendType().getAmerceAmount();   }   pay.setPayMonth(payMonth);   pay.setEmployee(employee);   pay.setPayAmount(amount);   paymentDao.save(pay);  }  }  @Override public int validPunch(String user, String dutyDay) {  //不能查找到对应的用户,返回无法打卡  Employee employee = employeeDao.findByName(user);  if(null == employee) {   return NO_PUNCH;  }  //打到员工指定日期的出勤记录  List<Attend> attends = attendDao.findByEmpAndDutyDay(employee, dutyDay);  //系统没有为用户在当前插入空打卡记录,无法打卡  if(null == attends || attends.size() == 0) {   return NO_PUNCH;  }   //开始上班打卡  if(attends.size() == 1 && attends.get(0).isCome() && null == attends.get(0).getPunchTime()) {   return COME_PUNCH;  }  if(attends.size() == 1 && null == attends.get(0).getPunchTime()) {   return LEAVE_PUNCH;  }  if(attends.size() == 2) {   if(null == attends.get(0).getPunchTime() && null == attends.get(0).getPunchTime()) {    return BOTH_PUNCH;   } else if(null == attends.get(1).getPunchTime()) {    return LEAVE_PUNCH;   } else {    return NO_PUNCH;   }  }  return NO_PUNCH; }  public int punch(String user, String dutyDay, boolean isCome) {  Employee employee = employeeDao.findByName(user);  if(null == employee) {   return PUNCH_FALT;  }   //找到员工本次打卡对应的出勤记录  Attend attend = attendDao.findByEmpAndDutyDayAndCome(employee, dutyDay, isCome);  if(null == attend) {   return PUNCH_FALT;  }  //如果已经打卡  if(null != attend.getPunchTime()) {   return PUNCHED;  }   int punchHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);  attend.setPunchTime(new Date());  //上班打卡  if(isCome) {   //9点之前算正常   if(punchHour < COME_LIMIT) {    //1表示正常    attend.setAttendType(attendTypeDao.findById(1));   } else if (punchHour < LATE_LIMIT) {    //9点到11点之间算迟到    attend.setAttendType(attendTypeDao.findById(4));   }   //11点之后算旷工,不用管,本来初始就是旷工  } else {   //下班打卡   //18点之后算正常   if(punchHour >= LEAVE_LIMIT) {    attend.setAttendType(attendTypeDao.findById(1));   } else if(punchHour >= LEAVE_LIMIT) {    //16到18点算正常    attend.setAttendType(attendTypeDao.findById(5));   }  }  attendDao.update(attend);  return PUNCH_SUCC; }  public List<PaymentVo> employeeSalary(String employeeName) {  Employee employee = employeeDao.findByName(employeeName);   List<Payment> payments = paymentDao.findByEmp(employee);  System.out.println(payments);   List<PaymentVo> result = new ArrayList<PaymentVo>();   for(Payment payment : payments) {   result.add(new PaymentVo(payment.getPayMonth(), payment.getPayAmount()));  }  return result; }  /**  * 查看自己最近三天的非正常打卡  * @param employeeName 员工名  * @return 该员工的最近三天的非正常打卡  */ public List<AttendVo> getUnAttend(String employeeName) {  AttendType type = attendTypeDao.findById(1);  Employee employee = employeeDao.findByName(employeeName);   //获取最近三天非正常的出勤记录  List<Attend> attends = attendDao.findByEmpUnAttend(employee, type);  List<AttendVo> result = new ArrayList<AttendVo>();  for(Attend attend : attends) {   result.add(new AttendVo(attend.getId(), attend.getDutyDay(), attend.getAttendType().getTypeName(), attend.getPunchTime()));  }  return result;  }  public List<AttendType> getAllType() {  return attendTypeDao.findAll(); }  @Override public boolean addApplication(int attId, int typeId, String reason) {  Application application = new Application();  //获取申请需要改变的出勤记录  Attend attend = attendDao.findById(attId);  AttendType type = attendTypeDao.findById(typeId);  application.setAttend(attend);  application.setAttendType(type);  if(reason != null) {   application.setApplicationReason(reason);  }  applicationDao.save(application);  return true; }}

这里需要介绍一下上面业务逻辑组件的一些方法,首先是autoPunch和autoPay,这两个方法并不由客户端直接调用,而是由任务调度来执行.

系统会在每个工作日的早上7点和下午12点时自动调用autoPunch,这个方法负责为每个员工插入一条旷工考勤记录,所以一天就会插入两条旷工记录了。这样做的原因在于,先插入记录为旷工,然后在员工上班打卡时,就将早上7点插入的记录的出勤类型根据出勤时间进行修改为正常或是迟到等,如果没有打卡就是旷工了。在员工下班打卡的时候,根据下班打卡的时间将12点插入的记录进行修改其出勤类型。

系统同样会在每月3日为所有员工完成工资结算,这个是通过autoPay来实现的。

六,实现任务的自动调度

系统中常常有些需要自动执行的任务,这些任务可能每隔一段时间就要执行一次,也可能需要在指定时间点自动执行,这些任务的自动执行必须使用任务的自动调度。通过使用开源框架Quartz,借助于它的支持,既可以实现简单的任务调度,也可以实现复杂的任务调度。

1)引入Quartz

为了在Spring项目中引入Quartz, 我们需要在pom.xml中加入如下的包依赖,

 <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context-support</artifactId>  <version>${spring.version}</version> </dependency>   <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dependency>  <groupId>org.quartz-scheduler</groupId>  <artifactId>quartz</artifactId>  <version>2.2.1</version> </dependency>

2)实现任务调度

Quartz允许提供一个名为quartz.properties的配置文件,通过该配置文件,可以修改框架运行时的环境,其配置如下:

# 配置主调度器属性org.quartz.scheduler.instanceName = QuartzSchedulerorg.quartz.scheduler.instanceId = AUTO# 配置线程池# Quartz线程池的实现类org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool# 线程池的线程数量org.quartz.threadPool.threadCount = 1# 线程池里线程的优先级org.quartz.threadPool.threadPriority = 10# 配置作业存储org.quartz.jobStore.misfireThreshold = 60000org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

接下来就实现我们的两个作业类了,这两个作业类都需要去继承QuartzJobBean,PunchJob的实现如下:

package com.kevin.HRSystem.schedule;import com.kevin.HRSystem.service.EmpManagerService;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean; import javax.annotation.Resource; public class PunchJob extends QuartzJobBean { //判断作业是否执行的标志 private boolean isRunning = false;  @Resource private EmpManagerService empManagerService;  public void setEmpManagerService(EmpManagerService empManagerService) {  this.empManagerService = empManagerService; }  public void executeInternal(JobExecutionContext ctx) throws JobExecutionException {  if(!isRunning) {   System.out.println("开始调度自动打卡");   isRunning = true;   empManagerService.autoPunch();   System.out.println("打卡结束");   isRunning = false;  } }}

定义完上面的工作类后,就可以在配置文件中进行定义Bean了,新建一个spring-quartz.xml文件,其配置如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  <!--croExpression指定cron表达式:每月3日2时启动--> <bean id="cronTriggerPay"   class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"   p:cronExpression="0 0 2 3 * ? *">  <property name="jobDetail">   <!--使用嵌套Bean的方法来定义任务Bean,jobClass指定任务bean的实现类-->   <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" p:jobClass="com.kevin.HRSystem.schedule.PayJob" p:durability="true" >    <property name="jobDataAsMap">     <map>      <entry key="empManagerService" value-ref="EmpManagerService"/>     </map>    </property>   </bean>  </property> </bean>  <!--定义触发器来管理任务 Bean cronExpression指定Cron表达式: 周一到周五7点和12点执行调度--> <bean id="cronTriggerPunch" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" p:cronExpression="0 0 7,12 ? * MON-FRI">  <property name="jobDetail">   <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" p:jobClass="com.kevin.HRSystem.schedule.PunchJob" p:durability="true">    <property name="jobDataAsMap">     <map>      <entry key="empManagerService" value-ref="EmpManagerService"/>     </map>    </property>   </bean>   </property> </bean>  <!--执行实际的调度--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  <property name="triggers">   <list>    <ref bean="cronTriggerPay"/>    <ref bean="cronTriggerPunch"/>   </list>  </property> </bean> </beans>

关于Cron表达式的使用请自行Google,其配置还是比较直观的。 

七, 实现系统Web层

在这一层,我们使用的是SpringMVC,在初始项目中,已经配置好了相关的文件,主要是配置下spring-mvc.xml和web.xml,两个文件的配置如下:

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:mvc="http://www.springframework.org/schema/mvc"  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  <!-- 扫描web相关的bean --> <context:component-scan base-package="com.kevin.HRSystem.controller"/>  <!-- 开启SpringMVC注解模式 --> <mvc:annotation-driven/>  <!-- 静态资源默认servlet配置 --> <mvc:default-servlet-handler/>  <!-- 配置jsp 显示ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  <property name="prefix" value="/WEB-INF/views/"/>  <property name="suffix" value=".jsp"/> </bean></beans>

web.xml

<?xml version="1.0" encoding="utf-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">  <display-name>Archetype Created Web Application</display-name>  <!--编码过滤器--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param>  <param-name>encoding</param-name>  <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>  <!--配置SpringMVC中心控制器 DispatcherServlet--> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要加载的配置文件--> <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!--<async-supported>true</async-supported>--> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 匹配所有请求,此处也可以配置成 *.do 形式 --> <url-pattern>/</url-pattern> </servlet-mapping>  <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

配置好上面的文件之后,我们的所有请求就会被SpringMVC所拦截进行分发了。为了节约篇幅,我们这里只陈述实现登录业务的整体过程,其他的业务大致类似。

因为到Service层我们都已经实现了,只要定义好前端和Controller层的数据就可以了,我们先开发jsp页面。在这里有一个要注意的,在tomcat项目中,放在应用程序目录下的任何资源,用户都可以通过输入该资源的URL而直接进行访问,如果你希望某个资源可以被Servlet访问,但是不能被用户访问,那么应用把它放在WEB-INF目录下面; 所以我们的jsp页面是放在webapp/WEB-INF下面的,而图片等静态资源是放在了webapp/resources下。

为了在我们的jsp中使用jstl,我们还需要引入如下的依赖

 <dependency>  <groupId>javax.servlet</groupId>  <artifactId>jstl</artifactId>  <version>1.2</version> </dependency>  <dependency>  <groupId>taglibs</groupId>  <artifactId>standard</artifactId>  <version>1.1.2</version> </dependency>

首先们来看下main.jsp的源码,当index.jsp被访问时(欢迎页面),请求就会被转发到这个页面

<%-- Created by IntelliJ IDEA. User: kevin Date: 18-3-3 Time: 下午5:34 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Java EE简单工作流系统</title></head><body><%@include file="header.jsp"%><table width="960" align="center"  background="${pageContext.request.contextPath}/resources/images/bodybg.jpg"> <tr>  <td colspan="3">请单击后面链接开始使用系统<a href="loginPage" rel="external nofollow" >登录系统</a></td> </tr> <tr>  <td colspan="3">   <br>   <p align="center"><span class="pt11">这仅仅是一个Java EE的框架程序。应用模拟一个简单的工作流系统。系统包含两个角色,<br> 普通员工的功能包括员工出勤打卡,员工的人事异动申请,员工查看工资单。<br> 经理的功能包括管理部门员工,签核申请,每月工资自动结算等。</span></p>   <p align="center" class="pt11">应用模拟了简单的工作流,使用的轻量级Java EE架构。技术包括:Struts 2.3、Spring 4.0、Hibernate 4.3、Quartz 2.2,整个应用使用Spring提供的DAO支持操作数据库,同时利用Spring的声明式事务。<br>    程序中的权限检查使用Spring的AOP框架支持,也利用了Spring的任务调度抽象<br>    Hibernate为底层的数据库访问提供支持,作为O/R Mapping框架使用。</p>   <p align="center" class="pt11">本程序的源代码随程序一起发布,版权属于李刚,<a href="http://www.crazyit.org" rel="external nofollow" >http://www.crazyit.org</a><br>    任何个人可用来参考学习Java EE架构,规范,但请勿在本程序的基础上修改,用做任何商业用途。<br>    本人保留依法追究相关责任的权利。转载和学习请保留此信息。    <br>   </p>  </td> </tr></table><%@include file="footer.jsp"%></body></html>

所以当我们点击了登录系统按钮之后,就会发起/loginPage的请求,我们需要定义这个方法,其定义如下(在CommonController.java中):

 @RequestMapping(value = "/loginPage", method = RequestMethod.GET) public ModelAndView showLoginPage() {  return new ModelAndView("login", "employee", new Employee()); }

关于SpringMVC的基本使用可以参考:链接

通过上面的逻辑我们知道,我们的SpringMVC经过处理请求后,会发送login.jsp给前端,login.jsp的定义如下:

<%-- Created by IntelliJ IDEA. User: kevin Date: 18-3-3 Time: 下午5:55 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%><html><head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>登录系统</title></head> <body> <%@include file="header.jsp"%> <table width="960" align="center"   background="${pageContext.request.contextPath}/resources/images/bodybg.jpg">  <tr>   <td>    请输入用户名和密码来登录<br />    <c:if test="${error.length() > 0}">     <div class="error">       <c:out value="${error}"></c:out>     </div>    </c:if>    <div class="center">     <form:form method="POST" action="processLogin" modelAttribute="employee">      <table>       <tr>        <td><form:label path="name">Name</form:label></td>        <td><form:input path="name"/></td>       </tr>       <tr>        <td><form:label path="password">Password</form:label></td>        <td><form:password path="password"/></td>       </tr>       <tr>        <td><input type="submit" value="登录"></td>        <td><input type="reset" value="重填"></td>       </tr>      </table>     </form:form>    </div>   </td>  </tr> </table><%@include file="footer.jsp"%> </body></html>

当我们输入用户名和密码,并点击登录之后,会发起/processLogin的请求,其定义如下:

@RequestMapping(value = "/processLogin", method = RequestMethod.POST) public ModelAndView processLogin(@ModelAttribute("employee") Employee employee, HttpServletRequest request) {  System.out.println(employee);  System.out.println(request.getProtocol());  ModelAndView modelAndView;  int result = empManagerService.validLogin(employee);  String message;  //登录结果为普通员工  System.out.println(result);  if(result == ConstantManager.LOGIN_EMP) {   //设置Session   request.getSession().setAttribute(WebConstant.USER, employee.getName());   request.getSession().setAttribute(WebConstant.LEVEL, WebConstant.EMP_LEVEL);    message = "您已成功登录系统,您的身份是普通员工";   System.out.println(message);//   modelAndView = new ModelAndView("success");   modelAndView = new ModelAndView("employee/index");   modelAndView.addObject("message", message);   return modelAndView;  } else if(result == ConstantManager.LOGIN_MGR){   request.getSession().setAttribute(WebConstant.USER, employee.getName());   request.getSession().setAttribute(WebConstant.LEVEL, WebConstant.MGR_LEVEL);    message = "您已成功登录系统,您的身份是经理";   System.out.println(message);//   modelAndView = new ModelAndView("success");   modelAndView = new ModelAndView("manager/index");   modelAndView.addObject("message", message);   return modelAndView;  } else {   message = "用户名与密码不匹配,登录失败";   System.out.println(message);   modelAndView = new ModelAndView("error");   modelAndView.addObject("message", message);   return modelAndView;  } }

根据校验结果的不同,而有不同的返回页面,假设校验成功是一个普通员工的话,我们就返回了employee/index.jsp,并设置了message变量供jsp页面获取,employee/index.jsp如下:

<%-- Created by IntelliJ IDEA. User: kevin Date: 18-3-6 Time: 下午4:36 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>员工首页</title></head><body> <%@include file="../header.jsp"%> <%@include file="empheader.jsp"%> <table width="960" align="center" background="${pageContext.request.contextPath}/resources/images/bodybg.jpg">  <tr height="60">   <td> </td>  </tr>  <tr>   <td>    <c:if test="${message.length()>0}">     <div class="error">      <c:out value="${message}"></c:out>     </div>    </c:if>   </td>  </tr>  <tr height="80">   <td> </td>  </tr>  <tr>   <td><%=request.getSession().getAttribute("user")%>    ,欢迎您使用JavaEE简单工作流系统,您是普通员工</td>  </tr>  <tr height="60">   <td> </td>  </tr> </table><%@include file="../footer.jsp"%></body></html>

其他页面的开发逻辑也类似如上。

八,结语

项目是依据《轻量级Java EE企业应用实战 第4版》的SSH项目来实现的,所以可能在实现过程中会有些问题,如果有问题,欢迎留言讨论。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CuoXin错新网。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表