整体结构图 springboot 2.1.2
application.yml 配置文件 此处有两个数据源 primary和merchant ,连接两台不同ip的服务器数据库
server:
port: 8080
spring:
datasource:
primary: #唯一标识
url: jdbc:mysql://192.168.12.100:3306/portal?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
name: primaryDB
initialSize: 20 #初始化大小
minIdle: 20 #最小
maxActive: 300 #最大
maxWait: 10000 #等待超时时间
timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒
validationQuery: SELECT 1
testWhileIdle: true #是否在连接空闲一段时间后检测其可用性
testOnBorrow: false #是否在获得连接后检测其可用性
testOnReturn: false #是否在连接放回连接池后检测其可用性
poolPreparedStatements: true #打开PSCache,并且指定每个连接上PSCache的大小
maxPoolPreparedStatementPerConnectionSize: 15
filters: stat,wall,log4j2 #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
type: com.alibaba.druid.pool.DruidDataSource
merchant:
url: jdbc:mysql://192.168.12.169:3306/mxwi?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
name: merchantDB
initialSize: 5 #初始化大小
minIdle: 5 #最小
maxActive: 20 #最大
maxWait: 60000 #等待超时时间
timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
minEvictableIdleTimeMillis: 30000 # 配置一个连接在池中最小生存的时间,单位是毫秒
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true #是否在连接空闲一段时间后检测其可用性
testOnBorrow: false #是否在获得连接后检测其可用性
testOnReturn: false #是否在连接放回连接池后检测其可用性
poolPreparedStatements: true #打开PSCache,并且指定每个连接上PSCache的大小
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j2 #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
type: com.alibaba.druid.pool.DruidDataSource
#若还有其他的数据源,只需要继续添加,同时还要添加一个数据源配置类,稍作修改即可
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.htkg.enity.*
若还有其他的数据源,只需要继续添加,同时还要添加一个数据源配置类,稍作修改即可
MerchantDataSourceConfig配置类,用来读取主数据的数据
package com.example.config;
import com.alibaba.druid.pool.DruidDataSource;
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.boot.context.properties.ConfigurationProperties;
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;
/**
* @Author: Journey
* @Description: merchant数据源
* @Date: 2019/6/25 15:53
* @Version: 1.0.0
**/
@Configuration //此处指向DAO接口
@MapperScan(basePackages = "com.example.dao.merchant", sqlSessionTemplateRef = "merchantSqlSessionTemplate")
public class MerchantDataSourceConfig {
@Bean(name = "merchantDataSource") //此处指向yml配置文件中具体的数据源
@ConfigurationProperties(prefix = "spring.datasource.merchant")
public DataSource testDataSource() {
return new DruidDataSource();
}
@Bean(name = "merchantSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("merchantDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/merchantDB/*.xml"));//指定mapper.xml路径
//增加驼峰配置
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "merchantTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("merchantDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "merchantSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("merchantSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
SpmctDataSourceConfig 配置类 主要读取副数据库数据
package com.example.config;
import com.alibaba.druid.pool.DruidDataSource;
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.boot.context.properties.ConfigurationProperties;
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;
/**
* @Author: Journey
* @Description: primary数据源
* @Date: 2019/6/25 15:53
* @Version: 1.0.0
**/
@Configuration //此处指向的DAO实现类
@MapperScan(basePackages = "com.example.dao.primary", sqlSessionTemplateRef = "spmctSqlSessionTemplate")
public class SpmctDataSourceConfig {
@Bean(name = "spmctDataSource") //指定yml文件中具体的数据源
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource testDataSource() {
return new DruidDataSource();
}
@Bean(name = "spmctSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("spmctDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/primaryDB/*.xml"));//指定mapper.xml路径
//增加驼峰配置
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "spmctTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("spmctDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "spmctSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("spmctSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
同时还要导入阿里巴巴连接驱动的maven坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>