使用MyBatis批量插入数据,MyBatis拦截器失效?-灵析社区

sumous_01

使用MyBatis批量插入数据,MyBatis拦截器失效? 问题描述:我在项目中写了一个MyBatis的拦截器(插件),作用是在插入或更新数据时自动填充id、create_by、create_time等基础字段的值,代码如下: @Component @Intercepts({ @Signature(type = Executor.class,method = "update",args = {MappedStatement.class, Object.class}) }) public class MyBatisAutoFillPlugin implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 被代理的对象 Object target = invocation.getTarget(); // 被代理的方法 Method method = invocation.getMethod(); // 被代理的方法执行参数 Object[] args = invocation.getArgs(); if (!(args.length > 1)){ return invocation.proceed(); } MappedStatement mappedStatement = (MappedStatement) args[0]; Object paramObj = args[1]; if (paramObj instanceof BaseEntity){ // 获取登录用户 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); SysLoginUser sysLoginUser = (SysLoginUser) authentication.getPrincipal(); BaseEntity entity = (BaseEntity) paramObj; Date now = new Date(); // 判断sql语句 if (Objects.equals(SqlCommandType.INSERT,sqlCommandType)){ entity.setId(IdGenerator.getId()); entity.setStatus(CommonStatusEnum.NORMAL.getCode()); entity.setCreateBy(sysLoginUser.getId()); entity.setCreateTime(now); }else if (Objects.equals(SqlCommandType.UPDATE,sqlCommandType)){ entity.setUpdateBy(sysLoginUser.getId()); entity.setUpdateTime(now); } } // 调用实际业务方法 Object result = invocation.proceed(); return result; } } 当我在Mapper中写了一个**批量插入** 的方法,执行后拦截器却失效了,导致我的基础字段无法赋值,这是什么原因? insert into sys_user_role ( id,user_id,role_id ,status,create_time,create_by ,update_time,update_by) values (#{item.id},#{item.userId}, #{item.roleId}, #{item.status}, #{item.createTime},#{item.createBy}, #{item.updateTime},#{item.updateBy}) 我尝试使用单个插入的方法,这是可以的。 insert into sys_user_role ( id,user_id,role_id ,status,create_time,create_by ,update_time,update_by) values (#{id,jdbcType=BIGINT},#{userId,jdbcType=BIGINT},#{roleId,jdbcType=BIGINT} ,#{status,jdbcType=TINYINT},#{createTime,jdbcType=TIMESTAMP},#{createBy,jdbcType=BIGINT} ,#{updateTime,jdbcType=TIMESTAMP},#{updateBy,jdbcType=BIGINT})

阅读量:13

点赞量:0

问AI
找到原因了,在@Intercepts注解添加要拦截的StatementHandler方法即可 @Intercepts({ @Signature(type = Executor.class,method = "update",args = {MappedStatement.class, Object.class}), @Signature(type = StatementHandler.class,method = "update",args = {Statement.class}) }) public class MyBatisAutoFillPlugin implements Interceptor { // some code... }