Springboot實(shí)現(xiàn)公共字段填充的示例詳解
問(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)文章
JDK、J2EE、J2SE、J2ME四個(gè)易混淆概念區(qū)分
這篇文章將向你詳細(xì)介紹JDK、J2EE、J2SE、J2ME的概念以及他們的關(guān)系區(qū)別。2015-09-09java 多線程饑餓現(xiàn)象的問(wèn)題解決方法
這篇文章主要介紹了java 多線程饑餓現(xiàn)象的問(wèn)題解決方法的相關(guān)資料,需要的朋友可以參考下2017-06-06java中java.util.Date和java.sql.Date之間的轉(zhuǎn)換的示例
java.util.Date是java.sql.Date的父類,有時(shí)候在和SqlServer數(shù)據(jù)庫(kù)打交道時(shí),也會(huì)遇到,本文主要介紹了java中java.util.Date和java.sql.Date之間的轉(zhuǎn)換的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄(思路詳解)
這篇文章主要介紹了springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04SpringCloud服務(wù)實(shí)現(xiàn)同時(shí)使用eureka和nacos方法
這篇文章主要介紹了SpringCloud服務(wù)實(shí)現(xiàn)同時(shí)使用eureka和nacos方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)
這篇文章主要介紹了Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04關(guān)于Spring中@Transactional事務(wù)回滾的注意事項(xiàng)
這篇文章主要介紹了關(guān)于Spring中@Transactional事務(wù)回滾的注意事項(xiàng),回滾(Rollback)指的是程序或數(shù)據(jù)處理錯(cuò)誤,將程序或數(shù)據(jù)恢復(fù)到上一次正確狀態(tài)的行為?;貪L包括程序回滾和數(shù)據(jù)回滾等類型,需要的朋友可以參考下2023-05-05基于mybatis中test條件中單引號(hào)雙引號(hào)的問(wèn)題
這篇文章主要介紹了基于mybatis中test條件中單引號(hào)雙引號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01