4、员工管理页面
4.1、查询全部员工
DAO层
- 编写EmployeeMapper
package com.bobooy.ems.mapper;
import com.bobooy.ems.pojo.Employee;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.Collection;
/**
* @author BoBooY
* @date 2022/9/26 23:23
*/
@Mapper
@Repository
public interface EmployeeMapper {
//查询全体员工信息
public Collection<Employee> getAll();
}
- 编写EmployeeMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bobooy.ems.mapper.EmployeeMapper">
<select id="getAll" resultMap="EmployeeDepartment">
select e.id eid, e.last_Name eName, e.email email, e.gender gender,d.id did,e.birth birth, d.department_name dName
from employee e
join department d
on e.department = d.id
</select>
<resultMap id="EmployeeDepartment" type="Employee">
<result property="id" column="eid"/>
<result property="lastName" column="eName"/>
<result property="email" column="email"/>
<result property="gender" column="gender"/>
<result property="birth" column="birth"/>
<association property="department" javaType="Department">
<result property="id" column="did"/>
<result property="departmentName" column="dName"/>
</association>
</resultMap>
</mapper>
service层
编写EmployeeService
package com.bobooy.ems.service; import com.bobooy.ems.mapper.EmployeeMapper; import com.bobooy.ems.pojo.Employee; import org.springframework.stereotype.Service; import java.util.Collection; /** * @author BoBooY * @date 2022/9/26 23:44 */ public interface EmployeeService { //查询全体员工信息 public Collection<Employee> getAll(); }
- EmployeeServiceImpl
package com.bobooy.ems.service; import com.bobooy.ems.mapper.EmployeeMapper; import com.bobooy.ems.pojo.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Collection; /** * @author BoBooY * @date 2022/9/26 23:45 */ @Service public class EmployeeServiceImpl implements EmployeeService{ @Override public Collection<Employee> getAll() { return employeeMapper.getAll(); } }
controller层
- 编写EmployeeContoller
package com.bobooy.ems.controller; import com.bobooy.ems.mapper.EmployeeMapper; import com.bobooy.ems.pojo.Employee; import com.bobooy.ems.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.RequestMapping; import java.util.Collection; /** * @author BoBooY * @date 2022/9/26 23:20 */ @Controller public class EmployeeController { @Autowired private EmployeeService employeeService; @RequestMapping("/emps") public String list(Model model) { Collection<Employee> employees = employeeService.getAll(); model.addAttribute("emps",employees); return "emp/list"; } }
测试
4.2、添加员工
4.2.1、跳转添加页面
DAO层
- 编写 DepartmentMapper
package com.bobooy.ems.mapper;
import com.bobooy.ems.pojo.Department;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.Collection;
/**
* @author BoBooY
* @date 2022/9/27 0:01
*/
@Mapper
@Repository
public interface DepartmentMapper {
//获取所有部门的信息
Collection<Department> getDepartments();
// 通过ID查询部门
Department getDepartmentById(Integer id);
}
- DepartmentMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bobooy.ems.mapper.DepartmentMapper">
<select id="getDepartments" resultType="Department">
select * from department
</select>
<select id="getDepartmentById" resultType="Department" parameterType="Integer">
select * from department where id = #{id}
</select>
</mapper>
- 在 application.yaml中开启 Myabtis 驼峰命名映射 注意空格
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
Service层
- DepartmentService
package com.bobooy.ems.service;
import com.bobooy.ems.pojo.Department;
import java.util.Collection;
/**
* @author BoBooY
* @date 2022/10/2 10:31
*/
public interface DepartmentService {
//获取所有部门的信息
Collection<Department> getDepartments();
// 通过ID查询部门
Department getDepartmentById(Integer id);
}
- DepartmentServiceImpl
package com.bobooy.ems.service;
import com.bobooy.ems.mapper.DepartmentMapper;
import com.bobooy.ems.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
/**
* @author BoBooY
* @date 2022/10/2 10:32
*/
@Service
public class DepartmentServiceImpl implements DepartmentService{
@Autowired
private DepartmentMapper departmentMapper;
@Override
public Collection<Department> getDepartments() {
return departmentMapper.getDepartments();
}
@Override
public Department getDepartmentById(Integer id) {
return departmentMapper.getDepartmentById(id);
}
}
Controller层
- 在EmployeeController中处理 ”/add“请求
@Autowired
private DepartmentService departmentService;
@GetMapping("/add")
public String add(Model model) {
Collection<Department> departments = departmentService.getDepartments();
model.addAttribute("depts",departments);
System.out.println(departments);
return "emp/add";
}
测试
4.2.2、处理添加员工请求
DAO层
- 编写EmployeeMapper
//添加员工
public int save(Employee employee);
- EmployeeMapper.xml
<insert id="save" parameterType="Employee">
insert into employee(`last_name`, `email`, `gender`, `department`, `birth`)
values (#{lastName}, #{email}, #{gender}, #{department.id}, #{birth})
</insert>
service层
- EmployeeService
//添加员工
public int save(Employee employee);
- EmployeeServiceImpl
@Override
public int save(Employee employee) {
return employeeMapper.save(employee);
}
controller层
- EmployeeController
//添加员工
@RequestMapping("/addEmp")
public String addEmp(Employee employee) {
int save = employeeService.save(employee);
if(save > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
return "redirect:/emps";
}
设置日期格式
- application.yaml(注意空格)
#日期格式配置
mvc:
format:
date: yyyy-MM-dd HH:mm
测试:添加员工
成功!
4.3、修改员工
Dao层
- 编写 EmployeeMapper
//修改员工
public int updateEmployee(Employee employee);
// 通过ID查询员工
public Employee getEmployee(Integer id);
- EmployeeMapper.xml
<select id="getEmployee" parameterType="int" resultMap="EmployeeDepartment">
select e.id eid, e.last_Name eName, e.email email, e.gender gender,d.id did,e.birth birth, d.department_name dName
from employee e
join department d
on e.department = d.id
where e.id = #{id}
</select>
<update id="updateEmployee" parameterType="Employee">
update employee set `last_name`=#{lastName},`email`=#{email},`gender`=#{gender},`department`=#{department.id},`birth`=#{birth} where `id` = #{id}
</update>
service层
- EmployeeService
// 通过ID查询员工
public Employee getEmployee(Integer id);
//修改员工
public int updateEmployee(Employee employee);
- EmployeeServiceImpl
@Override
public Employee getEmployee(Integer id) {
return employeeMapper.getEmployee(id);
}
@Override
public int updateEmployee(Employee employee) {
return employeeMapper.updateEmployee(employee);
}
Controller层
- EmployeeController
//跳转修改页面
@GetMapping("/update/{id}")
public String update(@PathVariable("id") int id,Model model) {
Employee employee = employeeService.getEmployee(id);
model.addAttribute("emp",employee);
Collection<Department> departments = departmentService.getDepartments();
model.addAttribute("departments",departments);
return "emp/update";
}
//修改员工信息
@PostMapping("/updateEmp")
public String update(Employee employee) {
int update = employeeService.updateEmployee(employee);
if(update > 0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
return "redirect:/emps";
}
测试:修改id为 1012 的员工部门为 后勤部 性别为男
- 修改前:
- 修改页面:
- 修改后:
成功!
4.4、删除员工
Dao层
- EmployeeMapper
// 删除员工
public int delete(Integer id);
- EmployeeMapper.xml
<delete id="delete" parameterType="int">
delete from employee where `id` = #{id}
</delete>
service层
- EmployeeService
// 删除员工
public int delete(Integer id);
- EmployeeServiceImpl
@Override
public int delete(Integer id) {
return employeeMapper.delete(id);
}
controller层
- EmployeeController
//删除员工
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") int id) {
int delete = employeeService.delete(id);
if(delete > 0)
System.out.println("删除成功!");
else {
System.out.println("删除失败!");
}
return "redirect:/emps";
}
测试:删除id为1011的员工
- 删除前
- 删除后
5、退出登录
- UserController
//退出登录
@RequestMapping("/logout")
public String logout(HttpSession session) {
session.invalidate(); //使当前session失效
return "redirect:/index.html";
}
- 测试:退出后 直接访问登录页面