Spring boot has done a lot of simplified configuration settings during configuration, but the simplified configuration often sacrifices a certain amount of customization. For example, when configuring data sources, spring boot only provides 4 kinds of database connection pool configurations, which do not support commonly used configurations. druid
Reading the source code of spring boot DataSourceBuilder can find that the four data source types provided by spring boot are not what we want
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
"org.apache.tomcat.jdbc.pool.DataSource",
"com.zaxxer.hikari.HikariDataSource",
"org.apache.commons.dbcp.BasicDataSource", // deprecated
"org.apache.commons.dbcp2.BasicDataSource" };
But DataSourceBuilder provides the type method to customize the DataSource type
public DataSourceBuilder type(Class<? extends DataSource> type) {
this.type = type;
return this;
}
Knowing the method, the following configuration is much simpler
The first is the configuration of the application.properties file
spring.datasource.sso.url=jdbc:mysql://localhost:3306/sso?useSSL=false
spring.datasource.sso.username=root
spring.datasource.sso.password=root
spring.datasource.sso.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.sso.max-idle=5
spring.datasource.sso.max-wait=10000
spring.datasource.sso.min-idle=1
spring.datasource.sso.initial-size=1
spring.datasource.sso.validation-query=SELECT 1
spring.datasource.sso.test-on-borrow=false
spring.datasource.sso.test-while-idle=true
spring.datasource.sso.time-between-eviction-runs-millis=18800
spring.datasource.message.url=jdbc:mysql://localhost:3306/message?useSSL=false
spring.datasource.message.username=root
spring.datasource.message.password=root
spring.datasource.message.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.message.max-idle=5
spring.datasource.message.max-wait=10000
spring.datasource.message.min-idle=1
spring.datasource.message.initial-size=1
spring.datasource.message.validation-query=SELECT 1
spring.datasource.message.test-on-borrow=false
spring.datasource.message.test-while-idle=true
spring.datasource.message.time-between-eviction-runs-millis=18800
Then the specific main data source configuration class
@Configuration
@MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.sso"}, sqlSessionFactoryRef = "ssoSqlSessionFactory")
public class SsoConfig {
@Bean(name = "ssoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sso")
@Primary
public DataSource ssoDataSource() {
//指定使用DruidDataSource
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean(name = "ssoSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ssoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sso/*.xml"));
return bean.getObject();
}
@Primary
@Bean(name = "ssoTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("ssoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "ssoSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ssoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
_ @ConfigurationProperties(prefix = "spring.datasource.sso") introduces configuration items_
Use the following methods to create DruidDataSource to simplify configuration
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
second data source
@Configuration
@MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.message"}, sqlSessionFactoryRef = "messageSqlSessionFactory")
public class MessageConfig {
@Bean(name = "messageDataSource")
@ConfigurationProperties(prefix = "spring.datasource.message")
public DataSource messageDataSource() {
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean(name = "messageSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("messageDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/message/*.xml"));
return bean.getObject();
}
@Bean(name = "messageTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("messageDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "messageSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("messageSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
The data source is configured
Each data source will generate its own sqlSession, independent of each other