需求:
在MyBatis框架中,如何对createBy,createTime,updateBy等这些公有字段实现自动填充呢?
网上搜了很多,实现的方案全是采用集成MyBatisPlus,利用其封装好的方法来实现的。。。。。
数据准备:
1、准备一张数据库表
CREATE TABLE `user` (
`id` varchar(36) NOT NULL,
`username` varchar(64) NOT NULL,
`password` varchar(64) NOT NULL,
`create_by` varchar(32) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_by` varchar(32) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
2、新建一个SpringBoot工程
3、引入依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<!--切面-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
4、公有字段实体类准备:
自定义注解
package com.example.domain;
import com.example.annoation.createAt;
import com.example.annoation.createDate;
import com.example.annoation.updateAt;
import com.example.annoation.updateDate;
import java.util.Date;
public class BaseEntity {
@createAt
private String createBy;
@createDate
private Date createTime;
@updateAt
private String updateBy;
@updateDate
private Date updateTime;
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
package com.example.domain;
public class User extends BaseEntity{
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
切面实现:
1、自定义注解
/**
* 创建人
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface createAt {
}
/**
* 创建时间
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface createDate {
}
/**
* 更新人
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface updateAt {
}
/**
* 更新时间
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface updateDate {
}
2、利用反射以及切面编程来实现
package com.example.aspect;
import com.example.annoation.createAt;
import com.example.annoation.createDate;
import com.example.annoation.updateAt;
import com.example.annoation.updateDate;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Date;
@Aspect
@Component
public class SqlAspect {
@Before("execution(* com.example..*(..)) && args(entity,..)")
public void beforeSaveOrUpdate(Object entity) throws IllegalAccessException {
if (entity != null) {
// 获取所有字段,包括继承的字段
Class<?> clazz = entity.getClass();
while (clazz != null) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true); // 设置可访问私有字段
if (field.isAnnotationPresent(createAt.class)) {
// 当前登录用户ID
field.set(entity, "login_user_id");
} else if (field.isAnnotationPresent(createDate.class)) {
// 当前时间
field.set(entity, new Date());
} else if (field.isAnnotationPresent(updateAt.class)) {
// 当前登录用户ID
field.set(entity, "login_user_id");
} else if (field.isAnnotationPresent(updateDate.class)) {
// 当前时间
field.set(entity, new Date());
}
}
clazz = clazz.getSuperclass();
}
}
}
}
最终效果:
标签:MyBatisPlus,String,example,field,切面,MyBatis,import,com,public From: https://blog.csdn.net/QQBongBong/article/details/140110331