Controller层:
@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
//根据ID修改员工数据
@PutMapping()
public Result update(@RequestBody Emp emp){
log.info("修改员工数据:{}", emp);
empService.update(emp);
return Result.success();
}
}
Service接口层:
public interface EmpService {
/**
* 修改员工数据
*/
void update(Emp emp);
}
Service接口实现层:
@Service
public class EmpServiceImpl implements EmpService{
@Autowired
private EmpMapper empMapper;
@Override
public void update(Emp emp) {
empMapper.update(emp.getUsername(), emp.getName(), emp.getGender(), emp.getImage(), emp.getId(), LocalDateTime.now());
}
}
Mapper接口层:
@Mapper
public interface EmpMapper {
/**
* 根据ID修改员工数据
*/
@Update("update emp set username = #{username},name = #{name},gender=#{gender},update_Time=#{updateTime} ,image=#{image} where id=#{id}")
void update(String username, String name, Short gender, String image, Integer id, LocalDateTime updateTime);
}
Controller层(相同):
@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
//根据ID修改员工数据
@PutMapping()
public Result update(@RequestBody Emp emp){
log.info("修改员工数据:{}", emp);
empService.update(emp);
return Result.success();
}
}
public interface EmpService {
/**
* 修改员工数据
*/
void update(Emp emp);
}
Service接口实现类:
@Service
public class EmpServiceImpl implements EmpService{
@Autowired
private EmpMapper empMapper;
//使用映射文件xml
@Override
public void update(Emp emp){
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
}
}
Mapper层:
@Mapper
public interface EmpMapper {
//使用映射文件xml的方式
void update(Emp emp);
}
Mapper映射文件xml:
update emp
username = #{username},
password = #{passwprd},
name =#{name},
gender=#{gender},
image =#{image},
job=#{job},
entrydate=#{entrydate},
dept_id=#{deptId},
update_time =#{updateTime}
where id=#{id}
Mapper映射文件xml的路径要求如下: