多数据源
一、依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <scope>runtime</scope> </dependency> <!--mybatis-plus 依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1.tmp</version> </dependency>
二、yml数据库配置
spring: datasource: dynamic: primary: db233 datasource: db233: jdbc-url: jdbc:sqlserver://120.0.0.1:1433;database=db1 username: sa password: 123456 driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver hikari: maximum-pool-size: 12 minimum-idle: 5 idle-timeout: 300000 connection-timeout: 20000 connection-test-query: select 1 db231: jdbc-url: jdbc:sqlserver://120.0.0.1:1433;database=db2 username: sa password: 123456 driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver hikari: maximum-pool-size: 12 minimum-idle: 5 idle-timeout: 300000 connection-timeout: 20000 connection-test-query: select 1 db231e: jdbc-url: jdbc:sqlserver://120.0.0.1:1433;database=db3 username: sa password: 123456 driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver hikari: maximum-pool-size: 12 minimum-idle: 5 idle-timeout: 300000 connection-timeout: 20000 connection-test-query: select 1 durid: initial-size: 1 max-active: 20 min-idle: 1 max-wait: 60000
三、配置文件
@Configuration @MapperScan(basePackages = "com.xxxx.mapper.db231", sqlSessionFactoryRef = "db231SqlSessionFactory") public class Db231DataSourceConfig { @Bean("db231DataSource") @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db231") public DataSource db231DataSource() { return DataSourceBuilder.create().build(); } @Bean("db231SqlSessionFactory") public SqlSessionFactory db231SqlSessionFactory(@Qualifier("db231DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db231/*.xml")); return bean.getObject(); } @Bean("db231SqlSessionTemplate") public SqlSessionTemplate db231SqlSessionTemplate(@Qualifier("db231SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "db231SqlSessionManager") public DataSourceTransactionManager db231SqlSessionManager() { return new DataSourceTransactionManager(db231DataSource()); } }
@Configuration @MapperScan(basePackages = "com.xxxx.mapper.db231e", sqlSessionFactoryRef = "db231eSqlSessionFactory") public class Db231eDataSourceConfig { @Bean("db231eDataSource") @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db231e") public DataSource db231eDataSource() { return DataSourceBuilder.create().build(); } @Bean("db231eSqlSessionFactory") public SqlSessionFactory db231eSqlSessionFactory(@Qualifier("db231eDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db231e/*.xml")); return bean.getObject(); } @Bean("db231eSqlSessionTemplate") public SqlSessionTemplate db231eSqlSessionTemplate(@Qualifier("db231eSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "db231eSqlSessionManager") public DataSourceTransactionManager db231eSqlSessionManager() { return new DataSourceTransactionManager(db231eDataSource()); } }
@Configuration @MapperScan(basePackages = "com.xxxx.mapper.db233", sqlSessionFactoryRef = "db233SqlSessionFactory") public class Db233DataSourceConfig { @Primary @Bean("db233DataSource") @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db233") //读取application.yml中的配置参数映射成为一个对象 public DataSource db233DataSource(){ return DataSourceBuilder.create().build(); } @Primary @Bean("db233SqlSessionFactory") public SqlSessionFactory db233SqlSessionFactory(@Qualifier("db233DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致) bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db233/*.xml")); return bean.getObject(); } @Primary @Bean("db233SqlSessionTemplate") public SqlSessionTemplate db233SqlSessionTemplate(@Qualifier("db233SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } @Primary @Bean(name = "db233SqlSessionManager") public DataSourceTransactionManager db233SqlSessionManager() { return new DataSourceTransactionManager(db233DataSource()); } }