当前位置:网站首页>Do you know that public fields are automatically filled in
Do you know that public fields are automatically filled in
2022-06-11 18:37:00 【C_ x_ three hundred and thirty】
The public fields are automatically populated
🥨 What is a public field ?
The new employees we are adding are , You need to set the creation time , Modification time , Modifier, etc We also need to set the creation time when editing employees , Modification time , The same operations as modifying peopleThese fields are public fields
🥨 In order to solve the problem of repeating these operations every time , Is there a quick and efficient way to simplify our development 🥪?
The answer is yes We can use MybatisPlus The public field auto filling function provided
How to understand MybatisPlus Public fields are automatically filled ?
MybatisPlus Public fields are automatically filled when inserting or modifying , Assign the specified value to the specified fieldadvantage
- Unified processing of public fields
- Avoid duplicate code
Knowing the above knowledge, how should we implement it ?
1. On the attribute of the entity class, suppose @TableField, Specify auto population policy 2. Write metadata object processor , Assign values to public fields in this class , This class needs to implement MetaObjectHandler Interface
- 🥡 Step 1 code demonstration
@TableField(fill = FieldFill.INSERT)// Fill in fields when inserting private LocalDateTime createTime; @TableField(fill = FieldFill.INSERT_UPDATE)// Fill in fields when inserting and modifying private LocalDateTime updateTime; @TableField(fill = FieldFill.INSERT)// Fill in fields when inserting private Long createUser; @TableField(fill = FieldFill.INSERT_UPDATE)// Fill in fields when inserting and modifying private Long updateUser;
- 🥡 The second code demonstration
/** * Custom metadata object processor */ @Component // Give Way Spring To deal with it @Slf4j public class MyMetaObjectHandler implements MetaObjectHandler { /** * Insert operation automatically fills * @param metaObject */ @Override public void insertFill(MetaObject metaObject) { log.info(" The public fields are automatically populated [insert]"); metaObject.setValue("createTime", LocalDateTime.now()); metaObject.setValue("updateTime",LocalDateTime.now()); metaObject.setValue("createUser",1L); metaObject.setValue("updateUser",1L); } /** * Update operation automatically populates * @param metaObject */ @Override public void updateFill(MetaObject metaObject) { log.info(" The public fields are automatically populated [update]"); metaObject.setValue("updateTime",LocalDateTime.now()); metaObject.setValue("updateUser",1L); } }
- I wonder if you have found a problem ?
- I set this ID It is set to be fixed , It must be wrong
- Think about it , How did we get ID Of ?
- Is it through Request.getSession.getAttritube(“employee”) By key or id It's worth it
- But there is no way to get Request
- So the previous method is invalid here
🥡** So how should we solve it ?**
ThreadLocal Each time the customer service sends http request , The corresponding server side will allocate a new thread to handle The thread obtained by the method in some classes is the same thread[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-sjmbM42j-1654173413602)(C:\Users\Cx_330\AppData\Roaming\Typora\typora-user-images\image-20220602194453002.png)]
🥡 To solve the above problem , We can go in three steps , This is based on what I wrote reggie Project presentations
- Create a class It creates ThreadLocal And write get/set Method
- Because every modification or update will pass through my filter , So you can get the current user's login first through the filter id
- And then to updateFill() In the method, pass again get Method to get id You can set up
/** * be based on ThreadLocal Build tool classes Used to save and obtain the current logged in user id */ public class BaseContext { private static ThreadLocal<Long> threadLocal= new ThreadLocal<Long>(); public static void setCurrnetId(Long id){ threadLocal.set(id); } public static Long getCurrentId(){ return threadLocal.get(); } }// If you need to deal with First judge the login status If you have landed The release if(request.getSession().getAttribute("employee")!=null){ Long empid = (Long) request.getSession().getAttribute("employee"); BaseContext.setCurrnetId(empid); filterChain.doFilter(request,response); return; }public void updateFill(MetaObject metaObject) { log.info(" The public fields are automatically populated [update]"); Long empId = BaseContext.getCurrentId(); metaObject.setValue("updateTime",LocalDateTime.now()); metaObject.setValue("updateUser",empId); }
边栏推荐
猜你喜欢

TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生

Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case

Balanced search binary tree -- AVL tree

Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots

全志T3开发板(4核ARM Cortex-A7)——系统启动阶段LOGO显示详解

金融银行_催收系统简介

Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case

为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由

Quanzhi Technology T3 Development Board (4 Core ARM Cortex - A7) - mqtt Communication Protocol case

炫酷的可视化工具:processing 初识
随机推荐
实现可以继续上局
学习使用LSTM和IMDB评论数据进行情感分析训练
使用mysql判断日期是星期几
Easycwmp source code analysis
合并多棵二叉搜索树
[c language] output the students within the specified score range with the structure
Feign shares login information for request
Force deduction 23 questions, merging K ascending linked lists
On the sequence traversal of binary tree Ⅱ
VIM common commands
高并发架构设计
SAP BTP 上 workflow 和 Business Service 的 project 管理
V-for loop traversal
Swagger2简单使用
System learning typescript (V) - joint type
[c language] output students' names and scores in descending order of scores with structures
Learn to use LSTM and IMDB comment data for emotion analysis training
牛客刷题——合法括号序列判断
MMA-Self-defining function
Balanced search binary tree -- AVL tree