Springboot+tk.mybatis,实现了Mybatis拦截器Interceptor。
在项目启动过程中执行的Mapper方法时,没有进入拦截器的intercept()方法;
在项目完全启动后,通过前端请求方法再调用的Mapper方法却可以进入拦截器的intercept()方法
@Intercepts({@Signature(type = Executor.class, method = "query", args = {
MappedStatement.class,
Object.class,
RowBounds.class,
ResultHandler.class,
CacheKey.class,
BoundSql.class})})
@Slf4j
public class MybatisSelectResultMapInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 自定义代码
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
}
return target;
}
@Override
public void setProperties(Properties properties) {
}
}
如何实现在项目启动过程中执行的Mapper方法时,进入拦截器的intercept方法?
问题已经解决。
@AutoConfigureAfter({MybatisAutoConfiguration.class})
@Configuration
public class MyCacheConfig {
@Autowired
private MyDAO myDAO;
@PostConstruct
public void test() {
myDAO.selectSomething();
}
}
修改为以下代码,myDAO.selectSomething()就可以被Mybatis Interceptor拦截了
@Configuration
@Order(Ordered.LOWEST_PRECEDENCE)
public class MyCacheConfig implements CommandLineRunner {
@Autowired
private MyDAO myDAO;
@Override
public void run(String... args) throws Exception {
myDAO.selectSomething();
}
}