# 员工管理系统(SpringBoot+Mybatis+Thymeleaf)
SpringBoot整合CRUD实现员工管理案例,将Mybatis整合到原项目中,加入了数据库,添加了日期选项的控件。
#### 环境要求
- JDK8以上
- IDEA
- MySQL8
- Maven3
- 需要熟练掌握MySQL数据库,SpringBoot及MyBatis知识,简单的前端知识;
#### 数据库环境
创建案例所使用的数据库
```sql
CREATE DATABASE `employee`;
USE `employee`;
```
创建登陆用户数据表
```sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) NOT NULL,
`user_name` varchar(255) NOT NULL COMMENT '用户名',
`password` varchar(255) NOT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
INSERT INTO `user` VALUES (1, 'admin', '123456');
```
创建部门信息的数据库表
```sql
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`id` int(10) NOT NULL,
`department_name` varchar(255) NOT NULL COMMENT '部门名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
BEGIN;
INSERT INTO `department` VALUES (1, '市场部');
INSERT INTO `department` VALUES (2, '技术部');
INSERT INTO `department` VALUES (3, '销售部');
INSERT INTO `department` VALUES (4, '客服部');
INSERT INTO `department` VALUES (5, '公关部');
COMMIT;
```
创建存放员工信息的数据库表
```sql
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`employee_name` varchar(255) NOT NULL COMMENT '员工姓名',
`email` varchar(255) NOT NULL COMMENT '员工邮箱',
`gender` int(2) NOT NULL COMMENT '员工性别',
`department_id` int(10) NOT NULL COMMENT '部门编号',
`date` date NOT NULL COMMENT '入职日期',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
BEGIN;
INSERT INTO `employee` VALUES (1, '张三', 'zhangsan@gmail.com', 0, 1, '2020-05-12');
INSERT INTO `employee` VALUES (2, '李四', 'lisi@qq.com', 1, 2, '2020-05-05');
INSERT INTO `employee` VALUES (3, '王五', 'wangwu@126.com', 0, 3, '2020-05-15');
INSERT INTO `employee` VALUES (4, '赵六', 'zhaoliu@163.com', 1, 4, '2020-04-21');
INSERT INTO `employee` VALUES (5, '田七', 'tianqi@foxmail.com', 0, 3, '2020-05-14');
INSERT INTO `employee` VALUES (10, '王伟', 'wangwei@gmail.com', 1, 3, '2020-05-08');
INSERT INTO `employee` VALUES (11, '张伟', 'zhangwei@gmail.com', 1, 2, '2020-05-11');
INSERT INTO `employee` VALUES (12, '李伟', 'liwei@gmail.com', 1, 3, '2020-05-18');
COMMIT;
```
#### 基本环境搭建
1. 新建Spring项目, 添加Lombok,Spring Web,Thymeleaf,Mybatis,MySQL Driver的支持
2. 相关的pom依赖
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
```
3. 建立基本结构和配置框架
`com/`
`|-- godfrey
|-- config
|-- controller
|-- dto
|-- mapper
|-- pojo
|-- service`
4. application.yml里配置数据库连接信息及Mapper映射文件信息
```yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/employee?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: employee
password: employee123
thymeleaf:
cache: false
messages:
basename: i18n.login
mybatis:
type-aliases-package: com.godfrey.pojo
mapper-locations: classpath:com.godfrey.mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
```
5. 测试数据库连接
```java
package com.godfrey;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class ApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println("数据源>>>>>>" + dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println("连接>>>>>>>>>" + connection);
System.out.println("连接地址>>>>>" + connection.getMetaData().getURL());
connection.close();
}
}
```
**查看输出结果,数据库配置ok**
#### 创建pojo实体类
1. 创建User实体
```java
package com.godfrey.pojo;
import lombok.Data;
/**
* description : 登录验证实体类
*
* @author godfrey
* @since 2020-05-26
*/
@Data
public class User {
private Integer id;
private String userName;
private String password;
}
```
2. 创建Department实体
```java
package com.godfrey.pojo;
import lombok.Data;
/**
* description : 部门实体类
*
* @author godfrey
* @since 2020-05-26
*/
@Data
public class Department {
private Integer id;
private String departmentName;
}
```
3. 创建Employee实体
```java
package com.godfrey.pojo;
import lombok.Data;
import java.sql.Date;
/**
* description : 员工实体类
*
* @author godfrey
* @since 2020-05-26
*/
@Data
public class Employee {
private Integer id;
private String employeeName;
private String email;
private Integer gender; //0:女 1:男
private Date date;
private Integer departmentId;
}
```
4. 创建EmployeeDTO实体
```java
package com.godfrey.dto;
import lombok.Data;
import java.sql.Date;
/**
* description : EmployeeDTO
*
* @author godfrey
* @since 2020-05-26
*/
@Data
public class EmployeeDTO {
private Integer id;
private String employeeName;
private String email;
private Integer gender;
private String departmentName;
private Date date;
}
```
#### Mapper层
文件存放目录:
com.godfrey.mapper 相关接口
resources/com.godfrey.mapper 相关mapper.xml
1. 编写User的Mapper接口:UserMapper
```java
package com.godfrey.mapper;
import com.godfrey.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
* description : 登录验证查询Mapper
*
* @author godfrey
* @since 2020-05-26
*/
@Mapper
@Repository
public interface UserMapper {
User selectPasswordByName(@Param("userName") String userName, @Param("password") String password);
}
```
2. 编写接口对应的Mapper.xml文件:UserMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.godfrey.mapper.UserMapper">
<select id="selectPasswordByName" resultType="User">
select * from employee.user where user_name = #{userName} and password = #{password};
</select>
</mapper>
```
3. 编写Department的Mapper接口:DepaertmentMapper
```java
pac