厉害!一个工具类搞定CRUD的创建人,修改人,时间等字段赋值~
大家好,我是D哥
点击关注下方公众号,Java面试资料 都在这里
来源:juejin.cn/post/6984555714752561183
# 背景
数据库设计过程中,我们往往会数据库表添加一些通用的场景,比如创建人、创建时间、修改人、修改时间,在一些公司的设计过程中,有时会强制要求每个表过程需要包含这些基础信息,为了记录数据操作时的一些基本日志记录。
饮食属性的操作,一般的做法是输出写sql时,将这些和对象的基本信息同时写入数据,当然,大家习以为常的操作,种写法无可厚非,但是对于一个高级开发人员来说,如果所有的表都进行了这样的操作,还不免有点啰嗦,而且数据表多的话,写就得不失失了。
实际上还有这样一种更引人入胜的细节,春天框架大家应该是比较了解的,几乎每个公司都比较了解,其中有一个场景的经典应用就是面编程记录,本文结合了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.代码介绍
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开头的方法
最后,D哥也建了一个技术群,主要探讨一些新的技术和开源项目值不值得去研究及IDEA使用的“骚操作”,有兴趣入群的同学,可长按扫描下方二维码,一定要备注:城市+昵称+技术方向,根据格式备注,可更快被通过且邀请进群。
▲长按扫描
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。