springboot 은 다 중 데이터 원본 을 설정 하고 Druid 와 my batis 를 통합 합 니 다.

my sql,Oacle 등 다양한 데이터 원본 일 수 있 습 니 다.
프로젝트 구조
在这里插入图片描述
메모:@Primary 데이터 원본 이 제어 하 는 mapper 파일 에 주석@Mapper 만 있 습 니 다.그렇지 않 으 면 my batis 에서 스 캔 을 전환 할 수 없습니다.즉,본문 에 있 는 mapper/opener 폴 더 에 mapper 주 해 를 추가 합 니 다.
1、pom
드라이브 외 에 druid 와 my batis 등 pom 을 추가 하여 my batis 를 통합 하여 자체 검색 합 니 다.

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.23</version>
 </dependency>
2.properties 설정

##################    JDBC       ################
#        
spring.datasource.druid.opener.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.druid.opener.url=jdbc:oracle:thin:@127.0.0.1:1521:wljrdb
spring.datasource.druid.opener.username=opener
spring.datasource.druid.opener.password=123456

#        
spring.datasource.druid.yq.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.druid.yq.url=jdbc:oracle:thin:@127.0.0.1:1521:wljrdb
spring.datasource.druid.yq.username=YQ
spring.datasource.druid.yq.password=123456

#f10      
spring.datasource.druid.f10.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.f10.url=jdbc:mysql://127.0.0.1:3306/iwin-f10?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8
spring.datasource.druid.f10.username=root
spring.datasource.druid.f10.password=123456

##########################  druid     ##########################
#            ,           #      ,  ,  
##################             ################
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=30000

spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.validation-query-timeout=30000
#          filters,       sql    ,'wall'     
spring.datasource.druid.filters=stat,wall,log4j2
#   connectProperties     mergeSql  ; SQL  
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

#           StatViewServlet,         ,
#          http://127.0.0.1:8083/druid/login.html,     ,    http://127.0.0.1:8083/druid/  
spring.datasource.druid.stat-view-servlet.enabled= true
spring.datasource.druid.stat-view-servlet.url-pattern= /druid/*
spring.datasource.druid.stat-view-servlet.reset-enable= true
spring.datasource.druid.stat-view-servlet.login-username= admin
spring.datasource.druid.stat-view-servlet.login-password= admin123
spring.datasource.druid.stat-view-servlet.allow= 127.0.0.1
3.프로필

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @Author: admin
 */
@Configuration
public class MultiDataSourceConfig {

    private final static Logger logger = LoggerFactory.getLogger(MultiDataSourceConfig.class);

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.opener")
    public DataSource openerDataSource(){
        logger.info("opener      。。。");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid.yq")
    public DataSource yqDataSource(){
        logger.info("YQ      。。。");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid.f10")
    public DataSource f10DataSource(){
        logger.info("f10      。。。");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory openerSqlSessionFactory(@Qualifier("openerDataSource")DataSource openerDataSource) throws Exception{
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(openerDataSource);
        fb.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/opener/*.xml")
        );
        return fb.getObject();
    }

    @Bean
    public SqlSessionFactory yqSqlSessionFactory(@Qualifier("yqDataSource")DataSource yqDataSource) throws Exception{
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(yqDataSource);
        fb.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/yq/*.xml")
        );
        return fb.getObject();
    }

    @Bean
    public SqlSessionFactory f10SqlSessionFactory(@Qualifier("f10DataSource")DataSource f10DataSource) throws Exception{
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(f10DataSource);
        fb.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/f10/*.xml")
        );
        return fb.getObject();
    }

    @Bean(name="openerMapperScannerConfigurer")
    @Primary
    public MapperScannerConfigurer openerMapperScannerConfigurer(){
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setSqlSessionFactoryBeanName("openerSqlSessionFactory");
        configurer.setBasePackage("com.cfzq.lz.mapper.opener");
        return configurer;
    }

    @Bean(name="yqMapperScannerConfigurer")
    public MapperScannerConfigurer yqMapperScannerConfigurer(){
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setSqlSessionFactoryBeanName("yqSqlSessionFactory");
        configurer.setBasePackage("com.cfzq.lz.mapper.yq");
        return configurer;
    }

    @Bean(name="f10MapperScannerConfigurer")
    public MapperScannerConfigurer f10MapperScannerConfigurer(){
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setSqlSessionFactoryBeanName("f10SqlSessionFactory");
        configurer.setBasePackage("com.cfzq.lz.mapper.f10");
        return configurer;
    }

    @Bean(name="openerTransactionManager")
    @Primary
    public DataSourceTransactionManager openerTransactionManager(@Qualifier("openerDataSource")DataSource openerDataSource) throws Exception{
        return new DataSourceTransactionManager(openerDataSource);
    }

    @Bean(name="yqTransactionManager")
    public DataSourceTransactionManager yqTransactionManager(@Qualifier("yqDataSource")DataSource yqDataSource) throws Exception{
        return new DataSourceTransactionManager(yqDataSource);
    }

    @Bean(name="f10TransactionManager")
    public DataSourceTransactionManager f10TransactionManager(@Qualifier("f10DataSource")DataSource f10DataSource) throws Exception{
        return new DataSourceTransactionManager(f10DataSource);
    }

}

코드 호출
간단 합 니 다.어떤 데이터 소스 서 비 스 를 사용 하여 어떤 mapper 를 주입 하면 됩 니 다.
在这里插入图片描述
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기