需求分析 业务规则
根据页码展示员工信息
每页展示10条数据
分页查询时可以根据需要输入员工姓名进行查询
代码开发
根据分页查询的接口设计对应的DTO 设计 EmployeePageQueryDTO.class1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.sky.dto;import lombok.Data;import java.io.Serializable;@Data public class EmployeePageQueryDTO implements Serializable { private String name; private int page; private int pageSize; }
后面的所有分页查询都统一封装为 PageResult 对象1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.sky.result;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;import java.util.List;@Data @AllArgsConstructor @NoArgsConstructor public class PageResult implements Serializable { private long total; private List records; }
代码完善——日期
方式一: 在属性上加注解,对日期进行格式化————一个注解只能对一个属性进行处理1 2 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;
方式二: 在 WebMvcConfiguration 中扩展 Spring MVC 的消息转换器统一对日期进行处理.1 2 3 4 5 6 7 8 9 10 11 12 13 14 protected void extendMessageConverters (List<HttpMessageConverter<?>> converters) { log.info("开始扩展消息转换器..." ); MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter (); converter.setObjectMapper(new JacksonObjectMapper ()); converters.add(0 ,converter); }
启用禁用员工账号 业务规则
可以对状态为“启用”的员工账号进行“禁用”操作
可以对状态为“禁用”的员工账号进行“启用”操作
状态为“禁用”的员工不能登录系统
代码开发 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 @PostMapping("/status/{status}") @ApiOperation("启用禁用员工账号") public Result trartOrStop (@PathVariable Integer status,long id) { log.info("启用禁用员工账号:{},{}" ,status,id); employeeService.startOrStop(status,id); return Result.success(); } public void startOrStop (Integer status, long id) { Employee employee=Employee.builder() .id(id) .status(status) .build(); employeeMapper.update(employee); } public void startOrStop (Integer status, long id) { Employee employee=Employee.builder() .id(id) .status(status) .build(); employeeMapper.update(employee); }
编写动态sql语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <update id ="update" > update employee <set > <if test ="name != null" > name = #{name},</if > <if test ="username != null" > username = #{username},</if > <if test ="password != null" > password = #{password},</if > <if test ="phone != null" > phone = #{phone},</if > <if test ="sex != null" > sex = #{sex},</if > <if test ="idNumber != null" > id_Number = #{idNumber},</if > <if test ="updateTime != null" > update_Time = #{updateTime},</if > <if test ="updateUser != null" > update_User = #{updateUser},</if > <if test ="status != null" > status = #{status},</if > </set > where id = #{id} </update >
编辑员工信息 编辑员工功能涉及到的两个接口
根据id查询员工信息
编辑员工信息
代码开发 在 EmployeeController.java中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @GetMapping("/{id}") @ApiOperation("根据id查员工") public Result<Employee> getById (@PathVariable Long id) { Employee enployee=employeeService.getById(id); return Result.success(enployee); } @PutMapping @ApiOperation("更新员工信息") public Result update (@RequestBody EmployeeDTO employeeDTO) { log.info("编辑员工信息 {}" , employeeDTO); employeeService.update(employeeDTO); return Result.success(); }
在 EmployeeServiceImp.java 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public Employee getById (Long id) { Employee employee=employeeMapper.getById(id); employee.setPassword("*******" ); return employee; } public void update (EmployeeDTO employeeDTO) { Employee employee=new Employee (); BeanUtils.copyProperties(employeeDTO,employee); employeeMapper.update(employee); }
在 EmployeeMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @AutoFill(value = OperationType.UPDATE) void update (Employee employee) ; @Select("select * from employee where id = #{id}") Employee getById (Long id) ;