苍穹外卖项目学习日记(2) day02-1
新增员工
- 设计新增员工接口的DTO
- controller层添加save方法,并且使用service的save方法(自己根据提示新建),在emploee service实现层使用对象属性拷贝
- 设置账号状态,使用常量类,设置密码
- 设置当前时间,设置当前创建人和修改人id(后续更新优化)
- EmployeeServiceImpl.java
public void save(EmployeeDTO employeeDTO) {
Employee employee = new Employee();
BeanUtils.copyProperties(employeeDTO,employee);
employee.setStatus(StatusConstant.ENABLE);
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
employee.setCreateUser(10L);
employee.setUpdateUser(10L);
employeeMapper.insert(employee);
}
- 注:sql语言自动提示需要自己设置,教程如下:
https://blog.csdn.net/qq2523208472/article/details/89366264
,新版idea的Show parameter name hints
在File→settings → Editor →Inlay Hints
,勾选上sql就行,如果还是没出现提示,在输入sql语句时,按alt+enter
选择Language injection settings
,选择mysql,再次输入即可,后续的括号出现提示需要先按一下i
- mapper层添加函数和sql语句
- EmployeeMapper.java
@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);
代码完善
- 注册时,若注册信息的username在表中已经存在,将报错,所以需要在异常处理器中添加自定义异常处理该种情况
- GlobalExceptionHandler.java
@ExceptionHandler
public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
String message = ex.getMessage();
if(message.contains("Duplicate entry")){
String[] s = message.split(" ");
String username = s[2];
String msg = username+ MessageConstant.ALREADY_EXISTS;
return Result.error(msg);
}else {
return Result.error(MessageConstant.UNKNOWN_ERROR);
}
}
- 在service层从jwt令牌中获取用户id信息,使用threadlocal,它是每个线程的局部变量,注:客户端每次发的请求都是不同的线程
- 在拦截器中的jwt处,即JwtTokenAdminInterceptor.java文件下,在校验JWT中添加set方法,
BaseContext.setCurrentId(empId);
- 然后将service层的save方法,保存id处,即之前的todo处,修改为
employee.setCreateUser(BaseContext.getCurrentId()); employee.setUpdateUser(BaseContext.getCurrentId());
员工分页查询
- controller层添加分页查询函数,请求方式为get,路径为
/page
- EmployeeController.java
@GetMapping("/page")
@ApiOperation("员工分页查询")
public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
log.info("员工分页查询:{}",employeePageQueryDTO);
PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
return Result.success(pageResult);
}
- service层使用pagehelper插件进行分页
- 安装mybaitsX插件,可以在mapper层链接到xml文件,并且快速生成xml中sql语句
- service层添加pageQuery方法
- EmployeeServiceImpl.java
@Override
public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
return new PageResult(page.getTotal(),page.getResult());
}
- 更新mapper层,添加相应方法,并且更新xml文件
- EmployeeMapper.xml
<select id="pageQuery" resultType="com.sky.entity.Employee">
select * from employee
<where>
<if test="name != null and name != ''">
and name like contact('%',#{name},'%')
</if>
</where>
order by create_time desc
</select>
- 如果返回401,记得在swagger中更新token
- 日期显示有问题,解决方式两种:
- 1.实体类前添加
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
注解,
- 2.配置springmvc消息转换器
- WebMvcConfiguration.java
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
log.info("扩展消息转换器");
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new JacksonObjectMapper());
converters.add(0,converter);
}