SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn)
思路:
利用的是SpringBoot的Aop思想和自定義注解和反射機(jī)制的方法來實(shí)現(xiàn)
項(xiàng)目中我涉及公共字段的有createTime、updateTime、createUser、updateUser
步驟:
1. 自定義注解AutoFill,用于標(biāo)識需要進(jìn)行公共字段自動填充的方法
/**
* 數(shù)據(jù)庫操作類型 使用的是枚舉方法
*/
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}/**
* 自定義注解,用于標(biāo)識某個方法需要進(jìn)行功能字段自動填充處理
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//數(shù)據(jù)庫操作類型:UPDATE INSERT
OperationType value();
}2. 自定義切面類AutoFillAspect,統(tǒng)一攔截加入了AutoFill注解的方法,通過反射為公共字段賦值
/**
* 自定義切面,實(shí)現(xiàn)公共字段字段填充處理邏輯
*/
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
/**
* 切入點(diǎn)
*/
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut(){}
/**
* 前置通知
*/
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint){
log.info("開始進(jìn)行公共字段的填充...");
//獲取到當(dāng)前被攔截方法上的數(shù)據(jù)庫操作類型
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
OperationType type = autoFill.value();
//獲取到當(dāng)前被攔截方法的參數(shù)--實(shí)體對象
Object[] args = joinPoint.getArgs();
if(args == null || args.length!=0){
return;
}
Object enity = args[0];
//準(zhǔn)備賦值的數(shù)據(jù)
Long id = BaseContext.getCurrentId();
LocalDateTime localDateTime = LocalDateTime.now();
//根據(jù)當(dāng)前不同的操作類型,為對應(yīng)額屬性通過反射來賦值
if(type == OperationType.INSERT){
//為四個公共字段賦值
try {
Method setCreateTime = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setCreateUser = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setCreateTime.invoke(enity,localDateTime);
setUpdateTime.invoke(enity,localDateTime);
setCreateUser.invoke(enity,id);
setUpdateUser.invoke(enity,id);
} catch (Exception e) {
e.printStackTrace();
}
}else if(type == OperationType.UPDATE){
//為兩個公共字段賦值
try {
Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
setUpdateTime.invoke(enity,localDateTime);
setUpdateUser.invoke(enity,id);
} catch (Exception e) {
e.printStackTrace();
}
}
}3. 在Mapper的需要自動填充公共字段的方法上加入AutoFill注解
@Insert("insert into category (type, name, sort, status, create_time, update_time, create_user, update_user) " +
"values (#{type},#{name},#{sort},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser});")
@AutoFill(value = OperationType.INSERT)
void save(Category category);
@Delete("delete from category where id = #{id}")
void deleteById(Long id);
@AutoFill(value = OperationType.UPDATE)
void update(Category category);到此這篇關(guān)于SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot公共字段填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot Mybatis Plus公共字段自動填充功能
- Mybatis plus的自動填充與樂觀鎖的實(shí)例詳解(springboot)
- SpringBoot實(shí)現(xiàn)字段自動填充的兩種方式
- Springboot實(shí)現(xiàn)公共字段填充的示例詳解
- SpringBoot ThreadLocal實(shí)現(xiàn)公共字段自動填充案例講解
- 使用EasyExcel根據(jù)模板導(dǎo)出文件方式
- SpringBoot實(shí)現(xiàn)公共字段自動填充的方法步驟
- SpringBoot基于MyBatisPlus實(shí)現(xiàn)公共字段自動填充
相關(guān)文章
基于@ConfigurationProperties的三種用法及說明
SpringBoot配置屬性綁定詳解,介紹@Component若您配置類、@ConfigurationProperties綁定屬性及三種常見應(yīng)用場景,附帶代碼示例與測試步驟2026-06-06
SpringBoot中自定義注解實(shí)現(xiàn)控制器訪問次數(shù)限制實(shí)例
本篇文章主要介紹了SpringBoot中自定義注解實(shí)現(xiàn)控制器訪問次數(shù)限制實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Apache DolphinScheduler完全設(shè)置東八區(qū)時區(qū)
這篇文章主要為大家介紹了Apache DolphinScheduler完全設(shè)置東八區(qū)配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
java觀察者模式的三種實(shí)現(xiàn)方式代碼實(shí)例
這篇文章主要介紹了java觀察者模式的三種實(shí)現(xiàn)方式代碼實(shí)例,觀察者模式(又被稱為發(fā)布-訂閱(Publish/Subscribe)模式,屬于行為型模式的一種,它定義了一種一對多的依賴關(guān)系,讓多個觀察者對象同時監(jiān)聽某一個主題對象,需要的朋友可以參考下2023-10-10
SpringBoot3.x使用es進(jìn)行數(shù)據(jù)查詢的方式
SpringBoot3中,Elasticsearch的版本升級至8.x,相關(guān)寫法有所改變,SpringBoot2中的ElasticsearchRestTemplate已被移除,推薦使用ElasticsearchTemplate或ElasticsearchClient進(jìn)行交互,本文介紹這兩個類的使用方式,并說明了依賴引入和自動裝配的配置,感興趣的朋友一起看看吧2025-12-12

