一:编写控制器EmployeeController
/**
* 禁用员工账号
*
* @return
*/
@PostMapping("/status/{status}")
@ApiOperation("启用禁用员工账号")
public Result startOrStop(@PathVariable Integer status, Long id) {
log.info("启用禁用员工账号:{},{}", status, id);
employeeService.startOrStop(status,id);
return Result.success();
}
public Result startOrStop(@PathVariable Integer status,Long id)
方法名:startOrStop,通过路径@PostMapping("/status/{status}")传递参数,加上@PathVariable注解,id通过地址栏,只需要保证参数名一致。
employeeService.startOrStop(status,id);
二:调用接口employeeService方法,创建startOrStop方法。
EmployeeServiceImpl
传统写法
@Override
public void startOrStop(Integer status, Long id) {
//update employee set status=?where id=?
Employee employee = new Employee();
employee.setStatus(status);
employee.setId(id);
employeeMapper.update(employee);
}
在实体类加入@Builder注解(链式构建)
@Override
public void startOrStop(Integer status, Long id) {
//update employee set status=?where id=?
// Employee employee = new Employee();
// employee.setStatus(status);
// employee.setId(id);
Employee employee = Employee.builder()
.status(status)
.id(id)
.build();
employeeMapper.update(employee);
}
在mapper接口扩展update方法,在映射文件编写
EmployeeMapper.xml
<update id="update" parameterMap="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="update_time != null">update_time = #{updateTime},</if>
<if test="update_user != null">update_user = #{updateUser},</if>
<if test="status != null">status = #{status}</if>
</set>
where id=#{id}
</update>
三、代码编写完成,启动调试
成功更改状态为0(禁用)