springBoot+ druid配置多数据源
1.在yml加:
spring:#1.JDBC数据源datasource:druid:first:username: PYpassword: ral2024url: jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=falsedriver-class-name: com.mysql.jdbc.Driver#初始化连接池的连接数量 大小,最小,最大initial-size: 5min-idle: 5max-active: 20#配置获取连接等待超时的时间max-wait: 60000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 30000# 配置一个连接在池中最大生存的时间,单位是毫秒max-evictable-idle-time-millis: 3000000validation-query: SELECT 1 FROM usertest-while-idle: truetest-on-borrow: truetest-on-return: false# 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,slf4jfilter:stat:merge-sql: trueslow-sql-millis: 5000second:username: L2_USER_Z1password: L2_USER_Z1url: jdbc:oracle:thin:@172.16.200.60:1521:PYCXMES.PYGT.COMdriver-class-name: oracle.jdbc.OracleDriver#初始化连接池的连接数量 大小,最小,最大initial-size: 5min-idle: 5max-active: 20#配置获取连接等待超时的时间max-wait: 600000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 30000# 配置一个连接在池中最大生存的时间,单位是毫秒max-evictable-idle-time-millis: 3000000validation-query: SELECT 1 FROM usertest-while-idle: truetest-on-borrow: truetest-on-return: false# 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,slf4jfilter:stat:merge-sql: true###slow-sql-millis: 5000
2.新建一个config文件
里面有3个类,分别:DruidConfigFirst ,DruidConfigSecond ,TwoDataSourceConfig
结构如图:
package com.alnan.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.alnan.dao.mysql", sqlSessionTemplateRef = "firstSqlSessionTemplate")
public class DruidConfigFirst {@Bean(name = "firstSqlSessionFactory")@Primarypublic SqlSessionFactory sqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}@Bean(name = "firstTransactionManager")@Primarypublic DataSourceTransactionManager transactionManager(@Qualifier("firstDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "firstSqlSessionTemplate")@Primarypublic SqlSessionTemplate sqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}
package com.alnan.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.alnan.dao.oracle", sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class DruidConfigSecond {@Bean(name = "secondSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}@Bean(name = "secondTransactionManager")public DataSourceTransactionManager transactionManager(@Qualifier("secondDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "secondSqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}
package com.alnan.config;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.sql.DataSource;@Configuration
public class TwoDataSourceConfig {@Primary@Bean(name = "firstDataSource")@ConfigurationProperties("spring.datasource.druid.first")public DataSource first() {return DruidDataSourceBuilder.create().build();}@Bean(name = "secondDataSource")@ConfigurationProperties("spring.datasource.druid.second")public DataSource second() {return DruidDataSourceBuilder.create().build();}
}
然后怎么用呢:
dao 里面分开对应mysql的Mapper,oracle的Mapper
应用:正常注入就行,他会自己去对应数据库
3.pom:
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency>