mybatis配置自动填充时间拦截器
1. Annotation:
① FillOnInsert
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface FillOnInsert {
Class<?> value();
}
② FillOnUpdate
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface FillOnUpdate {
Class<?> value();
}
2. handler(class):
import com.embracesource.common.core.utils.StringUtils;
import com.jichu.shebei.annotation.FillOnInsert;
import com.jichu.shebei.annotation.FillOnUpdate;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;
/**
* @description: 填充时间字段处理器
**/
@Component
@Intercepts({@Signature(type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class})})
public class FillTimeFieldHandler implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
SqlCommandType sqlCommandType = statement.getSqlCommandType();
Object param = invocation.getArgs()[1];
Class<?> aClass = param.getClass();
Class<?> superclass = aClass.getSuperclass();
// 判断是否是BaseEntity,并将填充其时间字段
if (StringUtils.equals(superclass.getName(),"com.embracesource.common.core.web.domain.BaseEntity")) {
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
superclass.getMethod("setCreateTime", Date.class).invoke(param, new Date());
superclass.getMethod("setUpdateTime", Date.class).invoke(param, new Date());
}
if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
superclass.getMethod("setUpdateTime", Date.class).invoke(param, new Date());
}
}
// 根据注解填充
for (Field field : aClass.getDeclaredFields()) {
if (field.isAnnotationPresent(FillOnInsert.class) && SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
Class<?> fillValueClass = field.getAnnotationsByType(FillOnInsert.class)[0].value();
field.set(param, fillValueClass.getDeclaredConstructor().newInstance());
}
if (field.isAnnotationPresent(FillOnUpdate.class) && SqlCommandType.UPDATE.equals(sqlCommandType)) {
field.setAccessible(true);
Class<?> fillValueClass = field.getAnnotationsByType(FillOnUpdate.class)[0].value();
field.set(param, fillValueClass.getDeclaredConstructor().newInstance());
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Interceptor.super.plugin(target);
}
@Override
public void setProperties(Properties properties) {
Interceptor.super.setProperties(properties);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示