springboot:使用pagehelper之后,无法添加自定义mybatis拦截器


1.实现自定义mybatis拦截器

@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class MybatisInterceptor implements Interceptor {

// 略......

}

2.在web listener中添加这个拦截器

@Component
public class StartSysListener implements ApplicationListener<ContextRefreshedEvent> {

  private static Logger log = LoggerFactory.getLogger(StartSysListener.class);
  
  @Autowired
  private MybatisInterceptor mybatisInterceptor;
  @Autowired
  private List<SqlSessionFactory> sqlSessionFactoryList;


  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {

    this.addMyInterceptor();

  }

  private void addMyInterceptor() {
    log.debug("添加自定义Mybatis SQL拦截器.");
    for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
      sqlSessionFactory.getConfiguration().addInterceptor(mybatisInterceptor);
    }
  }

}


这种方式添加简单,易于使用。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM