SpringBoot整合Mybatis使用注解实现多数据源
添加依赖
pom.xml 添加以下依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency>
|
配置数据源
注意事项:db后的url注意使用jdbc-url
1 2 3 4 5 6 7
| spring.datasource.first.jdbc-url=jdbc:mysql://localhost:3306/mybatis_test spring.datasource.first.username=root spring.datasource.first.password=123456
spring.datasource.second.jdbc-url=jdbc:mysql://localhost:3306/mybatis_test1 spring.datasource.second.username=root spring.datasource.second.password=123456
|
创建MyBatis Mapper文件
db1 Mapper
1 2 3 4 5 6
| @Repository public interface FirstUserMapper {
@Select("select * from user") List<User> findUser(); }
|
db2 Mapper
1 2 3 4 5
| @Repository public interface SecondUserMapper { @Select("select * from user") List<User> findUser(); }
|
创建数据源配置
数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Configuration public class MyBatisConfigurationDatasource {
@Bean("db1") @ConfigurationProperties(prefix = "spring.datasource.first") public DataSource dataSource1() { return DataSourceBuilder.create().build(); }
@Bean("db2") @ConfigurationProperties(prefix = "spring.datasource.second") public DataSource dataSource2() { return DataSourceBuilder.create().build(); } }
|
MyBatis数据源配置
db1数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @Configuration
@MapperScan(value = "com.example.springbootmybatis.first", sqlSessionFactoryRef = "sqlSessionFactoryDb1") public class Db1Config {
@Autowired @Qualifier("db1") private DataSource dataSource;
@Bean public SqlSessionFactory sqlSessionFactoryDb1() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); try { return sqlSessionFactoryBean.getObject(); } catch (Exception e) { e.printStackTrace(); } return null; }
@Bean public SqlSessionTemplate sqlSessionTemplate1() { return new SqlSessionTemplate(sqlSessionFactoryDb1()); } }
|
db2数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @Configuration
@MapperScan(value = "com.example.springbootmybatis.first", sqlSessionFactoryRef = "sqlSessionFactoryDb1") public class Db1Config {
@Autowired @Qualifier("db1") private DataSource dataSource;
@Bean public SqlSessionFactory sqlSessionFactoryDb1() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); try { return sqlSessionFactoryBean.getObject(); } catch (Exception e) { e.printStackTrace(); } return null; }
@Bean public SqlSessionTemplate sqlSessionTemplate1() { return new SqlSessionTemplate(sqlSessionFactoryDb1()); } }
|
测试
测试接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @RestController public class TestController { @Autowired FirstUserMapper firstUserMapper;
@Autowired SecondUserMapper secondUserMapper;
@GetMapping("getUser1") public String findUser1() { List<User> user1 = firstUserMapper.findUser(); List<User> user2 = secondUserMapper.findUser(); System.out.println(user1); System.out.println(user2); Map<String, List<User>> map = new HashMap<>(); map.put("first", user1); map.put("second", user2); return JSON.toJSONString(map); } }
|
查看结果
浏览器访问localhost:8080/getUser1 看到以下两个数据源数据,可证明数据源有效:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| { "first": [ { "id": 1, "name": "name" }, { "id": 2, "name": "test" }, { "id": 3, "name": "213" } ], "second": [ { "id": 1, "name": "db2" } ] }
|
其它问题
打印日志
在applicatio.properties添加以下配置即可在控制台查看sql执行日志
1 2 3
| # 项目日志目录的日志级别 logging.level.sql=debug mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
双数据源打印日志
MyBatis配置双数据源后会导致日志不打印,修改配置日志level可解决
1 2
| logging.level.com.example.springbootmybatis=debug # 配置到具体目录
|