在 SpringBoot 中实现多数据源访问的最佳实践( 四 )

  • 测试 test_get 方法,输出如下
12:43:06.033 [main] INFOc.c.config.MultiDataSourceHolder - Thread main set datasource order12:43:06.042 [main] INFOcom.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...12:43:06.359 [main] INFOcom.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.12:43:06.516 [main] INFOc.c.config.MultiDataSourceHolder - Thread main unset datasource order1从上面可见,在数据源加载前,先修改了 datasource key 为 order 数据库 。
  • TestUserService 测试如下
import com.ckjava.service.TUserService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class TestUserService { @Autowired private TUserService tUserService; @Test public void test_get() {tUserService.get(1L).ifPresent(entity -> {System.out.println(entity.getId());}); }}
  • 执行 test_get 方法,输出如下
12:45:30.389 [main] INFOc.c.config.MultiDataSourceHolder - Thread main set datasource user12:45:30.403 [main] INFOcom.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...12:45:30.906 [main] INFOcom.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.12:45:31.159 [main] INFOc.c.config.MultiDataSourceHolder - Thread main unset datasource user1从输出结果看,在数据源加载前,先修改了 datasource key 为 user 数据库 。
6 代码例子的完整代码:gitee spring-boot-multidb




推荐阅读