spring boot多数据源配置

[b][color=green]spring boot多数据源的配置,spring boot+mybatis配置数据源[/color][/b]

[color=green]第一步pom.xml配置如下:[/color]


org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE






org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-starter-aop




org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1


org.springframework.boot
spring-boot-starter-test



mysql
mysql-connector-java


com.alibaba
druid
1.0.20


com.alibaba
fastjson
1.2.12




[color=green]第二步application.properties配置如下:[/color]
#datascore one
spring.datasource.primary.url=jdbc:mysql://localhost/testdb1
spring.datasource.primary.username=test1
spring.datasource.primary.password=1234
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver


#datascore two
spring.datasource.secondary.url=jdbc:mysql://localhost/testdb2
spring.datasource.secondary.username=test2
spring.datasource.secondary.password=1234
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver


[color=green]第三步主要数据源对象配置如下:[/color]
/**
* 数据源test1的配置
* @author zx
* @date 2017-03-12
*/
@Configuration
@MapperScan(basePackages = "com.zx.dao.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {

@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);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/test1/*Mapper*.xml"));
return bean.getObject();
}

@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager 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);
}

}


/**
* 数据源test2的配置
* @author zx
* @date 2017-03-12
*/
@Configuration
@MapperScan(basePackages = "com.zx.dao.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate")
public class DataSource2Config {

@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:mapper/test2/*.xml"));
return bean.getObject();
}

@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager 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);
}

}


[color=red]具体项目参考github如下:[/color]
[color=green]项目地址:[url]https://github.com/zhangxia1030/spring-boot-datasources[/url][/color]

你可能感兴趣的