主数据源(由于代码没有办法复制的原因,下面图片和文字不一致)
package com.xxxx.config; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @program: test * @description: * @author: chenzhian * @create: 2021-10-13 20:31:54 * @version: 1.0 **/ @Configuration // 扫描 Mapper 接口并容器管理 // @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MasterDataSourceConfig { // 精确到 master 目录,以便跟其他数据源隔离 static final String PACKAGE = "com.xxx.masterdao"; static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml"; @Bean(name = "masterDataSource") @Primary public DataSource masterDataSource() { return new HikariDataSource(); } @Bean(name = "masterTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); } @Bean(name = "masterSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(MasterDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); }
/*
可以另外手动指定目录
*/
@Bean(name="masterMapperScannerConfigurer")
public MapperScannerConfigurer masterMapperScannerConfigurer(){
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setSqlSessionFactoryBeanName("secondSqlSessionFactory");
configurer.setBasePackage(PACKAGE);
return configurer;
}
}
第二个数据源(由于代码没有办法复制的原因,下面图片和文字不一致)
package com.xxxx.config; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @program: test * @description: * @author: chenzhian * @create: 2021-10-13 20:31:54 * @version: 1.0 **/ @Configuration // 扫描 Mapper 接口并容器管理 // @MapperScan(basePackages = secondDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory") public class SecondDataSourceConfig { // 精确到 second 目录,以便跟其他数据源隔离 static final String PACKAGE = "com.xxx.seconddao"; static final String MAPPER_LOCATION = "classpath:mapper/second/*.xml"; @Bean(name = "secondDataSource") @Primary public DataSource secondDataSource() { return new HikariDataSource(); } @Bean(name = "secondTransactionManager") @Primary public DataSourceTransactionManager secondTransactionManager() { return new DataSourceTransactionManager(secondDataSource()); } @Bean(name = "secondSqlSessionFactory") @Primary public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource secondDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(secondDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(secondDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); }
/*
可以另外手动指定目录
*/
@Bean(name="secondMapperScannerConfigurer")
public MapperScannerConfigurer crwalerMapperScannerConfigurer(){
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setSqlSessionFactoryBeanName("secondSqlSessionFactory");
configurer.setBasePackage(PACKAGE);
return configurer;
}
}
application.yml的配置
mybatis 配置 多个数据源 org.apache.ibatis.binding.BindingException Vakud bound statement (not found)
原因是没有配置@MapperScan ,因为我这里是多个目录,所以也要配置多个,在Application的启动上面加
@MapperScan(value{"com.xxxx","com.aaaa"})
如果上面有用到MapperScannerConfigurer 就不需要这个了。
The bean 'xxxService' could not be injected as a 'com.xxxxImpl' because it is a JDK dynamic proxy that implements:
com.xxxxService
Action:
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
原因居然是因为用到是@Resouce 来直接调用ServiceImpl,造成程序加载的时候还没有Service的Impl实现类,让程序加载的时候找不到这个类
解决方案
改成调用Service 接口,另外把@Resouce 改成 @Autowired 就可以了。
资料整理来源
SpringBoot和Mybatis配置多数据源连接多个数据库