application.yml配置文件
#durid数据库连接池设置
spring:
profiles:
##此处引入配置其他数据源配置文件
include: dev-datasource
aop:
proxy-target-class: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/primarydatasource?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 15
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j2
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
druid:
remove-abandoned: true
remove-abandoned-timeout: 180
log-abandoned: true
application-dev-datasource.yml配置文件 (个人习惯单独写了个yml,再增加数据源可以在这里配置)
## 第二个数据源
spring:
datasource2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/seconddatasource?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
主数据源配置代码
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Primary
@Configuration
@MapperScan(basePackages = "com.btwl.primary.mapper" , sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSource {
@Bean(name = "DataSource_primary")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("DataSource_primary") DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("DataSource_primary") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二个数据源配置代码
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.btwl.second.mapper" , sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSource {
@Bean(name = "DataSource_second")
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource secondDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("DataSource_second") DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager secondTransactionManager(@Qualifier("DataSource_second") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Primary:指定为默认数据源,没有该注解会报错,系统找不到默认数据源
@MapperScan:扫描指定包的mapper作为对应数据源,建议每个数据源都用不同的包区分
@Qualifier:与@Autowired类似,用作区分如果存在多个实现类要指定要注入哪个 参数为指定Bean的name
spring-database-jdbc-url 不要写成 spring-database-url 会报错 exception : jdbcUrl is required with driverClassName,(嗯,不知道为啥
业务逻辑代码就不放了跟其他的没什么区别,再添加数据源的话就按以上配置再加一个就好了,
网友评论