SpringBoot员工管理系统(整合Mybatis+mysql)
前部分:https://blog.csdn.net/weixin_43501359/article/details/112714664
成品:https://download.csdn.net/download/weixin_43501359/14928697
文章目录
1.导入依赖
pom.xml
<!--mybaits-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
2.配置数据源(datasource)和Mybatis配置
application.yaml
server:
port: 8089
spring:
messages:
basename: i18n.login
mvc:
format:
date: yyyy-MM-dd
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.cj.jdbc.Driver
#基本属性
url: jdbc:mysql://127.0.0.1:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.kuang.pojo
3.数据库构建文件
CREATE DATABASE mydatabase;
DROP TABLE IF EXISTS `Employee`;
CREATE TABLE `Employee` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lastname` varchar(50) NOT NULL DEFAULT '',
`email` varchar(50) DEFAULT '',
`gander` int DEFAULT NULL,
`departmentId` int(10) DEFAULT NULL,
`birth` DATE NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
BEGIN;
INSERT INTO `Employee` VALUES (1001, 'AA1', '24734674@qq.com', 1, 101, NOW());
INSERT INTO `Employee` VALUES (1002, 'AA2', '24734674@qq.com', 0, 102, NOW());
INSERT INTO `Employee` VALUES (1003, 'AA3', '24734674@qq.com', 1, 103, NOW());
INSERT INTO `Employee` VALUES (1004, 'AA4', '24734674@qq.com', 0, 104, NOW());
INSERT INTO `Employee` VALUES (1005, 'AA5', '24734674@qq.com', 1, 105, NOW());
COMMIT;
DROP TABLE IF EXISTS `Department`;
CREATE TABLE `Department` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`departmentName` varchar(50) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
BEGIN;
INSERT INTO `Department` VALUES (101,'教学部');
INSERT INTO `Department` VALUES (102,'市场部');
INSERT INTO `Department` VALUES (103,'教研部');
INSERT INTO `Department` VALUES (104,'运营部');
INSERT INTO `Department` VALUES (105,'后勤部');
COMMIT;
4.编写xxxxMapper接口类
EmployeeMapper.java
package com.kuang.dao;
import com.kuang.pojo.Employee;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
@Mapper
public interface EmployeeMapper {
//增加一位员工
void add(Employee employee);
//更新员工信息
void update(Employee employee);
//查询全部员工
Collection<Employee> getAll();
//通过ID查询员工
Employee getEmployeeById(Integer id);
//通过ID删除一位员工
void deleteEmployeeById(Integer id);
}
DepartmentMapper.java
package com.kuang.dao;
import com.kuang.pojo.Department;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
@Mapper
public interface DepartmentMapper {
//获取所有部门
Collection<Department> getDepartments();
//通过ID获取部门
Department getDepartmentById(Integer id);
}
5.编写xxxxMapper.xml配置类
EmployeeMapper.xml
<?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.kuang.dao.EmployeeMapper" >
<sql id="BASE_COLUMN">
lastname,email,gander,departmentId,birth
</sql>
<!--×××× 结果映射 -->
<resultMap id="EmployeeResultMap" type="Employee">
<id property="id" column="id"/>
<result property="lastName" column="lastname"/>
<result property="email" column="email"/>
<result property="gander" column="gander"/>
<result property="department.id" column="departmentId"/>
<result property="department.departmentName" column="departmentName"/>
<result property="birth" column="birth"/>
</resultMap>
<select id="getAll" resultMap="EmployeeResultMap">
SELECT * FROM employee,department WHERE employee.departmentId=department.id
</select>
<insert id="add" parameterType="Employee">
INSERT INTO employee
<trim prefix="(" suffix=")">
<include refid="BASE_COLUMN"/>
</trim>
<trim prefix="VALUES(" suffix=")">
#{lastName},#{email},#{gander},#{department.id},#{birth}
</trim>
</insert>
<update id="update" parameterType="Employee">
UPDATE employee
<set>
<if test="lastName != null">
lastname = #{lastName},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="gander != null">
gander = #{gander},
</if>
<if test="department.id != null">
departmentId = #{department.id},
</if>
<if test="birth != null">
birth = #{birth},
</if>
</set>
<where>
id=#{id}
</where>
</update>
<select id="getEmployeeById" parameterType="int" resultMap="EmployeeResultMap">
SELECT * FROM employee WHERE id=#{id}
</select>
<delete id="deleteEmployeeById" parameterType="int">
DELETE FROM employee WHERE id=#{id}
</delete>
</mapper>
DepartmentMapper.xml
<?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.kuang.dao.DepartmentMapper" >
<select id="getDepartments" resultType="Department">
SELECT * FROM department
</select>
<select id="getDepartmentById" resultType="Department" parameterType="int">
SELECT * FROM department WHERE id=#{id}
</select>
</mapper>
6.service层调用dao层
EmployeeService.java
package com.kuang.service;
import com.kuang.dao.EmployeeMapper;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
public class EmployeeService {
@Autowired
private EmployeeMapper employeeMapper; //报红不用管
//增加一位员工
public void add(Employee employee){
employeeMapper.add(employee);
}
//更新员工信息
public void update(Employee employee){
employeeMapper.update(employee);
}
//查询全部员工
public Collection<Employee> getAll(){
return employeeMapper.getAll();
}
//通过ID查询员工
public Employee getEmployeeById(Integer id){
return employeeMapper.getEmployeeById(id);
}
//通过ID删除一位员工
public void deleteEmployeeById(Integer id){
employeeMapper.deleteEmployeeById(id);
}
}
DepartmentService.java
package com.kuang.service;
import com.kuang.dao.DepartmentMapper;
import com.kuang.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
public class DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;//报红不用管
//获取所有部门
public Collection<Department> getDepartments(){
return departmentMapper.getDepartments();
}
//通过ID获取部门
public Department getDepartmentById(Integer id){
return departmentMapper.getDepartmentById(id);
}
}
7.Controller层调用Service层
EmployeeController.java
package com.kuang.controller;
import com.kuang.dao.DepartmentMapper;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import com.kuang.service.DepartmentService;
import com.kuang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.Collection;
@Controller
public class EmployeeController {
@Autowired
private DepartmentService departmentService;
@Autowired
private EmployeeService employeeService;
@RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeService.getAll();
model.addAttribute("emps",employees);
return "emp/list";
}
@GetMapping("/emp")
public String toAddpage(Model model){
//查出所有部门
Collection<Department> departments = departmentService.getDepartments();
model.addAttribute("departments",departments);
return "emp/add";
}
@PostMapping("/emp")
public String Add(Employee employee){
//添加操作
System.out.println(employee.toString());
employeeService.add(employee);
return "redirect:/emps";
}
@GetMapping("/emp/{id}")
public String toUpdate(@PathVariable("id") Integer id,Model model){
//查出原来的数据
Employee employee = employeeService.getEmployeeById(id);
model.addAttribute("emp",employee);
//查出所有部门
Collection<Department> departments = departmentService.getDepartments();
System.out.println(departments.toString());
model.addAttribute("departments",departments);
return "emp/update";
}
@PostMapping("/emp/update")
public String Update(Employee employee){
employeeService.update(employee);
return "redirect:/emps";
}
@RequestMapping("/emp/delete/{id}")
public String Delete(@PathVariable("id") Integer id){
employeeService.deleteEmployeeById(id);
return "redirect:/emps";
}
@RequestMapping("/out")
public String loginOut(HttpSession session){
session.removeAttribute("loginUser");
return "redirect:/";
}
}