解析Jeesuite框架中mybatis拦截器的使用

Jeesuite系列包含基础库、配置中心、统一认证平台、网关、快速开发平台五大部分(计划陆续开源)。提供灵活可靠的一站式解决方案、开箱即用。

其中,Jeesuite基础库提供了数据库、缓存、消息中间件、分布式定时任务、云存储等基础模块。每个组件都可以独立使用,十分方便.下面我们来看看mybatis拦截器在Jeesuite框架中的使用.

SpringBoot并没有为mybatis提供自动启动类,但是mybatis自己提供了

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.0</version>
</dependency>
在这里mybatis-spring-boot-autoconfigure-1.3.0.jar!\META-INF\spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfigurationJeesuiteMybatisConfiguration配置类中实现了InitializingBean
@Configuration
@EnableConfigurationProperties(MybatisPluginProperties.class)
@ConditionalOnClass(MutiRouteDataSource.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class JeesuiteMybatisConfiguration implements InitializingBean {}

所以springboot启动的时候会实例化MybatisAutoConfiguration类.

JeesuiteMybatisConfiguration还实现了afterPropertiesSet()方法.如下

if(org.apache.commons.lang3.StringUtils.isNotBlank(properties.getInterceptorHandlerClass())){
	String[] customHanlderClass = StringUtils.tokenizeToStringArray(properties.getInterceptorHandlerClass(), ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
	hanlders.addAll(Arrays.asList(customHanlderClass));
}
		
if (properties.isCacheEnabled()) {
	hanlders.add(CacheHandler.NAME);
}

if (properties.isRwRouteEnabled()) {
	hanlders.add(RwRouteHandler.NAME);
}

if (properties.isDbShardEnabled()) {
	hanlders.add(DatabaseRouteHandler.NAME);
}
		
if (properties.isPaginationEnabled()) {
	hanlders.add(PaginationHandler.NAME);
}

这里添加了默认的拦截器处理类.当然还可以自定义拦截器类,写在配置文件中

jeesuite.mybatis.interceptorHandlerClass=com.xxxxxx.xxxxxxx.XXXXXHandler

当然这个handler实现了InterceptorHandler接口,设置了

interceptorOrder优先级排序

当拦截器不为空时,调用setInterceptorHandlers方法初始化拦截器类,并且将拦截器加入sqlSessionFactory的配置类中,至此拦截器配置完成.

JeesuiteMybatisInterceptor interceptor = new JeesuiteMybatisInterceptor();
interceptor.setMapperLocations(mapperLocations);
interceptor.setInterceptorHandlers(org.apache.commons.lang3.StringUtils.join(hanlders.toArray(), ","));
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);

其中JeesuiteMybatisInterceptor是实现了

implements Interceptor,InitializingBean,DisposableBean,ApplicationContextAware

调用时将所有拦截器添加到了

List<InterceptorHandler> interceptorHandlers = new ArrayList<>();

并且按照优先级interceptorOrder参数排序.

mybatis拦截器起作用的关键就是:给sqlSessionFactory添加拦截器

@Configuration
public class AutoSetConfiguration {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public AutoSetConfiguration() {
    }

    @PostConstruct
    public void init() {
        // 给sqlSessionFactory添加拦截器
        this.sqlSessionFactory.getConfiguration().addInterceptor(new XXXXInterceptor());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot使用Mybatis拦截器,需要进行以下步骤: 1.定义拦截器类,实现Mybatis的Interceptor接口。 ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) }) public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { //拦截逻辑 return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { //设置拦截器属性 } } ``` 2.在Spring Boot的配置文件配置拦截器。 ```yaml mybatis: configuration: #配置拦截器 #注意:mybatis下的configuration属性是Mybatis的Configuration对象,不是Spring Boot的配置文件 #使用Mybatis的配置文件时需要使用mybatis.config-location属性 #使用Spring Boot的配置文件时需要使用mybatis.configuration属性 #两者不能同时使用 plugins: - com.example.MyInterceptor ``` 3.在Mybatis的Mapper接口使用@Intercepts注解指定拦截器。 ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) }) public interface UserMapper { List<User> selectAll(); } ``` 其,@Intercepts注解用于指定拦截器,@Signature注解用于指定拦截的方法。 总的来说,在Spring Boot使用Mybatis拦截器的过程和在其他环境下使用Mybatis拦截器的过程类似,只需要在Spring Boot的配置文件配置拦截器即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的咩咩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值