会编程的羽流云
多数据源配置方式
多数据源配置方式(springboot整合mybatis)
项目依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
springboot配置文件
spring:
datasource:
nt-cultural-tourism:
url: jdbc:mysql://ip:port/datasuource_name1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: org.apache.commons.dbcp2.BasicDataSource
dbcp2:
max-wait-millis: 10000
min-idle: 5
initial-size: 5
max-total: 300
nt-emergency:
url: jdbc:mysql://ip:port/datasuource_name2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: org.apache.commons.dbcp2.BasicDataSource
dbcp2:
max-wait-millis: 10000
min-idle: 5
initial-size: 5
max-total: 300
nt-natural-resources:
url: jdbc:mysql://ip:port/datasuource_name3?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: org.apache.commons.dbcp2.BasicDataSource
dbcp2:
max-wait-millis: 10000
min-idle: 5
initial-size: 5
max-total: 300
Java配置类
目录格式配置类
NtCulturalTourismConfiguration.java配置
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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;
@Configuration
@MapperScan(basePackages = "com.dataojo.nantong.mapper.NtCulturalTourismMapper", sqlSessionTemplateRef = "nt_cultural_tourism_SqlSessionTemplate")
public class NtCulturalTourismConfiguration {
@Value("${spring.datasource.nt-cultural-tourism.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.nt-cultural-tourism.url}")
private String url;
@Value("${spring.datasource.nt-cultural-tourism.username}")
private String username;
@Value("${spring.datasource.nt-cultural-tourism.password}")
private String password;
@Bean(name = "nt_cultural_tourism_DataSource")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}
@Bean(name = "nt_cultural_tourism_SqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("nt_cultural_tourism_DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/NtCulturalTourismMappering/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "nt_cultural_tourism_TransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("nt_cultural_tourism_DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "nt_cultural_tourism_SqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("nt_cultural_tourism_SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
NtEmergencyConfiguration.java配置
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.dataojo.nantong.mapper.NtEmergencyMapper", sqlSessionTemplateRef = "nt_emergency_SqlSessionTemplate")
public class NtEmergencyConfiguration {
@Value("${spring.datasource.nt-emergency.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.nt-emergency.url}")
private String url;
@Value("${spring.datasource.nt-emergency.username}")
private String username;
@Value("${spring.datasource.nt-emergency.password}")
private String password;
@Bean(name = "nt_emergency_DataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}
@Bean(name = "nt_emergency_SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("nt_emergency_DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/NtEmergencyMappering/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "nt_emergency_TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("nt_emergency_DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "nt_emergency_SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("nt_emergency_SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
NtNaturalResourcesConfiguration.java配置
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.dataojo.nantong.mapper.NtNaturalResourcesMapper", sqlSessionTemplateRef = "nt_natural_resources_SqlSessionTemplate")
public class NtNaturalResourcesConfiguration {
@Value("${spring.datasource.nt-natural-resources.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.nt-natural-resources.url}")
private String url;
@Value("${spring.datasource.nt-natural-resources.username}")
private String username;
@Value("${spring.datasource.nt-natural-resources.password}")
private String password;
@Bean(name = "nt_natural_resources_DataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}
@Bean(name = "nt_natural_resources_SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("nt_natural_resources_DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/NtNaturalResourcesMappering/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "nt_natural_resources_TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("nt_natural_resources_DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "nt_natural_resources_SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("nt_natural_resources_SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
至此多数据源配置已完成