项目中需要使用多个数据源,可进行一下配置。
1.首先在配置文件application.yaml中配置多个数据源。
spring:
datasource:
online:
name: online
jdbc-url: jdbc:mysql://127.0.0.1:3306/xxxuseSSL=false&characterEncoding=utf8&autoReconnect=true
username: root
password: xx
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
test:
name: test
jdbc-url: jdbc:mysql://127.0.0.1:3306/xxx-finance?useSSL=false&characterEncoding=utf8&autoReconnect=true
username: root
password: xx.x
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.lj.cmslive.model # 注意:对应实体类的路径
#日志相关配置
logging:
level:
root : info
file: ./log/scrawl.log
2.新建配置文件类。
package com.linjiao.cmslive.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* # @Configuration 注册到springboot容器,相当于原来xml文件里的<beans>
* # @@MapperScan下面要进行扫包,目的是标清楚为谁添加的数据源,这样对应的包里函数执行数据库操作的时候,
* 就知道要执行的数据库账号密码,表,以及事务处理之类的。
*/
@Configuration
@MapperScan(value = "com.linjiao.cmslive.mapper.online", sqlSessionFactoryRef = "onlineSqlSessionFactory")
public class OnlineDataSourceConfig {
/**
* # @Bean(name = "x") 注入到这个容器
* # @Qualifier("xxx") 的含义是告诉他使用哪个DataSource
* # @ConfigurationProperties(prefix ="") 表示取配置文件中的前缀
*/
@Bean(name = "onlineDataSource")
@Qualifier("onlineDataSource")
@ConfigurationProperties(prefix = "spring.datasource.online")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
/**
* # @Primary 因为有多个数据源,在没有明确指定用哪个的情况下,会用带有primary的,这个注解必须有一个数据源要添加
*
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "onlineSqlSessionFactory")
@Primary
public SqlSessionFactory sentinelSqlSessionFactory(@Qualifier("onlineDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/online/*.xml"));
return bean.getObject();
}
}
3.同理配置其他的数据源。在service中注入mapper就可以使用了。