注意事项
一:如果插入时有设置的值就使用之前设置的值,不带时才自动赋值。
二:xml文件中必须带有需要自动赋值的字段,否则无法知道赋值(如id、create_time、update_time)
代码详解
注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AutoId {
IdType type() default IdType.SNOWFLAKE;
enum IdType{
UUID,
SNOWFLAKE,
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface CreateTime {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface UpdateTime {
String value() default "";
}
拦截器配置:
@Slf4j
@Component
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class MybatisAutoFillingInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
// 获取 SQL 命令
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
//只判断新增和修改
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
// 获取参数
Object parameter = invocation.getArgs()[1];
//批量操作时
if (parameter instanceof MapperMethod.ParamMap) {
MapperMethod.ParamMap map = (MapperMethod.ParamMap) parameter;
Object obj = map.get("list");
List<?> list = (List<?>) obj;
if (list != null) {
for (Object o : list) {
setParameter(o, sqlCommandType);
}
}
} else {
setParameter(parameter, sqlCommandType);
}
}
return invocation.proceed();
}
public void setParameter(Object parameter, SqlCommandType sqlCommandType) throws Throwable {
Class<?> aClass = parameter.getClass();
Field[] declaredFields;
declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
LocalDateTime now = LocalDateTime.now();
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
if (field.getAnnotation(CreateTime.class) != null) {
field.setAccessible(true);
if (field.get(parameter) == null) {
field.set(parameter, now);
}
}
if (field.getAnnotation(UpdateTime.class) != null) {
field.setAccessible(true);
if (field.get(parameter) == null) {
field.set(parameter, now);
}
}
if (field.getAnnotation(AutoId.class) != null ) {
field.setAccessible(true);
if (field.get(parameter) == null) {
if (Objects.equals(field.getAnnotation(AutoId.class).type(), AutoId.IdType.SNOWFLAKE)) {
field.set(parameter, IdUtil.getSnowflakeNextId());
} else if (Objects.equals(field.getAnnotation(AutoId.class).type(), AutoId.IdType.UUID)) {
field.set(parameter, IdUtil.simpleUUID());
}
}
}
}
if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
if (field.getAnnotation(UpdateTime.class) != null) {
field.setAccessible(true);
if (field.get(parameter) == null) {
field.set(parameter, now);
}
}
}
}
}
}
测试:
@Data
public class Temp implements Serializable {
@AutoId
private Long id;
private String name;
@CreateTime
private LocalDateTime createTime;
@UpdateTime
private LocalDateTime updateTime;
}
@SpringBootTest
class MyBatisPlusApplicationTests {
@Resource
private TempMapper tempMapper;
@Test
void mapper() {
Temp temp = new Temp();
temp.setName("自动ID222");
temp.setCreateTime(LocalDateTime.now().plusYears(11L));
tempMapper.insert(temp);
}
}
测试结果:
参考链接:
https://www.imooc.com/article/310554
http://t.csdn.cn/ZLsfK
https://hutool.cn/docs/#/