参考官网:多数据源 | MyBatis-Plus (baomidou.com)
使用方法
1、引入dynamic-datasource-spring-boot-starter。
1 <dependency> 2 <groupId>com.baomidou</groupId> 3 <artifactId>dynamic-datasource-spring-boot-starter</artifactId> 4 <version>3.5.2</version> 5 </dependency>
2、配置数据源。
1 spring: 2 datasource: 3 dynamic: 4 primary: master #设置默认的数据源或者数据源组,默认值即为master 5 strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 6 datasource: 7 master: 8 url: jdbc:mysql://127.0.0.1:3306/test_mybatis_plus?allowPublicKeyRetrieval=true&useSSL=false 9 username: root 10 password: 123456 11 driver-class-name: com.mysql.cj.jdbc.Driver 12 slave_1: 13 url: jdbc:mysql://127.0.0.1:3306/test_mybatis_plus2?allowPublicKeyRetrieval=true&useSSL=false 14 username: root 15 password: 123456 16 driver-class-name: com.mysql.cj.jdbc.Driver 17 18 19 mybatis-plus: 20 configuration: 21 # 日志输出SQL 22 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 23 # mapper xml文件默认位置:classpath*:/mapper/**/*.xml 24 # mapper-locations: classpath*:/mapper/**/*.xml 25 # 配置类型别名对应的包 26 # type-aliases-package: com.test.mybatisplus.entity
3、使用 @DS 切换数据源
@DS 可以注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解。
注解 | 结果 |
---|---|
没有@DS | 默认数据源 |
@DS("dsName") | dsName可以为组名也可以为具体某个库的名称 |
1 @DS("master") 2 public interface EmployeeMapper extends BaseMapper<Employee> { 3 4 }
1 @DS("slave_1") 2 public interface ProductMapper extends BaseMapper<Product> { 3 4 }
4、测试类
1 @SpringBootTest 2 public class SampleTest { 3 4 @Autowired 5 private EmployeeMapper employeeMapper; 6 @Autowired 7 private ProductMapper productMapper; 8 9 10 @Test 11 public void testSelectList() { 12 System.out.println(("----- selectAll method test ------")); 13 List<Employee> userList = employeeMapper.selectList(null); 14 userList.forEach(System.out::println); 15 List<Product> productList = productMapper.selectList(null); 16 productList.forEach(System.out::println); 17 } 18 19 }
5、运行测试效果
----- selectAll method test ------ Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6bbab114] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@64364705 wrapping com.mysql.cj.jdbc.ConnectionImpl@4f486211] will not be managed by Spring ==> Preparing: SELECT id,last_name,email,age FROM employee ==> Parameters: <== Columns: id, last_name, email, age <== Row: 1, Jone, test1@baomidou.com, 18 <== Row: 2, Jack, test2@baomidou.com, 20 <== Row: 3, Tom, test3@baomidou.com, 28 <== Row: 4, Sandy, test4@baomidou.com, 21 <== Row: 5, Billie, test5@baomidou.com, 24 <== Total: 5 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6bbab114] Employee(id=1, lastName=Jone, email=test1@baomidou.com, age=18) Employee(id=2, lastName=Jack, email=test2@baomidou.com, age=20) Employee(id=3, lastName=Tom, email=test3@baomidou.com, age=28) Employee(id=4, lastName=Sandy, email=test4@baomidou.com, age=21) Employee(id=5, lastName=Billie, email=test5@baomidou.com, age=24) Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@603cabc4] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@1744457797 wrapping com.mysql.cj.jdbc.ConnectionImpl@6f347d7] will not be managed by Spring ==> Preparing: SELECT id,name,price,version FROM t_product ==> Parameters: <== Columns: id, name, price, version <== Row: 1, 外星人笔记本, 100, 2 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@603cabc4] Product(id=1, name=外星人笔记本, price=100, version=2) 2024-01-19 23:29:43.903 INFO 41640 --- [ionShutdownHook] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource start closing .... 2024-01-19 23:29:43.903 INFO 41640 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : master - Shutdown initiated... 2024-01-19 23:29:43.918 INFO 41640 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : master - Shutdown completed. 2024-01-19 23:29:43.918 INFO 41640 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : slave_1 - Shutdown initiated... 2024-01-19 23:29:43.918 INFO 41640 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : slave_1 - Shutdown completed. 2024-01-19 23:29:43.918 INFO 41640 --- [ionShutdownHook] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource all closed success,bye