SpringBoot配置双(多)数据源后,MyBatis拦截器失效的问题 - ZhangTory's NoteBlog - 张耀誉的笔记博客

SpringBoot配置双(多)数据源后,MyBatis拦截器失效的问题

某个项目使用到了双数据源,同时又使用了自定义MyBatis拦截器,于是发现自定义拦截器失效了。
需要在创建SqlSessionFactory时设置拦截器即可。

SpringBoot配置双数据源方法

首先配置主数据源:

@Configuration
@MapperScan(basePackages = "com.zhangtory.doubledb.mapper.write", sqlSessionFactoryRef = "writeSqlSessionFactory")
public class WriteDataSourceConfig {

    //主数据源
    @Primary
    @Bean(name = "writeDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.write")
    public DataSource getWriteDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "writeSqlSessionFactory")
    public SqlSessionFactory writeSqlSessionFactory(@Qualifier("writeDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/write/*.xml"));
        return bean.getObject();
    }

    @Primary
    @Bean("writeSqlSessionTemplate")
    public SqlSessionTemplate writeSqlSessionTemplate(@Qualifier("writeSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
}

次数据源:

@Configuration
@MapperScan(basePackages = "com.zhangtory.doubledb.mapper.read", sqlSessionFactoryRef = "readSqlSessionFactory")
public class ReadDataSourceConfig {

    @Bean(name = "readDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.read")
    public DataSource getReadDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "readSqlSessionFactory")
    public SqlSessionFactory readSqlSessionFactory(@Qualifier("readDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/read/*.xml"));
        return bean.getObject();
    }

    @Bean("readSqlSessionTemplate")
    public SqlSessionTemplate readSqlSessionTemplate(@Qualifier("readSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
}

配置文件写入两个数据源的配置信息:

spring:
  datasource:
    write:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: zyy
      password: nobodycanstopzyy
      jdbc-url: jdbc:mysql://192.168.31.50:3306/write?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
    read:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: zyy
      password: nobodycanstopzyy
      jdbc-url: jdbc:mysql://192.168.31.50:3306/read?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false

另外需要在Application上加入mapper扫描配置:

@MapperScan({"com.zhangtory.doubledb.mapper.read","com.zhangtory.doubledb.mapper.write"})

配置拦截器

我们在主数据源中配置自定义的拦截器:

    @Autowired
    private SqlInterceptor sqlInterceptor;

    @Autowired
    private SqlLogInterceptor sqlLogInterceptor;

    @Primary
    @Bean(name = "walletSqlSessionFactory")
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("walletDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setPlugins(sqlInterceptor, sqlLogInterceptor);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/wallet/*.xml"));
        return bean.getObject();
    }

添加新评论

电子邮件地址不会被公开,评论内容可能需要管理员审核后显示。