当前位置:网站首页>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 people     
    
  • These 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 field 
    
  • advantage

    • 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

    1. Create a class It creates ThreadLocal And write get/set Method
    2. Because every modification or update will pass through my filter , So you can get the current user's login first through the filter id
    3. 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);
    }
原网站

版权声明
本文为[C_ x_ three hundred and thirty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111819132104.html