1.引入数据源:
(1)在application.local.properties 配置多个数据源连接:
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配置连接池避免连接超时的问题
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类:
@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)数据源的配置:
数据源-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中加:
src/main/resources
**/*.xml
false
最后运行成功。
很多第一次引入多数据源会出现 无效绑定此类异常,这些根本原因就是因为没有扫到对应的Mapper或者XML。
-----2018-4-13 17:25