pom.xml
#指定当前环境
#spring.profiles.active=dev
#tomcat
server.port=8080
server.servlet.context-path=/south
#注册与发现服务
#spring.application.name=service-pc
#eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#debug=true
logging.level.com.nanty.core.system.mapper=debug
#映射文件路径,支持Ant风格的通配符,多个配置可以使用英文逗号隔开
mybatis.mapper-locations=classpath:mapper/*.xml
#类型别名包配置,只能指定具体包,多个配置可以使用英文逗号隔开
mybatis.type-aliases-package=**.entity
#自动将以下滑线命名的数据库映射到java对象的驼峰式命名属性中。默认为false
mybatis.configuration.map-underscore-to-camel-case=true
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
# 服务端口
#server.port=8082
# 数据库链接驱动
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
# 数据库url
spring.datasource.db1.url=jdbc:mysql://localhost:3306/yifangshipin?useUnicode=true&allowMultiQueries=true
spring.datasource.db1.username=root
spring.datasource.db1.password=123
# 服务端口
#server.port=8082
# 数据库链接驱动
spring.datasource.db2.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
# 数据库url
spring.datasource.db2.url=jdbc:sqlserver://localhost:1433;databasename=KF_CSMD_Data
spring.datasource.db2.username=sa
spring.datasource.db2.password=123
# 服务端口
#server.port=8082
# 数据库链接驱动
spring.datasource.db3.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
# 数据库url
spring.datasource.db3.url=jdbc:sqlserver://localhost:1433;databasename=ChwareEFong
spring.datasource.db3.username=sa
spring.datasource.db3.password=123
# 数据库类型
spring.datasource.db1.jpa.database=mysql
spring.datasource.db2.jpa.database=mysql
spring.datasource.db3.jpa.database=mysql
# 显示sql语句
spring.datasource.db1.jpa.show-sql=true
spring.datasource.db2.jpa.show-sql=true
spring.datasource.db3.jpa.show-sql=true
# 更新表结构
spring.datasource.db1.jpa.hibernate.ddl-auto=update
spring.datasource.db2.jpa.hibernate.ddl-auto=update
spring.datasource.db3.jpa.hibernate.ddl-auto=update
spring.datasource.db1.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.db2.jpa.database-platfor=org.hibernate.dialect.SQLServer2008Dialect
spring.datasource.db3.jpa.database-platfor=org.hibernate.dialect.SQLServer2008Dialect
#druid配置 http://localhost:8080/druid/login.html
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 初始化连接大小[0]
spring.datasource.db1.druid.initial-size=1
spring.datasource.db2.druid.initial-size=1
spring.datasource.db3.druid.initial-size=1
# 最小空闲连接数[0]
spring.datasource.db1.druid.min-idle=1
spring.datasource.db2.druid.min-idle=1
spring.datasource.db3.druid.min-idle=1
# 最大连接数[8]
spring.datasource.db1.druid.max-active=20
spring.datasource.db2.druid.max-active=20
spring.datasource.db3.druid.max-active=20
# 配置获取连接等待超时的时间(毫秒)[-1]
spring.datasource.db1.druid.max-wait=60000
spring.datasource.db2.druid.max-wait=60000
spring.datasource.db3.druid.max-wait=60000
# 查询超时时间(秒)
spring.datasource.db1.druid.query-timeout=90
spring.datasource.db2.druid.query-timeout=90
spring.datasource.db3.druid.query-timeout=90
# 用来检测连接是否有效的sql,要求是一个查询语句
spring.datasource.db1.druid.validation-query=SELECT 'x'
spring.datasource.db2.druid.validation-query=SELECT 'x'
spring.datasource.db3.druid.validation-query=SELECT 'x'
# 申请连接时检测连接可用性[false]
spring.datasource.db1.druid.test-on-borrow=false
spring.datasource.db2.druid.test-on-borrow=false
spring.datasource.db3.druid.test-on-borrow=false
# 归还连接检测[false]
spring.datasource.db1.druid.test-on-return=false
spring.datasource.db2.druid.test-on-return=false
spring.datasource.db3.druid.test-on-return=false
# 超时是否检测连接可用性[true]
spring.datasource.db1.druid.test-while-idle=true
spring.datasource.db2.druid.test-while-idle=true
spring.datasource.db3.druid.test-while-idle=true
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接 (毫秒)
spring.datasource.db1.druid.time-between-eviction-runs-millis=60000
spring.datasource.db2.druid.time-between-eviction-runs-millis=60000
spring.datasource.db3.druid.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间(毫秒,默认30分钟)
spring.datasource.db1.druid.min-evictable-idle-time-millis=300000
spring.datasource.db2.druid.min-evictable-idle-time-millis=300000
spring.datasource.db3.druid.min-evictable-idle-time-millis=300000
将连接spring.datasource.db1.url=jdbc:mysql://localhost:3306/yifangshipin?useUnicode=true&allowMultiQueries=true 数据库中的 Mapper.java 文件放在 db1文件目录下
项目文件目录
DataSource1Config.java 配置第一个数据库
package com.nanty.core.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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;
/**
* 多数据源配置 druid
* @version 1.0
* @author ling 2020-04-30
**/
@Configuration
@tk.mybatis.spring.annotation.MapperScan(basePackages = DataSource1Config.PACKAGE, sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSource1Config {
static final String PACKAGE = "com.nanty.core.system.mapper.db1";
static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
// static final String MAPPER_LOCATION = "classpath:mapper/bb/*.xml";
@Value("${spring.datasource.db1.url}")
private String url;
@Value("${spring.datasource.db1.username}")
private String user;
@Value("${spring.datasource.db1.password}")
private String password;
@Value("${spring.datasource.db1.driver-class-name}")
private String driverClass;
@Bean(name = "db1DataSource")
@Primary
public DataSource db1DataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "db1TransactionManager")
@Primary
public DataSourceTransactionManager db1TransactionManager() {
return new DataSourceTransactionManager(db1DataSource());
}
@Bean(name = "db1SqlSessionFactory")
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource db1DataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(db1DataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
// sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
将
之后添加数据库的格式 每添加一个数据库 便加一个DataSourceConfig.java 配置
除了第一个DataSourceConfig.java 配置 需要@Primary注解
其余配置不需要添加@Primary注解
package com.nanty.core.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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;
/**
* 多数据源配置 druid
* @version 1.0
* @author ling 2020-04-30
**/
@Configuration
@tk.mybatis.spring.annotation.MapperScan(basePackages = DataSource2Config.PACKAGE, sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSource2Config {
static final String PACKAGE = "com.nanty.core.system.mapper.db2";
static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
@Value("${spring.datasource.db2.url}")
private String url;
@Value("${spring.datasource.db2.username}")
private String user;
@Value("${spring.datasource.db2.password}")
private String password;
@Value("${spring.datasource.db2.driver-class-name}")
private String driverClass;
@Bean(name = "db2DataSource")
public DataSource db2DataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "db2TransactionManager")
public DataSourceTransactionManager db2TransactionManager() {
return new DataSourceTransactionManager(db2DataSource());
}
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource db2DataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(db2DataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
return sessionFactory.getObject();
}