最近由于项目中需要同时连接两个数据库,所以需要配置多个数据源,由于springboot 默认帮我们做了很多自动配置,所以在配置的时候就遇到了问,稍后我们会讨论这个问题。
项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
数据库不同源配置文件
master连接的是test库,slave连接的是haozi,这里的命名并没有主从关系,只是为了区分
datasource.master.url=jdbc:mysql://127.0.0.1:3306/test
datasource.master.driverClassName=com.mysql.jdbc.Driver
datasource.master.username=root
datasource.master.password=
datasource.slave.url=jdbc:mysql://127.0.0.1:3306/haozi
datasource.slave.driverClassName=com.mysql.jdbc.Driver
datasource.slave.username=root
datasource.slave.password=
Master Mybatis配置
@Configuration
@MapperScan(basePackages = "com.example.mapper.master",sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
@Bean(name = "masterDataSource")
@ConfigurationProperties("datasource.master")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/master/*.xml"));
return sessionFactoryBean.getObject();
}
}
Slave Mybatis配置
@Configuration
@MapperScan(basePackages = "com.example.mapper.slave",sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveSourceConfig {
@Bean(name = "slaveDataSource")
@ConfigurationProperties("datasource.slave")
public DataSource slaveDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/slave/*.xml"));
return sessionFactoryBean.getObject();
}
}
结构图及对应关系分析
structure1.png这里的mapper目录下的master和slave主要存放的是mapper接口,被上面的DataSourceConfig上的@MapperScan注解所烧面,这里面我把master和slave的mapper接口放在不同的子目录下了。
structure1.png这里分别放的是和上面mapper接口对应的.xml文件,被上面的配置来扫描,然后通过setMapperLocations注册到SqlSessionFactory中。
可能遇到的问题
- mybatis required a single bean, but 2 were found
这是因我们这里配置了两个数据源,springboot会自动的注入,但是注入的时候发现有两个,所以不知道选择哪一个了,因为我们要关闭springboot关于mybatis的一些自动注入,在@SpringBootApplication注解下面,加入一个@EnableAutoConfiguration(exclude=""),具体如下:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
第一个是数据源,第二个是数据源的事务管理器,第三个是和SessionFactory相关的。
网友评论