springboot多数据库源匹配关键点

基本思路:

1、通过@Primary注解解除datasoure的单例模式,配置多个datasource源指向多个数据库地址

2、按照不同的数据库建立不同的mapper地址:

 关键代码:

application.yml的数据源配置:

server:
  port: 8091
spring:
  datasource:
    cost:
      name: cost
      url: jdbc:mysql://10.16.1.54:3306/njwd_cost8?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true
      username: root
      password: njwddining1234
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      connectionProperties: druid.stat.mergeSql=true
      maxIdle: 50
      maxWait: 60000
      minIdle: 6
      initialSize: 10
      maxActive: 50
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 'x' from dual
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 20
    stock:
      name: stock
      url: jdbc:mysql://10.16.1.54:3306/njwd_cost_stock8?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true
      username: root
      password: njwddining1234
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      connectionProperties: druid.stat.mergeSql=true
      maxIdle: 50
      maxWait: 60000
      minIdle: 6
      initialSize: 10
      maxActive: 50
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 'x' from dual
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 20
## 日志设置
logging:
  path: logs
  level:
    root: info

cost的DataSource配置:

package com.njwd.photoupdate.config;

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.autoconfigure.jdbc.DataSourceBuilder;
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;

@Configuration
@MapperScan(basePackages = "com.njwd.photoupdate.mapperCost", sqlSessionFactoryRef = "costSqlSessionFactory")
public class CostSourceConfig {

    @Primary
    @Bean(name = "costDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.cost")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "costSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("costDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper-cost/*.xml"));
        return factoryBean.getObject();
    }

    @Primary
    @Bean(name = "costTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("costDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "costSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("costSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 stock的DataSource配置:

package com.njwd.photoupdate.config;

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.autoconfigure.jdbc.DataSourceBuilder;
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;
 
@Configuration
@MapperScan(basePackages = "com.njwd.photoupdate.mapperStock", sqlSessionFactoryRef = "stockSqlSessionFactory")
public class StockSourceConfig {
 
    @Bean(name = "stockDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.stock")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "stockTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("stockDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
 
    }
 
    @Bean(name = "stockSqlSessionFactory")
    public SqlSessionFactory basicSqlSessionFactory(@Qualifier("stockDataSource") DataSource basicDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(basicDataSource);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper-stock/*.xml"));
        return factoryBean.getObject();
    }
 
    @Bean(name = "stockSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("stockSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值