痛点
在实际开发过程中:在inset和update过程方法中要不停的set的字段,是不是看着就感觉恶心(对代码有洁癖,对代码有追求的人)包括我也是
然而今天我就写了这种代码 摆烂自己
entity.setCreateBy("sss");
entity.setCreateTime(new Date());
entity.setUpdateBy("ssss");
entity.setUpdateTime(new Date());
振作起来~~
另外个同事就开骂,这人代码写的真有水平。哈哈哈
开始我的表演
源码地址
源码目录:com.song.kkxxpoi.common.AutoInsertTimeAndUser
开始摆烂-Mybatis之拦截器自动添加创建人创建时间更新人更新时间·视频
代码截图
web
Interceptor-拦截器
/**
* 功能描述:拦截器 反射
*帮助文档:https://blog.csdn.net/minghao0508/article/details/124420953
* @author Songxianyang
* @date 2022-08-12 21:50
*/
@Component
@Intercepts({@Signature(type = Executor.class, method = "update",
args = {MappedStatement.class, Object.class})})
public class AutoInsertTimeAndUser implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 线程安全
AtomicReference<User> userAtomicReference = new AtomicReference<>();
userAtomicReference.set(new User("sxy"));
// 获取一个MappedStatement
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
// 获取 SQL 命令 UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
// 获取参数
Object parameter = invocation.getArgs()[1];
// 获取私有成员变量
Field[] declaredFields = parameter.getClass().getDeclaredFields();
for (Field field : declaredFields) {
if (Objects.nonNull(field.getAnnotation(CreateTime.class))) {
// 插入
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter, new Date());
}
}
if (Objects.nonNull(field.getAnnotation(CreateUser.class))) {
// 插入
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter, userAtomicReference.get().getName());
}
}
if (Objects.nonNull(field.getAnnotation(UpdateTime.class))) {
// 修改
if (SqlCommandType.UPDATE.equals(sqlCommandType)|| SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter, new Date());
}
}
if (Objects.nonNull(field.getAnnotation(UpdateUser.class))) {
// 修改
if (SqlCommandType.UPDATE.equals(sqlCommandType)||SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
field.set(parameter, userAtomicReference.get().getName());
}
}
}
//获取方法参数
Object[] args = invocation.getArgs();
//获取方法名
invocation.getMethod();
//获取代理对象
invocation.getTarget();
return invocation.proceed();
}
/**
* 生成MyBatis拦截器代理对象
*/
@Override
public Object plugin(Object target) {
return Interceptor.super.plugin(target);
}
/**
* 设置插件属性(直接通过Spring的方式获取属性,所以这个方法一般也用不到)
* 项目启动的时候数据就会被加载
*/
@Override
public void setProperties(Properties properties) {
Interceptor.super.setProperties(properties);
}
}
service
@Override
@Transactional(rollbackFor = Exception.class)
public int insert(IntEndScaleForecastEntity entity) {
return iIntEndScaleForecastMapper.insert(entity);
}
entity
以上几个注解
代码
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-08-12 21:51
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface CreateTime {
}
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-08-12 21:51
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface CreateUser {
}
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-08-12 21:51
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface UpdateTime {
}
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-08-12 21:51
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface UpdateUser {
}