application.yml
server:
port: 8080
servlet:
context-path: /
spring:
application:
name: crm-full
datasource:
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
crm:
url: jdbc:mysql://192.xxx.x.xx:3306/crm?useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: admin
password: xxxxxxxxx!
xir:
url: jdbc:mysql://192.xxx.x.xx:3306/xir?useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: admin
password: xxxxxxxxx!
# 配置初始化大小(默认0)、最小、最大(默认8)
initial-size: 1
min-idle: 1
max-active: 10
# 配置获取连接等待超时的时间
max-wait: 60000
# 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大。 默认为false
pool-prepared-statements: true
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
max-open-prepared-statements: 20
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小和最大生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
max-evictable-idle-time-millis: 900000
# 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。
# 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
validation-query: SELECT 1 FROM DUAL
# 申请连接时执行validationQuery检测连接是否有效 默认为true
test-on-borrow: false
# 归还连接时执行validationQuery检测连接是否有效 默认为false
test-on-return: false
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
test-while-idle: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
两个数据源confg类
@Configuration
@MapperScan(basePackages = "com.xxxx.fullSync.dao.crm", sqlSessionFactoryRef = "crmSqlSessionFactory")
public class CrmDataSourceAutoConfiguration {
//crm mappers路径
private static final String MAPPERS_LOCAL = "classpath:mappers/crm/*.xml";
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.druid.crm")
public DruidDataSource crmDataSource() {
return new DruidDataSource();
}
@Bean
@Primary
public DataSourceTransactionManager crmTransactionManager(@Qualifier("crmDataSource") DruidDataSource crmDataSource) {
return new DataSourceTransactionManager(crmDataSource);
}
@Bean
@Primary
public SqlSessionFactory crmSqlSessionFactory(@Qualifier("crmDataSource") DruidDataSource crmDataSource) throws Exception {
final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(crmDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPERS_LOCAL));
return bean.getObject();
}
}
@Configuration
@MapperScan(basePackages = "com.xxxx.fullSync.dao.xir", sqlSessionFactoryRef = "xirSqlSessionFactory")
public class XirDataSourceAutoConfiguration {
//fullSync mappers路径
private static final String MAPPERS_LOCAL = "classpath:mappers/xir/*.xml";
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid.xir")
public DruidDataSource xirDataSource() {
return new DruidDataSource();
}
@Bean
public DataSourceTransactionManager xirTransactionManager(@Qualifier("xirDataSource") DruidDataSource xirDataSource) {
return new DataSourceTransactionManager(xirDataSource);
}
@Bean
public SqlSessionFactory xirSqlSessionFactory(@Qualifier("xirDataSource") DruidDataSource xirDataSource) throws Exception {
final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(xirDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPERS_LOCAL));
return bean.getObject();
}
}