springboot不同类型多数据源配置及使⽤springboot多数据源配置:
datasource.master.jdbc=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true          datasource.master.username=root
datasource.master.password=123456
datasource.master.sql.jdbc.Driver
datasource.master.url=jdbc:oracle:thin:@localhost:1521:test
datasource.master.username=acct
datasource.master.password=123456
datasource.master.driver-class-name=oracle.jdbc.driver.OracleDriver
datasource.master.max-idle=10
datasource.master.max-wait=10000
datasource.master.min-idle=5
datasource.master.initial-size=5
datasource.master.validation-query=SELECT 1
st-on-borrow=false
st-while-idle=true
datasource.master.time-between-eviction-runs-millis=18800
第⼀个数据源实现:
import javax.sql.DataSource;
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 org.springframework.boot.jdbc.DataSourceBuilder;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import t.annotation.PropertySource;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* 主数据源配置
* @author zyl
*
*/
@Configuration
//读取⾃定义配置⽂件 datasource.properties
@PropertySource("classpath:config/datasource.properties")
//扫描 Mapper 接⼝并容器管理
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MasterDataSourceConfig {
// 精确到 master ⽬录,以便跟其他数据源隔离
static final String PACKAGE = "com.acct.dao.master";
static final String TYPE_ALIASES_PACKAGE = "com.ity.master";
static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
@Bean(name = "masterDataSource")
@Primary
@ConfigurationProperties(prefix = "datasource.master")
public DataSource masterDataSource() {
ate().build();
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
Object();
}
}
@Primary 注解默认为第⼀个数据源
第⼆个数据源实现:
import javax.sql.DataSource;
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 org.springframework.boot.jdbc.DataSourceBuilder;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.PropertySource;
import io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* 从数据源配置
* @author zyl
*
*/
@Configuration
springboot结构
//读取⾃定义配置⽂件 datasource.properties
@PropertySource("classpath:config/datasource.properties")
// 扫描 Mapper 接⼝并容器管理
@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory") public class ClusterDataSourceConfig {
// 精确到 cluster ⽬录,以便跟其他数据源隔离
static final String PACKAGE = "com.acct.dao.cluster";
static final String TYPE_ALIASES_PACKAGE = "com.ity.cluster";
static final String MAPPER_LOCATION = "classpath:mapper/cluster/*.xml";
@Bean(name = "clusterDataSource")
@ConfigurationProperties(prefix = "datasource.cluster")
public DataSource clusterDataSource() {
ate().build();
}
@Bean(name = "clusterTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(clusterDataSource());
}
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
sessionFactory.setTypeAliasesPackage(ClusterDataSourceConfig.TYPE_ALIASES_PACKAGE);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION)); Object();
}
}
⽬录结构:
启动正常,ok!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。