springboot+mybatis配置双数据源(mysql,oracle,sqlserv。。。
以上如图为项⽬⽂件夹⽬录。
1,⾸先配置l 配置⽂件
server:
port: 8084
tomcat:
uri-encoding: utf-8
servlet:
context-path: /services
#spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
# # name: skynet
# driver-class-name: sql.jdbc.Driver
# url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false # username: root
# password: root
#
#
# jpa:
# show-sql: true #是否在控制台打印sql语句
# ⾃定义的mysql主数据源配置信息
primary:
datasource:
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driverClassName: sql.jdbc.Driver
#配置基本属性
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: root
#配置初始化⼤⼩/最⼩/最⼤
initialSize: 1
minIdle: 1
maxActive: 20
#获取连接等待超时时间
maxWait: 60000
#间隔多久进⾏⼀次检测,检测需要关闭的空闲连接
timeBetweenEvictionRunsMillis: 60000
#⼀个连接在池中最⼩⽣存的时间
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
#打开PSCache,并指定每个连接上PSCache的⼤⼩。oracle设为true,mysql设为false。分库分表较多推荐设置为false
poolPreparedStatements: false
maxPoolPreparedStatementPerConnectionSize: 20
# ⾃定义的sqlserver数据源配置信息或者oracle, db2 数据库,只需要更改driverClassName以及数据库基本属性
back:
datasource:
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
#配置基本属性
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=tiuweb_drgs
username: admin
password: 123
#配置初始化⼤⼩/最⼩/最⼤
initialSize: 1
minIdle: 1
maxActive: 20
#获取连接等待超时时间
maxWait: 60000
#间隔多久进⾏⼀次检测,检测需要关闭的空闲连接
timeBetweenEvictionRunsMillis: 60000
#⼀个连接在池中最⼩⽣存的时间
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
#打开PSCache,并指定每个连接上PSCache的⼤⼩。oracle设为true,mysql设为false。分库分表较多推荐设置为false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
resources:
static-locations: classpath:/static/,classpath:/views/
#mybatis
mybatis:
mapper-locations: classpath:mapper/**/*.xml
configuration:
map-underscore-to-camel-case: true
default-fetch-size: 500
default-statement-timeout: 80
# mybatis 分页插件需要配置l坐标
pagehelper:
helperDialect: sqlserver
reasonable: true
supportMethodsArguments: true
pageSizeZero: true
params: count=countSql
#logback⽇志配置需要配置l坐标以及在resources下配置config配置⽂件
logging:
config: classpath:l
level:
com.tiuweb.skynet.dao: DEBUG
file:
path: DRGS_LOG #⽇志保存⽂件夹
2,配置config数据源配置⽂件 PrimaryDataBaseConfig 和 BackDataBaseConfig
package com.fig;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.apache.ibatis.session.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.SQLException;
@Data
@Configuration
@Component
// 前缀为primary.datasource.druid的配置信息
@ConfigurationProperties(prefix = "primary.datasource.druid")
@MapperScan(basePackages = PrimaryDataBaseConfig.PACKAGE, sqlSessionFactoryRef = "primarySqlSessionFactory") public class PrimaryDataBaseConfig {
/**
* dao层的包路径
*/
static final String PACKAGE = "com.tw.skynet.dao";
/**
* mapper⽂件的相对路径
*/
private static final String MAPPER_LOCATION = "classpath:mapper/primary/*.xml";
private String filters;
jpa mybatisprivate String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
private long maxWait;
private long timeBetweenEvictionRunsMillis;
private long minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
/
/ 主数据源使⽤@Primary注解进⾏标识
@Primary
@Bean(name = "primaryDataSource")
public DataSource primaryDataSource() throws SQLException {
DruidDataSource druid = new DruidDataSource();
// 监控统计拦截的filters
druid.setFilters(filters);
// 配置基本属性
druid.setDriverClassName(driverClassName);
druid.setUsername(username);
druid.setPassword(password);
druid.setUrl(url);
//初始化时建⽴物理连接的个数
druid.setInitialSize(initialSize);
//最⼤连接池数量
druid.setMaxActive(maxActive);
//最⼩连接池数量
druid.setMinIdle(minIdle);
//获取连接时最⼤等待时间,单位毫秒。
druid.setMaxWait(maxWait);
//间隔多久进⾏⼀次检测,检测需要关闭的空闲连接
druid.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
/
/⼀个连接在池中最⼩⽣存的时间
druid.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//⽤来检测连接是否有效的sql
druid.setValidationQuery(validationQuery);
//建议配置为true,不影响性能,并且保证安全性。
druid.setTestWhileIdle(testWhileIdle);
//申请连接时执⾏validationQuery检测连接是否有效
druid.setTestOnBorrow(testOnBorrow);
druid.setTestOnReturn(testOnReturn);
//是否缓存preparedStatement,也就是PSCache,oracle设为true,mysql设为false。分库分表较多推荐设置为false
druid.setPoolPreparedStatements(poolPreparedStatements);
// 打开PSCache时,指定每个连接上PSCache的⼤⼩
druid.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
return druid;
}
// 创建该数据源的事务管理
@Primary
@Bean(name = "primaryTransactionManager")
public DataSourceTransactionManager primaryTransactionManager() throws SQLException {
return new DataSourceTransactionManager(primaryDataSource());
}
// 创建Mybatis的连接会话⼯⼚实例
@Primary
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(primaryDataSource); // 设置数据源bean
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(PrimaryDataBaseConfig.MAPPER_LOCATION)); // 设置mapper⽂件路径
Object();
}
}
package com.fig;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.apache.ibatis.session.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* @Description: 后台数据源配置类
*/
@Data
@Configuration
@Component
@ConfigurationProperties(prefix = "back.datasource.druid")
@MapperScan(basePackages = BackDataBaseConfig.PACKAGE, sqlSessionFactoryRef = "backSqlSessionFactory")
public class BackDataBaseConfig {
/**
* dao层的包路径
*/
static final String PACKAGE = "com.tw.skynet.dao.back";
/**
* mapper⽂件的相对路径
*/
private static final String MAPPER_LOCATION = "classpath:mapper/back/*.xml";
private String filters;
private String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
private long maxWait;
private long timeBetweenEvictionRunsMillis;
private long minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
@Bean(name = "backDataSource")
public DataSource backDataSource() throws SQLException {
DruidDataSource druid = new DruidDataSource();
// 监控统计拦截的filters
druid.setFilters(filters);
// 配置基本属性
druid.setDriverClassName(driverClassName);
druid.setUsername(username);
druid.setPassword(password);
druid.setUrl(url);
//初始化时建⽴物理连接的个数
druid.setInitialSize(initialSize);
//最⼤连接池数量
druid.setMaxActive(maxActive);
//最⼩连接池数量
druid.setMinIdle(minIdle);
//获取连接时最⼤等待时间,单位毫秒。
druid.setMaxWait(maxWait);
/
/间隔多久进⾏⼀次检测,检测需要关闭的空闲连接
druid.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//⼀个连接在池中最⼩⽣存的时间
druid.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//⽤来检测连接是否有效的sql
druid.setValidationQuery(validationQuery);
//建议配置为true,不影响性能,并且保证安全性。
druid.setTestWhileIdle(testWhileIdle);
//申请连接时执⾏validationQuery检测连接是否有效
druid.setTestOnBorrow(testOnBorrow);
druid.setTestOnReturn(testOnReturn);
/
/是否缓存preparedStatement,也就是PSCache,oracle设为true,mysql设为false。分库分表较多推荐设置为false
druid.setPoolPreparedStatements(poolPreparedStatements);
// 打开PSCache时,指定每个连接上PSCache的⼤⼩
druid.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
return druid;
}
@Bean(name = "backTransactionManager")
public DataSourceTransactionManager backTransactionManager() throws SQLException {
return new DataSourceTransactionManager(backDataSource());
}
@Bean(name = "backSqlSessionFactory")
public SqlSessionFactory backSqlSessionFactory(@Qualifier("backDataSource") DataSource backDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(backDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(BackDataBaseConfig.MAPPER_LOCATION));
Object();
}
}
3,以上配置就完成了,接下来就是dao⽂件夹和mapper⽂件夹分包
现在所有配置已经完了,最后不要忘记l 导⼊所需要的数据库驱动jar包
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论