基于 Spring Boot 瑞吉外卖系统开发(八)

基于 Spring Boot 瑞吉外卖系统开发(八)

自动填充公共字段

MyBatis-Plus公共字段自动填充,也就是在插入或者更新的时候为指定字段赋予指定的值,使用它的好处就是可以统一对这些字段进行处理,降低了冗余代码的数量。本系统中有四个公共字段,需要在新增或更新时进行自动赋值操作。

使用MyBatis-Plus为公共字段自动填充时,需要在实体类的属性上加入@TableField注解,指定自动填充的策略,并编写元数据对象处理器,在类中统一为公共字段赋值。

自定义元数据对象处理器

在com.itheima.reggie.common包下自定义元数据对象处理器,在该元数据对象处理器中创建插入和更新操作时公共字段自动填充的方法。

java 复制代码
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    public HttpSession session;

    @Override
    public void insertFill(MetaObject metaObject) {
        Employee loginEmployee = (Employee) session.getAttribute("loginEmployee");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",loginEmployee.getId());
        metaObject.setValue("updateUser",loginEmployee.getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Employee loginEmployee = (Employee) session.getAttribute("loginEmployee");
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",loginEmployee.getId());
    }

}

删除冗余代码


相关推荐
ss27343 分钟前
基于Springboot + vue + 爬虫实现的高考志愿智能推荐系统
spring boot·后端·高考
两点王爷1 小时前
springboot项目文件上传到服务器本机,返回访问地址
java·服务器·spring boot·文件上传
pjx9872 小时前
质量的“试金石”:精通Spring Boot单元测试与集成测试
spring boot·spring·单元测试·集成测试
JavaDog程序狗5 小时前
【java】easypoi导出excel单元格,填充动态下拉列
java·spring boot·excel
呆萌很5 小时前
基于 Spring Boot 瑞吉外卖系统开发(九)
spring boot
Kings906 小时前
告别手动注入!Bean Assistant插件让SpringBoot开发效率翻倍🚀
spring boot
.生产的驴6 小时前
SpringBoot 接口国际化i18n 多语言返回 中英文切换 全球化 语言切换
java·开发语言·spring boot·后端·前端框架
-曾牛6 小时前
Spring Boot中@RequestParam、@RequestBody、@PathVariable的区别与使用
java·spring boot·后端·intellij-idea·注解·spring boot 注解·混淆用法
软件2056 小时前
【UserDetailsService】
spring boot