返回添加成功的信息
//新增员工
@PostMapping
private R save(HttpServletRequest request,@RequestBody Employee employee){
//同一设定初始密码MD5加密
employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//设置创建时间
employee.setCreateTime(LocalDateTime.now());
// //更新时间
employee.setUpdateTime(LocalDateTime.now());
//创建人
Long employee1 = (Long)request.getSession().getAttribute("employee");
employee.setCreateUser(employee1);
employee.setUpdateUser(employee1);
//数据库添加
employeeService.save(employee);
return R.success("添加成功");
当出现账号相同的情况,会报sql异常
Duplicate entry ‘baixiaoyun’ for key ‘employee.idx_username’] with root cause
处理方法:
定义一个异常处理器捕获全局的异常进行处理
//全局异常处理
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
public class GloableExceptionHandler {
//捕获异常进行处理
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public R<String> exception(SQLIntegrityConstraintViolationException ex){
//判断异常信息里面是否是Duplicate entry(名字重复)
if (ex.getMessage().contains("Duplicate entry")){
// 异常信息:Duplicate entry 'admin' for key 'employee.idx_username'
String msg=ex.getMessage().split(" ")[2]+"已存在";
return R.error(msg);
}
return R.error("未知错误");
}
}


并且按照更新的时间进行排序
@Configuration
//配置MybatisPlus分页插件
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
//分页查询
@GetMapping("/page")
public R<Page> page( int page,int pageSize,String name){
//配置分页构造器
//当前页,当前页的大小
Page pageInfo=new Page(page,pageSize);
//条件过滤器
LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
queryWrapper.like(name!=null,Employee::getName,name);
//添加排序条件--更新时间
queryWrapper.orderByDesc(Employee::getUpdateTime);
employeeService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}
//修改员工信息
@PutMapping
public R update(HttpServletRequest request,@RequestBody Employee employee){
employee.setUpdateTime(LocalDateTime.now());
Long employeeId=(Long)request.getSession().getAttribute("employee");
employee.setUpdateUser(employeeId);
employeeService.updateById(employee);
return R.success("员工信息修改成功");
}
但是:当我们点击禁用,账号并没有被禁用
原因:


js对long型数据进行处理时会导致丢失精度,导致提交的id和数据库的id不一致
解决办法:
long类型数据统一转为String字符串
导入一个对象映射器:
/**
* 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
*/
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注册功能模块 例如,可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}
在WebMvcConfig类(继承WebMvcConfigurationSupport)中
//扩展MVC框架的消息转换器
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter();
//设置对象转换器,底层使用Jackson将对象转为JSON
mappingJackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
//将上面的消息转换器对象追加到mvc框架的转换器集合中
converters.add(0,mappingJackson2HttpMessageConverter);
}
1.回显数据
根据id查询数据
//根据id查询员工信息
@GetMapping("/{id}")
public R<Employee> getById(@PathVariable Long id){
Employee byId = employeeService.getById(id);
if (byId!=null){
return R.success(byId);
}
return R.error("没有查询到员工信息");
}
2.修改数据
直接会调用
//修改员工信息
@PutMapping
public R update(HttpServletRequest request,@RequestBody Employee employee)
进行修改
由于每次更新和新增都需要设置时间和更新人的id,代码重复
解决办法:
实现步骤:
@TableField(fill = FieldFill.INSERT)//插入时填充字段
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)//插入和更新填充字段
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//元数据对象处理器
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime",LocalDateTime.now());
metaObject.setValue("createUser",new Long(1));//先默认设置为1,后期优化
metaObject.setValue("updateUser",new Long(1));
}
@Override
public void updateFill(MetaObject metaObject) {
}
}
将添加和修改方法中有关更新时间和修改人的id的部分删除即可
优化公共字段填充
ThreadLocal:
ThreadLocal并不是一个Thread,而是Thread的局部变量。当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每个线程都可以独立的改变自己的副本而不会影响其他线程对应的副本。TheadLocal为每个线程提供单独的一份存储空间,具有线程隔离的效果,只有在线程内才能获取对应的值,线程外则不能访问
实现步骤:
//基于ThreadLocal封装工具类,用保存和获取当前登陆的id
public class BaseContext {
private static ThreadLocal<Long> threadLocal=new ThreadLocal<>();
public static void setCuttrntid(Long id){
threadLocal.set(id);
}
public static Long getCuttrntid(){
return threadLocal.get();
}
}
//元数据对象处理器
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime",LocalDateTime.now());
metaObject.setValue("createUser",BaseContext.getCuttrntid());
metaObject.setValue("updateUser",BaseContext.getCuttrntid());
}
@Override
public void updateFill(MetaObject metaObject) {
}
}