1.引入數據源:mysql
(1)在application.local.properties 配置多個數據源鏈接:spring
spring.datasource.primary.url=XXXXXXXX?useUnicode=true&characterEncoding=UTF-8 spring.datasource.primary.username=XXXXXXXX spring.datasource.primary.password=XXXXXXXX spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.url=XXXXXXXX?useUnicode=true&characterEncoding=UTF-8 spring.datasource.secondary.username=XXXXXXXX spring.datasource.secondary.password=XXXXXXXX spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
備註:在引入多數據源的時候須要在application.properties配置鏈接池避免鏈接超時的問題sql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.initial-size=1 spring.datasource.druid.min-idle=1 spring.datasource.druid.max-active=20 spring.datasource.druid.test-on-borrow=true spring.datasource.druid.stat-view-servlet.allow=true
(2)springBoot啓動Application類:app
@EnableTransactionManagement @SpringBootApplication @ComponentScan(basePackages = { "com.***.***.***.config" 此處掃描的應該爲數據源配置所在的類包 }) public class SccSupplierApplication { private static Logger logger = LoggerFactory.getLogger(ServletInitializer.class); public static void main(String[] args) { logger.info("SccSupplierApplication starting"); ConfigurableApplicationContext app = SpringApplication.run(SccSupplierApplication.class, args); logger.info("SccSupplierApplication start success"); } }
(3)數據源的配置:ui
數據源-01 @Configuration @MapperScan(basePackages = {"***.***.***.sysMapper"}, sqlSessionTemplateRef = "test1SqlSessionTemplate") public class DataSource1 { @Bean(name = "test1DataSource") @ConfigurationProperties(prefix ="spring.datasource.primary") @Primary public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "test1SqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources("classpath*:**/mapper/*Mapper.xml"); Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:**/sysMapper/**/*Mapper.xml"); Resource[] resources3 = new Resource[11]; for (int i = 0; i <=10; i++) { resources3[i]=resources2[i]; } Resource[] allResource = (Resource[]) ArrayUtils.addAll(resources3, resources); bean.setMapperLocations(allResource); return bean.getObject(); } @Bean(name = "test1TransactionManager") @Primary public PlatformTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "test1SqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
數據源-02 @Configuration @MapperScan(basePackages ={"com.***.***.mpoMapper"},sqlSessionTemplateRef = "test2SqlSessionTemplate") public class DataSource2 { @Bean(name = "test2DataSource") @ConfigurationProperties(prefix ="spring.datasource.secondary") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "test2SqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:**/mpoMapper/*Mapper.xml")); return bean.getObject(); } @Bean(name = "test2TransactionManager") public PlatformTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "test2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
備註:IDEA和Eclipse不太同樣,關於掃XML,映射文件的XML須要放在Resource包下,而且須要在POM中加:url
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
最後運行成功。spa
不少第一次引入多數據源會出現 無效綁定此類異常,這些根本緣由就是由於沒有掃到對應的Mapper或者XML。code
-----2018-4-13 17:25orm