欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot實(shí)現(xiàn)公共字段填充的示例詳解

 更新時(shí)間:2023年10月05日 11:24:33   作者:xyy123  
這篇文章主要為大家詳細(xì)介紹了Springboot實(shí)現(xiàn)公共字段填充的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們深入學(xué)習(xí)springboot有一定的幫助,感興趣的小伙伴可以了解下

問(wèn)題分析

方式一:自定義注解AutoFill

創(chuàng)建注解

/**  
* 自定義注解,用于標(biāo)識(shí)某個(gè)方法需要進(jìn)行功能字段自動(dòng)填充處理  
*/  
@Target(ElementType.METHOD)  // 只能加載方法上
@Retention(RetentionPolicy.RUNTIME)  // 固定寫法 
public @interface AutoFill {  
    //枚舉,數(shù)據(jù)庫(kù)操作類型:UPDATE INSERT  
    OperationType value();  
}

創(chuàng)建枚舉

/**  
* 數(shù)據(jù)庫(kù)操作類型  
*/  
public enum OperationType {  
    /**  
    * 更新操作  
    */  
    UPDATE,  
    /**  
    * 插入操作  
    */  
    INSERT  
}

創(chuàng)建切面類

/**  
* 自定義切面,實(shí)現(xiàn)公共字段自動(dòng)填充處理邏輯  
*/  
@Aspect  
@Component  
@Slf4j  
public class AutoFillAspect {  
    /**  
    * 切入點(diǎn),指定攔截mapper的下帶有autofill注解的方法
    */  
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")  
    public void autoFillPointCut(){
    }  
    /**  
    * 前置通知,在通知中進(jìn)行公共字段的賦值  
    */  
    @Before("autoFillPointCut()")  
    public void autoFill(JoinPoint joinPoint){  
        log.info("開始進(jìn)行公共字段自動(dòng)填充...");  
        //獲取到當(dāng)前被攔截的方法上的數(shù)據(jù)庫(kù)操作類型  
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法簽名對(duì)象  
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//獲得方法上的注解對(duì)象  
        OperationType operationType = autoFill.value();//獲得數(shù)據(jù)庫(kù)操作類型  
        //獲取到當(dāng)前被攔截的方法的參數(shù)--實(shí)體對(duì)象  
        Object[] args = joinPoint.getArgs();  
        if(args == null || args.length == 0){  
        return;  
    }  
    Object entity = args[0];  
    //準(zhǔn)備賦值的數(shù)據(jù)  
    LocalDateTime now = LocalDateTime.now();  
    Long currentId = BaseContext.getCurrentId();  
    //根據(jù)當(dāng)前不同的操作類型,為對(duì)應(yīng)的屬性通過(guò)反射來(lái)賦值  
    if(operationType == OperationType.INSERT){  
        //為4個(gè)公共字段賦值  
        try {  
            Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);  
            Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);  
            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);  
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);  
            //通過(guò)反射為對(duì)象屬性賦值  
            setCreateTime.invoke(entity,now);  
            setCreateUser.invoke(entity,currentId);  
            setUpdateTime.invoke(entity,now);  
            setUpdateUser.invoke(entity,currentId);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }else if(operationType == OperationType.UPDATE){  
        //為2個(gè)公共字段賦值  
        try {  
            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);  
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);  
            //通過(guò)反射為對(duì)象屬性賦值  
            setUpdateTime.invoke(entity,now);  
            setUpdateUser.invoke(entity,currentId);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
 }  
}

mapper方法中添加注解

方式二:使用mybatis plus提供方法

實(shí)體類添加注解 @TableField(fill = FieldFill.XXX)

package com.example.demo.pojo;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
 * 員工信息
 * @TableName employee
 */
@TableName(value ="employee")
@Data
@Accessors(chain = true)
public class Employee implements Serializable {
    /**
     * 主鍵
     */
    @TableId
    private Long id;
    /**
     * 姓名
     */
    private String name;
    /**
     * 創(chuàng)建時(shí)間
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(fill = FieldFill.INSERT)//插入時(shí)填充字段
    private LocalDateTime createTime;
    /**
     * 更新時(shí)間
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(fill = FieldFill.INSERT_UPDATE)//插入、更新時(shí)填充字段,后面的是枚舉
    private LocalDateTime updateTime;
    /**
     * 創(chuàng)建人
     */
    @TableField(fill = FieldFill.INSERT)
    private Long createUser;
    /**
     * 修改人
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;
}

創(chuàng)建數(shù)據(jù)處理器

package com.example.demo.common;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.demo.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    HttpServletRequest request;
    @Autowired
    RedisUtils redisUtils;
    @Override
    public void insertFill(MetaObject metaObject) {
        //填充創(chuàng)建時(shí)間
        metaObject.setValue("createTime", LocalDateTime.now());
        //填充 更新的時(shí)間
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long result = BaseContext.get();
        //填充創(chuàng)建人信息
        metaObject.setValue("createUser",result);
        //填充更新人信息
        metaObject.setValue("updateUser",result);
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        //因?yàn)槭歉?,所以不用操作?chuàng)建時(shí)間
        //更新 更新的時(shí)間
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long result = BaseContext.get();;
        //填充更新人信息
        metaObject.setValue("updateUser",result);
    }
}

以上就是Springboot實(shí)現(xiàn)公共字段填充的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Springboot公共字段填充的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論