1.pom.xml文件
<spring.boot.version>2.2.5.RELEASE</spring.boot.version>
<mysql-connector-java-version>6.0.6</mysql-connector-java-version>
<alibaba-druid-version>1.1.10</alibaba-druid-version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
2.啟動類
// 這個就不需要在啟動類加了,相比較單數據源 @MapperScan("com.swapping.dao.mapper.mysql")
@SpringBootApplication @ComponentScan(basePackages = {"com.swapping.*"}) @Slf4j public class SwappingApiApplication { public static void main(String[] args) { try { SpringApplication.run(SwappingApiApplication.class, args); System.out.println("SwappingApiApplication 啟動成功......"); } catch (Exception e) { log.error("SwappingApiApplication 啟動失敗", e); } } }
3.yml配置文件
yml配置文件如下,雙數據源如下,多數據源就往下加配置就行 注意:相比較單數據源,應該用jdbc-url做key,否則會有運行時異常 多數據源java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName. ${my_first_mysql.host} 或 ${my_sencond_tidb.host} 可以是配置文件取的配置,也可以像username一樣直接寫死,都可以
# 驅動配置信息 spring: datasource: my-first-mysql: #連接池的配置信息 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: filters: max-active: 16 initial-size: 2 max-wait: 3000 minIdle: 2 time-between-eviction-runsMillis: 60000 min-evictable-idle-timeMillis: 300000 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false max-open-prepared-statements: -1 jdbc-url: jdbc:mysql://${my_first_mysql.host}:${my_first_mysql.port}/my_first_mysql_dbName?characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=GMT%2B8&connectTimeout=2000&socketTimeout=150000 username: XXX password: XXX my-sencond-tidb: #連接池的配置信息 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: filters: max-active: 16 initial-size: 2 max-wait: 3000 minIdle: 2 time-between-eviction-runsMillis: 60000 min-evictable-idle-timeMillis: 300000 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false max-open-prepared-statements: -1 jdbc-url: jdbc:mysql://${my_sencond_tidb.host}:${my_sencond_tidb.port}/my_sencond_mysql_dbName?characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=GMT%2B8&connectTimeout=2000&socketTimeout=150000 username: XXX password: XXX
4.自定義DataSourceConfig
自定義DataSourceConfig,而不用Springboot自啟動的DataSourceConfig。
兩個數據源就定義兩個,多個數據源就定義多個
如果重點2沒有區分各數據源的包掃描路徑,會有運行時異常:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
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.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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; /** * 第一個數據源 * * @author xudong.shen * @date 2022/04/16 */ @Configuration @MapperScan(basePackages = "com.swapping.dao.mapper.mysql", sqlSessionTemplateRef = "sqlSessionTemplate")//重點1 指定包掃描,當前這個數據源對應的mapper.java文件放在哪個包下,這里路徑就指定哪里
public class MyFirstDataSourceConfig { @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource.my-first-mysql")//重點3 這里對應yml的當前數據源的前綴
public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean @Primary public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysql/*.xml"));//重點2 指定包掃描,當前這個數據源對應的mapper.xml文件在哪個resource下的包里,這里路徑就指定哪里 // bean.setPlugins(new Interceptor[] {xxx}); // 設置MyBatis插件
return bean.getObject(); } @Bean @Primary public DataSourceTransactionManager testTransactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean @Primary public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
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.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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; /** * 第二個數據源 * * @author xudong.shen * @date 2022/04/18 */ @Configuration @MapperScan(basePackages = "com.swapping.dao.mapper.tidb", sqlSessionTemplateRef = "sqlSessionTemplateSecond")//重點1 指定包掃描,當前這個數據源對應的mapper.java文件放在哪個包下,這里路徑就指定哪里
public class MySecondDataSourceConfig { @Bean(name = "dataSourceSecond") @ConfigurationProperties(prefix = "spring.datasource.my-sencond-tidb")//重點3 這里對應yml的當前數據源的前綴
public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactorySecond") public SqlSessionFactory testSqlSessionFactory(@Qualifier("dataSourceSecond") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/tidb/*.xml"));//重點2 指定包掃描,當前這個數據源對應的mapper.xml文件在哪個resource下的包里,這里路徑就指定哪里 // bean.setPlugins(new Interceptor[] {xxx}); // 設置mybatis插件
return bean.getObject(); } @Bean(name = "transactionManagerSecond") public DataSourceTransactionManager testTransactionManager(@Qualifier("dataSourceSecond") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplateSecond") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlSessionFactorySecond") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
5.Mapper.java 和 Mapper.xml的說明
到這里,多數據源的配置層面東西,就全部完成了。
后面的mapper.java mapper.xml 就放在上面各自數據源的指定包路徑下,即可。
后面Service調用Mapper的動作,就和單數據源 一樣了。正常調用就行了。(當然,跨數據源了,事務肯定就行不通了!)