1,application.yml 配置
spring:
datasource:
primary:
jdbc-url: jdbc:mysql://localhost:3306/cloakroom_business?characterEncoding=utf8&serverTimezone=GMT%2b8&connectTimeout=5000&socketTimeout=30000&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
poolName: testOneDB
maximum-pool-size: 15
max-lifetime: 180000
idle-timeout: 50000
connection-timeout: 10000
minimum-idle: 5
connection-test-query: SELECT 1
second:
jdbc-url: jdbc:mysql://198.101.526.66:3306/cloakroom_business?characterEncoding=utf8&serverTimezone=GMT%2b8&connectTimeout=5000&socketTimeout=30000&allowMultiQueries=true
username: root
password: 132467896
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
poolName: testSecondDB
maximum-pool-size: 15
max-lifetime: 180000
idle-timeout: 50000
connection-timeout: 10000
minimum-idle: 5
connection-test-query: SELECT 1
2,java代码配置文件
(1)第一个数据源
package com.mine.cloakroom.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.mine.cloakroom.dao", sqlSessionFactoryRef = "primarySqlSessionFactory")// sqlSessionFactoryRef 一定要加上, 指定用哪个sqlSessionFactory扫描dao
public class DataSource1Config {
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource testDataSource() {
return new HikariDataSource();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
(2)第二个数据源
package com.mine.cloakroom.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.mine.cloakroom.dao2", sqlSessionFactoryRef = "secondSqlSessionFactory")// sqlSessionFactoryRef 一定要加上, 指定用哪个sqlSessionFactory扫描dao
public class DataSource2Config {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource testDataSource() {
// return DataSourceBuilder.create().build(); // 这个也可行
return new HikariDataSource();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper2/*/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}