1、项目目录图示:
2、案例依赖:
<parent> <artifactId>spring-boot-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.2.12.RELEASE</version> </parent> <dependencies> <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>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.14</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
3、案例配置文件yml:
server: port: 8099 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource db1: jdbc-url: jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver db2: jdbc-url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver druid: # druid数据库连接池的基本初始化属性 initial-size: 5 # 连接池初始化的大小 min-idle: 1 # 最小空闲的线程数 max-active: 20 # 最大活动的线程数 mybatis-plus: mapper-locations: classpath:/mapper/*/*.xml # 配置MyBatis-Plus扫描Mapper文件的位置 type-aliases-package: com.hua.bean # 创建别名的类所在的包 configuration: map-underscore-to-camel-case: true # 开启驼峰命名 #sql语句打印 logging: level: #路径 com.hua: debug
4、重点 ,SqlSessionFactory的配置如下:(MySQLDataSourceConfig1类)
@Configuration @MapperScan(basePackages = "com.hua.mapper.db1", sqlSessionFactoryRef = "MySQLSqlSessionFactory1") public class MySQLDataSourceConfig1 { @Bean(name = "MySQLDataSource1") @Primary @ConfigurationProperties(prefix = "spring.datasource.db1") public DataSource getDateSource1() { return DataSourceBuilder.create().build(); } @Bean(name = "MySQLSqlSessionFactory1") @Primary public SqlSessionFactory test1SqlSessionFactory( @Qualifier("MySQLDataSource1") DataSource datasource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean (); bean.setDataSource(datasource); bean.setMapperLocations(// 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml")); return bean.getObject(); } @Bean("MySQLSqlSessionTemplate1") @Primary public SqlSessionTemplate test1SqlSessionTemplate( @Qualifier("MySQLSqlSessionFactory1") SqlSessionFactory sessionFactory) { return new SqlSessionTemplate(sessionFactory); } @Bean public PlatformTransactionManager transactionManager1(@Qualifier("MySQLDataSource1")DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
4.5、第二个(多个数据源则多个这种配置类) ,SqlSessionFactory的配置如下:(MySQLDataSourceConfig2类)
@Configuration @MapperScan(basePackages = "com.hua.mapper.db2", sqlSessionFactoryRef = "MySQLSqlSessionFactory2") public class MySQLDataSourceConfig2 { @Bean(name = "MySQLDataSource2") @ConfigurationProperties(prefix = "spring.datasource.db2") public DataSource getDateSource2() { return DataSourceBuilder.create().build(); } @Bean(name = "MySQLSqlSessionFactory2") public SqlSessionFactory test2SqlSessionFactory( @Qualifier("MySQLDataSource2") DataSource datasource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean (); bean.setDataSource(datasource); bean.setMapperLocations(// 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml")); return bean.getObject(); } @Bean("MySQLSqlSessionTemplate2") public SqlSessionTemplate test2SqlSessionTemplate( @Qualifier("MySQLSqlSessionFactory2") SqlSessionFactory sessionFactory) { return new SqlSessionTemplate(sessionFactory); } // 此处根据数据库的类型来配置,在MySQLDataSourceConfig1类中已经配置了mysql的事务类型,如此处数据库类型也是mysql则不用配置,否则将注释放开正常配置即可 // @Bean // public PlatformTransactionManager transactionManager2(@Qualifier("MySQLDataSource2")DataSource dataSource) { // return new DataSourceTransactionManager(dataSource); // } }
相同的数据库类型PlatformTransactionManager事务管理器只需配置一次加入ioc即可不用重复配置,不同数据库类型则需要再次配置bean加入容器(放开上面代码注释即可)。
补充:启动类上加入注解取消datasource的自动配置
5、测试代码:
注解方式Spring Boot配置多数据源操作参考:文章来源:https://uudwc.com/A/9dE55
注解方式Spring Boot配置多数据源操作_O_G的博客-CSDN博客文章来源地址https://uudwc.com/A/9dE55