1.新增员工
/**
* 处理SQL异常
* @param sqlIntegrityConstraintViolationException
* @return
*/
@ExceptionHandler
public Result exceptionHandler(SQLIntegrityConstraintViolationException sqlIntegrityConstraintViolationException){
//
String message = sqlIntegrityConstraintViolationException.getMessage();
if (message.contains("Duplicate entry")){
String[] split = message.split(" ");
String username = split[2];
String msg = username + MessageConstant.ALREADY_EXISTS;
return Result.error(msg);
}else {
return Result.error(MessageConstant.UNKNOWN_ERROR);
}
}
新增员工的代码
2.员工分页查询
2.1需求分析
2.2代码开发
在这里已经把当前的page对象存储到线程里,
然后后续将页码和每页记录数动态拼接到sql语句中
2.3代码完善
/**
* 扩展sqringmvc的消息转换器
* @param converters
*/
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建一个消息转换器
MappingJackson2CborHttpMessageConverter converter = new MappingJackson2CborHttpMessageConverter();
//需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为Json数据
converter.setObjectMapper(new JacksonObjectMapper());
//将自己的消息转换器加入到容器里
converters.add(0,converter);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.EmployeeMapper">
<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>
</mapper>
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.sky.entity
configuration:
#开启驼峰命名
map-underscore-to-camel-case: true
员工分页查询代码
3.启用禁用员工账号
3.1需求分析
3.2代码开发
启用禁用账号代码
4.编辑员工信息
4.1需求分析
4.2代码开发
/**
* 根据id查询员工信息
* @param id
* @return
*/
@GetMapping("/{id}")
@ApiOperation("根据id查询员工信息")
public Result<Employee> queryById(@PathVariable Long id){
Employee employee=employeeService.queryById(id);
return Result.success(employee);
}
/**
* 根据id查询员工信息
* @param id
* @return
*/
@Select("select * from employee where id =#{id}")
Employee queryById(Long id);
编辑员工信息代码