來源:juejin.cn/post/6984555714752561183
背景
資料庫設計程序中,我們往往會給資料庫表添加一些通用欄位,比如創建人、創建時間、修改人、修改時間,在一些公司的設計程序中有時會強制要求每個表都要包含這些基礎資訊,以便記錄資料操作時的一些基本日志記錄,
按照平常的操作來說,通用做法是輸寫sql時,將這些資訊和物件的基本屬性資訊一起寫入資料庫,當然,這也是大家習以為常的操作,這種寫法無可厚非,但是對于一個高級開發人員來說,如果所有的表都進行如此操作,未免顯得有點啰嗦,而且資料表多的話,這樣寫就有點得不償失了,
其實還有一種更簡便的做法,spring框架大家應該是比較熟悉的,幾乎每個公司都會用到,其中aop思想(切面編程)的經典應用場景之一就是日志記錄,本文結合aop思想,著重介紹下springboot框架下如何利用切面編程思想實作將創建人、創建時間、更新人、更新時間等基礎資訊寫入資料庫,
核心代碼
@Aspect
@Component
@Configuration
public class CommonDaoAspect {
private static final String creater = "creater";
private static final String createTime = "createTime";
private static final String updater = "updater";
private static final String updateTime = "updateTime";
@Pointcut("execution(* com.xx.xxxx.*.dao.*.update*(..))")
public void daoUpdate() {
}
@Pointcut("execution(* com.xx.xxxx.*.dao.*.insert*(..))")
public void daoCreate() {
}
@Around("daoUpdate()")
public Object doDaoUpdate(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
HttpServletRequest request = attributes.getRequest();
String token = request.getHeader("token");
String username = getUserName();
if (token != null && username != null) {
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
BeanUtils.setProperty(arg, updater, username);
BeanUtils.setProperty(arg, updateTime, new Date());
}
}
}
Object object = pjp.proceed();
return object;
}
@Around("daoCreate()")
public Object doDaoCreate(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
String username = getUserName();
if (username != null) {
if (StringUtils.isBlank(BeanUtils.getProperty(arg, creater))) {
BeanUtils.setProperty(arg, creater, username);
}
if (StringUtils.isBlank(BeanUtils.getProperty(arg, createTime))) {
BeanUtils.setProperty(arg, createTime, new Date());
}
}
}
}
Object object = pjp.proceed();
return object;
}
private String getUserName() {
return UserUtils.getUsername();
}
}
復制代碼
代碼介紹及注解說明
1.代碼介紹
核心代碼宣告了一個CommonDaoAspect切面類,物體類中宣告了4個核心方法和一個獲取用戶名資訊的方法,UserUtils是專案中宣告的工具類,包含獲取用戶id、姓名等一些基礎資訊,大家可以根據自己的實際情況去定義,不要照部就搬,
4個核心方法中,daoUpdate和daoCreate上添加了@Pointcut注解,該注解通過宣告正則運算式來確定專案包中dao目錄下哪些方法執行該切面方法,doDaoUpdate和doDaoCreate方法上添加了@Around注解,注解中引入了上述兩個方法,表示環繞通知,在我們自己dao目錄下的對應檔案目標方法完成前后做增強處理,

2.注解說明
- @Aspect:宣告切面類,里面可以定義切入點和通知
- @Component:表明該類是spring管理的一個物件
- @Pointcut:切入點,通過正則運算式宣告切入的時機,本文中是在目標方法(即專案中dao目錄下物體類中包含insert或update字串的方法)執行時加入切入資訊,即執行新增或更新時加入創建人和更新人等資訊,
- @Around:環繞通知,在目標方法完成前后做增強處理,本案例中表示在doCreate和doUpdate方法執行時添加引數資訊
注:execution(* com.xx.xxxx..dao..update*(..)) 表示在dao目錄下的任何檔案中的以update開頭的方法
execution(* com.xx.xxxx..dao..insert*(..)) 表示在dao目錄下的任何檔案中的以insert開頭的方法
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2021最新版)
2.別在再滿屏的 if/ else 了,試試策略模式,真香!!
3.臥槽!Java 中的 xx ≠ null 是什么新語法?
4.Spring Boot 2.5 重磅發布,黑暗模式太炸了!
5.《Java開發手冊(嵩山版)》最新發布,速速下載!
覺得不錯,別忘了隨手點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/301162.html
標籤:其他
