springboot配置多数据源,占坑mybatis-plus

springboot中配置多数据源步骤如下:
1.配置datesource和template

@Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.mix")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

2.配置事务,sqlSessionFactory,以及sqlSessionTemplate
注意自定义的mybatis拦截器通过sqlSessionFactory配置进去

@Configuration
@MapperScan(basePackages = "xxx", sqlSessionTemplateRef  = "primarySqlSessionTemplate")
public class MixSqlSessionTemplate {
    @Autowired
    private AutoChangeBaseNameInterceptor autoChangeBaseNameInterceptor;
    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setPlugins(autoChangeBaseNameInterceptor);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mix/*.xml"));
        return bean.getObject();
    }
//配置声明式事务管理器
@Bean(name = "primaryTransactionManager")
@Primary
public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(
        @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

}
3.配置文件中配置数据源信息

#dataSource----mysql 合同信息
spring.datasource.mix.jdbc-url=xxx
spring.datasource.mix.username=xxx
spring.datasource.mix.password=xxx
spring.datasource.mix.driver-class-name=com.mysql.jdbc.Driver

4.注意扫描的xml位置,以及mapper位置,自定义的数据源需要使用jdbc-url而不是url等

如果要使用mybatis-plus 配置sqlSessionFactoryBean的时候改为MybatisSqlSessionFactoryBean 不然会出现Invalid bound statement (not found)错误

 @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)
            throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值