This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

background

During database design, we often add some common fields to database tables, such as creator, creation time, modifier, and modification time. In some companies’ design process, it is sometimes mandatory that every table should contain these basic information, so as to record some basic log records of data operations. According to the normal operation, the general practice is the writing SQL, the basic attributes of the information and the object information written to the database together, of course, this is everybody used to operation, this kind of writing it, but for a senior developer, if all the tables are such operation, may seem a bit repetitive, and the data table, It’s a bit of a pyrrhic. In fact, there is an easier way to do this. The Spring framework should be familiar to you, and almost every company will use it. One of the classic application scenarios of AOP ideas (aspect programming) is logging. This paper mainly introduces how to write the basic information such as creator, creation time, update person and update time into database by using the aspect programming idea under springboot framework.

The core code

@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(); }}Copy the code

Code introduction and annotation

1. Code introduction

The core code declares a CommonDaoAspect aspect class, and the entity class declares four core methods and a method to get user name information. UserUtils is a utility class declared in the project, which contains basic information such as user ID, name, and so on. You can define it according to your own situation. Among the four core methods, the @pointCut annotation was added to daoUpdate and daoCreate, which declares regular expressions to determine which methods in the dao directory of the project package execute the aspect method. The @around annotation has been added to the doDaoUpdate and doDaoCreate methods, which introduce the above two methods to wrap Around the notification and enhance the processing before and after the corresponding file target method in our dao directory completes.

2. Explanatory notes

  • @Aspect: Declares an Aspect class where pointcuts and advice can be defined
  • @Component: Indicates that the class is a Spring-managed object
  • Pointcut: A Pointcut is a regular expression that states the timing of an entry. In this case, the entry information is added when the target method (that is, the method that contains the insert or update string in the entity class in the DAO directory of the project) is executed. The entry information is added when the addition or update is performed.
  • @around: Around notification, which is enhanced before and after the target method completes, in this case by adding parameter information when the doCreate and doUpdate methods are executed

Note: execution (* com. Xx. XXXX.. dao.. update*(..) ) represents methods beginning with UPDATE in any file in the DAO directory

execution(* com.xx.xxxx.. dao.. insert*(..) ) represents methods beginning with INSERT in any file in the DAO directory

Due to the small series of shallow talent and limited ability, for the inaccurate or inappropriate description of the article, I hope we can timely point out and correct, in the coding road together to move forward. If this article is helpful to you, I also hope to give xiaobian some likes and attention oh, your recognition and love is my continuous output of the biggest power.