1.新增员工
@PostMapping
@ApiOperation(value = "新增员工")
public Result<Employee> save(@RequestBody EmployeeDTO employeeDTO){
log.info("新增员工:{}",employeeDTO);
System.out.println("当前线程的id:" + Thread.currentThread().getId());
employeeService.save(employeeDTO);
return Result.success();
}
新增员工,封装了对象EmployeeDTO
@Data
public class EmployeeDTO implements Serializable {
private Long id;
private String username;
private String name;
private String phone;
private String sex;
private String idNumber;
}
同时,数据库中存在创建人ID这个字段
public class BaseContext {
public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
public static void setCurrentId(Long id) {
threadLocal.set(id);
}
public static Long getCurrentId() {
return threadLocal.get();
}
public static void removeCurrentId() {
threadLocal.remove();
}
}
EmployeeService
void save(EmployeeDTO employeeDTO);
EmployeeServiceImpl
/**
* 新增员工
* @param employeeDTO
*/
@Override
public void save(EmployeeDTO employeeDTO) {
System.out.println("当前线程的id:" + Thread.currentThread().getId());
Employee employee = new Employee();
//对象拷贝
BeanUtils.copyProperties(employeeDTO,employee);
//设置账号的状态,默认正常状态 1表示正常 0表示锁定
employee.setStatus(StatusConstant.ENABLE);
//设置默认密码
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
//设置时间
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
//设置创建人id和更新人id
employee.setCreateUser(BaseContext.getCurrentId());
employee.setUpdateUser(BaseContext.getCurrentId());
employeeMapper.insert(employee);
}
EmployeeMapper.xml
/**
* 插入员工数据
* @param employee
*/
@Insert("insert into employee (name,username,password,phone,sex,id_number,create_time,update_time,create_user,update_user)" +
"values " +
"(#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{createTime},#{updateTime},#{createUser},#{updateUser})")
void insert(Employee employee);
2.员工分页查询
@ApiOperation(value = "员工分页查询")
@GetMapping("/page")
public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
log.info("员工分页查询,参数为:{}" + employeePageQueryDTO);
PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
return Result.success(pageResult);
}
在分页查询中,后端也封装了对象EmployeePageQueryDTO
@Data
public class EmployeePageQueryDTO implements Serializable {
//员工姓名
private String name;
//页码
private int page;
//每页显示记录数
private int pageSize;
}
EmployeeService
/**
* 员工分页查询
* @param employeePageQueryDTO
* @return
*/
PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
EmployeeServiceImpl
/**
* 员工分页查询
* @param employeePageQueryDTO
* @return
*/
@Override
public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
//select * from employee limit 0,10
//开始分页查询
PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
long total = page.getTotal();
List<Employee> records = page.getResult();
return new PageResult(total,records);
}
EmployeeMapper
/**
* 员工分页查询
* @param employeePageQueryDTO
* @return
*/
Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
EmployeeMapper.xml
<select id="pageQuery" resultType="com.sky.entity.Employee">
select * from employee
<where>
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
</where>
order by create_time desc
</select>
在分页查询中,后端封装了分页查询结果PageResult
/**
* 封装分页查询结果
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult implements Serializable {
private long total; //总记录数
private List records; //当前页数据集合
}
3.禁用启用员工账号
/**
* 启用禁用员工账号
* @param id
* @param status
* @return
*/
@ApiOperation(value = "启用禁用员工账号")
@PostMapping("/status/{status}")
public Result startOrStop(Long id,@PathVariable("status") Integer status){
log.info("启用禁用员工账号:{},{}",status,id);
employeeService.startOrStop(status,id);
return Result.success();
}
EmployeeService
/**
* 启用禁用员工账号
* @param status
* @param id
*/
void startOrStop(Integer status, Long id);
EmployeeServiceImpl
/**
* 启用禁用员工账号
* @param status
* @param id
*/
@Override
public void startOrStop(Integer status, Long id) {
// Employee employee = new Employee();
// employee.setStatus(status);
// employee.setId(id);
Employee employee = Employee.builder().status(status).id(id).build();
employeeMapper.update(employee);
}
EmployeeMapper
/**
* 根据主键动态修改属性
* @param employee
*/
void update(Employee employee);
EmployeeMapper.xml
<update id="update" parameterType="com.sky.entity.Employee">
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>
4.编辑员工信息
在编辑员工信息时,我们同时需要根据ID查询员工的信息进行回显
/**
* 编辑员工信息
* @param employeeDTO
* @return
*/
@ApiOperation("编辑员工信息")
@PutMapping
public Result update(@RequestBody EmployeeDTO employeeDTO){
employeeService.update(employeeDTO);
return Result.success();
}
@ApiOperation("根据ID查询员工信息")
@GetMapping("/{id}")
public Result<Employee> getById(@PathVariable(value = "id") Long id){
Employee employee = employeeService.getById(id);
return Result.success(employee);
}
EmployeeService
/**
* 根据ID查询员工信息
* @param id
* @return
*/
Employee getById(Long id);
/**
* 编辑员工信息
* @param employeeDTO
*/
void update(EmployeeDTO employeeDTO);
EmployeeServiceImpl
/**
* 根据ID查询员工信息
* @param id
* @return
*/
@Override
public Employee getById(Long id) {
Employee employee = employeeMapper.getById(id);
employee.setPassword("****");
return employee;
}
/**
* 编辑员工信息
* @param employeeDTO
*/
@Override
public void update(EmployeeDTO employeeDTO) {
Employee employee = new Employee();
BeanUtils.copyProperties(employeeDTO,employee);
//设置修改时间
employee.setUpdateTime(LocalDateTime.now());
//设置修改人ID
employee.setUpdateUser(BaseContext.getCurrentId());
employeeMapper.update(employee);
}
EmployeeMapper
/**
* 根据主键动态修改属性
* @param employee
*/
void update(Employee employee);
/**
* 根据ID查询员工信息
* @param id
* @return
*/
@Select("select * from employee where id = #{id}")
Employee getById(Long id);
EmployeeMapper.xml
<update id="update" parameterType="com.sky.entity.Employee">
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>