MyBatis笔记----@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Co...

使用MyBatis 3.4.1或者其以上版本

@Intercepts({  

@Signature(type = StatementHandler.class,  
method = "prepare",  
args = {Connection.class, Integer.class})}) 

 

使用MyBatis 3.4.1(不包含)以下

 

@Intercepts({  

@Signature(type = StatementHandler.class,  
method = "prepare",  
args = {Connection.class})})

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将这个 MyBatis 自定义拦截器注册为 Bean ,可以使用 MyBatis 提供的 `@Intercepts` 和 `@Component` 注解,以及 Spring Framework 提供的 `@Bean` 注解。具体的步骤如下: 1. 在拦截器类上添加 `@Intercepts` 注解,用于指定拦截哪个方法。 2. 在拦截器类上添加 `@Component` 注解,用于将拦截器类注册为 Spring Bean 。 3. 在配置类中使用 `@Bean` 注解将拦截器类注册为 Bean 。 示例代码如下: ```java @Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})}) @Component public class SqlStatsInterceptor implements Interceptor { // 拦截器逻辑代码 } @Configuration public class MyBatisConfig { @Autowired private SqlStatsInterceptor sqlStatsInterceptor; @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); // 注册拦截器 sessionFactory.setPlugins(new Interceptor[] { sqlStatsInterceptor }); return sessionFactory; } } ``` 在上面的示例代码中,`SqlStatsInterceptor` 类使用 `@Intercepts` 和 `@Component` 注解分别指定拦截 `StatementHandler` 接口的 `prepare` 方法,并将拦截器类注册为 Spring Bean 。然后在配置类中使用 `@Bean` 注解将拦截器类注册为 Bean ,并将其添加到 `SqlSessionFactory` 中的拦截器列表中。这样,MyBatis 在创建 `SqlSession` 时就会将该拦截器应用到所有的 SQL 语句中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值