SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn)
思路:
利用的是SpringBoot的Aop思想和自定義注解和反射機(jī)制的方法來實(shí)現(xiàn)
項(xiàng)目中我涉及公共字段的有createTime、updateTime、createUser、updateUser
步驟:
1. 自定義注解AutoFill,用于標(biāo)識(shí)需要進(jìn)行公共字段自動(dòng)填充的方法
/**
* 數(shù)據(jù)庫操作類型 使用的是枚舉方法
*/
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}/**
* 自定義注解,用于標(biāo)識(shí)某個(gè)方法需要進(jìn)行功能字段自動(dòng)填充處理
*/
@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í)體對(duì)象
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)前不同的操作類型,為對(duì)應(yīng)額屬性通過反射來賦值
if(type == OperationType.INSERT){
//為四個(gè)公共字段賦值
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){
//為兩個(gè)公共字段賦值
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的需要自動(dòng)填充公共字段的方法上加入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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot Mybatis Plus公共字段自動(dòng)填充功能
- Mybatis plus的自動(dòng)填充與樂觀鎖的實(shí)例詳解(springboot)
- SpringBoot實(shí)現(xiàn)字段自動(dòng)填充的兩種方式
- Springboot實(shí)現(xiàn)公共字段填充的示例詳解
- SpringBoot ThreadLocal實(shí)現(xiàn)公共字段自動(dòng)填充案例講解
- SpringBoot自定義注解如何解決公共字段填充問題
- SpringBoot實(shí)現(xiàn)公共字段自動(dòng)填充的方法步驟
- SpringBoot基于MyBatisPlus實(shí)現(xiàn)公共字段自動(dòng)填充
相關(guān)文章
Springmvc國際化自動(dòng)配置代碼實(shí)現(xiàn)
這篇文章主要介紹了Springmvc國際化自動(dòng)配置代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring Bean生命周期之Bean元信息的配置與解析階段詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別
這篇文章主要介紹了JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來一起學(xué)習(xí)下吧2019-07-07
Spring boot 實(shí)現(xiàn)單個(gè)或批量文件上傳功能
這篇文章主要介紹了Spring boot 實(shí)現(xiàn)單個(gè)或批量文件上傳功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
Java微信公眾平臺(tái)開發(fā)(12) 微信用戶信息的獲取
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第十二步,微信用戶信息的獲取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的步驟
這篇文章主要介紹了SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控,一步一步的實(shí)現(xiàn)?Springboot?整合?Redis?來存儲(chǔ)數(shù)據(jù),讀取數(shù)據(jù),需要的朋友可以參考下2022-01-01
Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件
這篇文章主要介紹了Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件,下面文章圍繞FileUtils的相關(guān)資料展開怎么讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件的內(nèi)容,具有一定的參考價(jià)值,徐婭奧德小伙伴可以參考一下2021-12-12
SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決
這篇文章主要介紹了SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Java使用OTP動(dòng)態(tài)口令(每分鐘變一次)進(jìn)行登錄認(rèn)證
這篇文章主要介紹了Java使用OTP動(dòng)態(tài)口令(每分鐘變一次)進(jìn)行登錄認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

