项目结构图
搭建步骤
pom
1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>org.mybatis.spring.boot</groupId>
7 <artifactId>mybatis-spring-boot-starter</artifactId>
8 <version>2.1.1</version>
9 </dependency>
10
11 <dependency>
12 <groupId>org.projectlombok</groupId>
13 <artifactId>lombok</artifactId>
14 </dependency>
15 <dependency>
16 <groupId>mysql</groupId>
17 <artifactId>mysql-connector-java</artifactId>
18 <scope>runtime</scope>
19 </dependency>
application.yml
1server:
2 port: 8080
3spring:
4 datasource:
5 db1:
6 username: root
7 password: 123456
8 driver-class-name: com.mysql.cj.jdbc.Driver
9 jdbc-url: jdbc:mysql://localhost:3306/db_demo_sys?serverTimezone=Asia/Shanghai
10 db2:
11 username: root
12 password: 123456
13 driver-class-name: com.mysql.cj.jdbc.Driver
14 jdbc-url: jdbc:mysql://localhost:3306/db_demo_per?serverTimezone=Asia/Shanghai
上面可以看到,我访问的不同数据库,数据库语句啥的就不贴了。
数据源配置类
这里我用了2个数据源,建立2个配置类。
DBConfig1.java
1package com.lytw13.demo.config;
2
3import org.apache.ibatis.session.SqlSessionFactory;
4import org.mybatis.spring.SqlSessionFactoryBean;
5import org.mybatis.spring.SqlSessionTemplate;
6import org.mybatis.spring.annotation.MapperScan;
7import org.springframework.beans.factory.annotation.Qualifier;
8import org.springframework.boot.context.properties.ConfigurationProperties;
9import org.springframework.boot.jdbc.DataSourceBuilder;
10import org.springframework.context.annotation.Bean;
11import org.springframework.context.annotation.Configuration;
12import org.springframework.context.annotation.Primary;
13import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
14
15import javax.sql.DataSource;
16
17@Configuration
18@MapperScan(basePackages = "com.lytw13.demo.mapper.db1", sqlSessionFactoryRef = "sqlSessionFactory1")
19public class DBConfig1 {
20
21 @Bean
22 @Primary
23 @ConfigurationProperties(prefix = "spring.datasource.db1")
24 public DataSource dataSource1() {
25 return DataSourceBuilder.create().build();
26 }
27 @Bean
28 @Primary
29 public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
30 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
31 sqlSessionFactoryBean.setDataSource(dataSource);
32 sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
33 return sqlSessionFactoryBean.getObject();
34 }
35}DBConfig2.java
1package com.lytw13.demo.config;
2
3import org.apache.ibatis.session.SqlSessionFactory;
4import org.mybatis.spring.SqlSessionFactoryBean;
5import org.mybatis.spring.annotation.MapperScan;
6import org.springframework.beans.factory.annotation.Qualifier;
7import org.springframework.boot.context.properties.ConfigurationProperties;
8import org.springframework.boot.jdbc.DataSourceBuilder;
9import org.springframework.context.annotation.Bean;
10import org.springframework.context.annotation.Configuration;
11import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
12
13import javax.sql.DataSource;
14
15@Configuration
16@MapperScan(basePackages = "com.lytw13.demo.mapper.db2", sqlSessionFactoryRef = "sqlSessionFactory2")
17public class DBConfig2 {
18
19 @Bean
20 @ConfigurationProperties(prefix = "spring.datasource.db2")
21 public DataSource dataSource2() {
22 return DataSourceBuilder.create().build();
23 }
24 @Bean
25 public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2")DataSource dataSource) throws Exception {
26 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
27 sqlSessionFactoryBean.setDataSource(dataSource);
28 sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
29 return sqlSessionFactoryBean.getObject();
30 }
31}上面两个没有什么区别,就是在配置的时候指定各自的扫描位置,主数据源需要添加上@Primary注解,标识这是一个主数据源,其他没什么了。
下面的controller,service啥的就不贴了,应该都知道咋回事。
测试
分别访问 http://localhost:8080/teacher/get/1 和 http://localhost:8080/user/get/1,查看是否能取到值。
springboot_multiDatasource_ceshi.png
可能会遇到的错误
Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found
1Description:
2
3Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:
4 - sqlSessionFactory1: defined by method 'sqlSessionFactory1' in class path resource [com/lytw13/demo/config/DBConfig1.class]
5 - sqlSessionFactory2: defined by method 'sqlSessionFactory2' in class path resource [com/lytw13/demo/config/DBConfig2.class]
6
7
8Action:
9
10Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
解决方法:
查看在设置sqlsessionfactory的时候有没有使用@Qualifier注解指定datasource;
查看是否忘记主数据源是否忘记添加@Primary注解。
我这里就是忘记添加注解了,疏忽了。
Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
1There was an unexpected error (type=Internal Server Error, status=500).
2nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName. ### The error may exist in file [E:\lytw13\demo_multidatasource\target\classes\mapper\db1\UserMapper.xml] ### The error may involve com.lytw13.demo.mapper.db1.UserMapper.selectByPrimaryKey ### The error occurred while executing a query ### Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
解决方法:
查看application.yml配置文件,url是否是使用的jdbc-url,使用url会出现上面的错误,将url改为jdbc-url。
原因不清楚一直,哪位大佬知道可以评论区告诉我写,多谢了。