1.引入依赖
gradle文件配置,用maven引入也可以
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'com.alibaba:druid-spring-boot-starter:1.1.10'
2.配置类
DataSourceConfig.java
package com.example;
import com.alibaba.druid.pool.DruidDataSource;
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.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
@Primary
@Bean("dataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.one")
public DataSource dataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean("dataSourceTwo")
@ConfigurationProperties(prefix = "spring.datasource.druid.two")
public DataSource dataSourceTwo() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Primary
@Bean("jdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean("jdbcTemplateTwo")
public JdbcTemplate jdbcTemplateTwo(@Qualifier("dataSourceTwo") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Primary
@Bean("platformTransactionManager")
public PlatformTransactionManager platformTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("platformTransactionManagerTwo")
public PlatformTransactionManager platformTransactionManagerTwo(@Qualifier("dataSourceTwo") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
3.application.properties
spring.profiles.active=test
## database settings
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.default_schema=dbo
## druid settings
#配置初始化大小、最小、最大 one
spring.datasource.druid.one.initial-size=25
spring.datasource.druid.one.min-idle=20
spring.datasource.druid.one.max-active=100
#配置初始化大小、最小、最大 two
spring.datasource.druid.two.initial-size=2
spring.datasource.druid.two.min-idle=2
spring.datasource.druid.two.max-active=5
#配置获取连接等待超时的时间
spring.datasource.druid.max-wait=65000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=25000
#配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=650000
spring.datasource.druid.max-evictable-idle-time-millis=950000
#连接校验配置
spring.datasource.druid.validation-query=select 1
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
#配置连接池中min-idle以内的连接,空闲时间超过min-evictable-idle-time-millis,则会执行keep-alive
spring.datasource.druid.keep-alive=true
#oracle:true,mysql:false,PSCache对支持游标的数据库有用
spring.datasource.druid.pool-prepared-statements=false
#oracle:20,mysql:-1
spring.datasource.druid.max-open-prepared-statements=-1
#asyncInit是1.1.4中新增加的配置,如果有initialSize数量较多时,打开会加快应用启动时间
spring.datasource.druid.async-init=true
#配置监控统计拦截的filters
#spring.datasource.druid.filters=stat
#spring.datasource.druid.stat-view-servlet.login-username=root
#spring.datasource.druid.stat-view-servlet.login-password=123
#spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
#spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
3.1 application-test.properties
## database settings
#datasource one primary
#spring.datasource.druid.url=jdbc:mysql://localhost:3306/router?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.druid.one.url=jdbc:sqlserver://127.0.0.1:2893;DatabaseName=qqTest
spring.datasource.druid.one.username=sa
spring.datasource.druid.one.password=qq
#datasource two
spring.datasource.druid.two.url=jdbc:sqlserver://127.0.0.1:2893;DatabaseName=weiTest
spring.datasource.druid.two.username=sa
spring.datasource.druid.two.password=qq
4.jdbc使用
使用主(默认)数据源的Jdbc,(像没有添加多数据源之前一样即可)
@org.springframework.stereotype.Repository
public class OrdersJdbc{
@javax.annotation.Resource
private org.springframework.jdbc.core.JdbcTemplate jdbcTemplate;
}
使用从数据源的Jdbc,需要指定bean的名称
@org.springframework.stereotype.Repository
public class SequenceJdbc {
@javax.annotation.Resource(name = "jdbcTemplateTwo")
private JdbcTemplate jdbcTemplate;
}
5.事务使用@Transactional
使用默认数据源的事务即@Primary标记的
@org.springframework.stereotype.Service
@org.springframework.transaction.annotation.Transactional
public class OrdersService {
}
使用指定的事务必须指定名称,如配置的bean,见配置类 platformTransactionManagerTwo
@org.springframework.stereotype.Service
@org.springframework.transaction.annotation.Transactional(transactionManager="platformTransactionManagerTwo",rollbackFor = Exception.class)
public class TSystemSequenceService {
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)