mybatis注册拦截器

背景:有时候直接声明bean的方式无法确保拦截器能够被Spring容器识别并正确地应用到MyBatis的环境中。
方式一:

@Configuration
public class MybatisConfig {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Bean
    public Interceptor mybatisTableNameInterceptor() {
        return new mybatisInterceptor();
    }

    @PostConstruct
    public void init() {
        sqlSessionFactory.getConfiguration().addInterceptor(mybatisInterceptor());
    }
}

方式二:

@Component
public class MybatisRunner implements ApplicationRunner {

    /**
     * @see DefaultListableBeanFactory
     */
    @Autowired
    private DefaultListableBeanFactory defaultListableBeanFactory;
    @Override
    public void run(ApplicationArguments args) {
        SqlSessionFactory sqlSessionFactory = defaultListableBeanFactory.getBean(SqlSessionFactory.class);
        sqlSessionFactory.getConfiguration().addInterceptor(new MybatisTableNameInterceptor());
    }
}
posted @   IamHzc  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2021-10-11 Vue - 插件
2021-10-11 Vue - mixin配置
2021-10-11 Vue - props配置
2021-10-11 Vue - ref属性
2021-10-11 SpringBoot-LocalDateTime格式化处理
点击右上角即可分享
微信分享提示
主题色彩