SpringBoot添加多数据源mysql和oracle

项目结构

          多数据源配置文件

   MultiDataSourceConfig.java

   SqlSessionTemplate1.java

   SqlSessionTemplate2.java

package com.holytax.accenture_fp_in.datasource;

import javax.sql.DataSource;

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;

/**
 * 配置多数据源
 * 
 * 
 *
 */
@Configuration
public class MultiDataSourceConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary // 定义主数据源
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "custom.datasource.ds1")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

 

package com.holytax.accenture_fp_in.datasource;

import javax.sql.DataSource;

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.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 org.springframework.transaction.PlatformTransactionManager;

/**
 * spring 整合 mybatis 配置SqlSessionTemplate
 * 
 *
 */
@Configuration
@MapperScan(basePackages = "com.holytax.accenture_fp_in.mapper", sqlSessionTemplateRef  = "primarySqlSessionTemplate")
public class SqlSessionTemplate1 {
    
    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    //配置声明式事务管理器
    @Bean(name = "primaryTransactionManager")
    @Primary
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(
            @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
}
package com.holytax.accenture_fp_in.datasource;

import javax.sql.DataSource;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@MapperScan(basePackages = "com.holytax.accenture_fp_in.oracleMapper", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SqlSessionTemplate2 {

    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:oracleMapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "secondaryTransactionManager")
    public PlatformTransactionManager secondaryTransactionManager(
            @Qualifier("secondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "secondarySqlSessionTemplate")
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

         这里是对多数据源,扫描做了处理。mapper 对应Mysql的部分处理 SqlSessionTemplate1.java。oracleMapper 对应oracle的部分处理 SqlSessionTemplate2.java.

      就是业务层的Mapper文件,不同数据源扫描不同的包。

   2、 数据库application.properties配置文件

#Server
server.port=8081
#MYBATIS
mybatis.configLocation:classpath:mybatis-config.xml
#mybatis.mapper-locations=classpath:mapper/*.xml    //注意在单数据时,这配置文件,和springboot启动文件上配置了,扫描路劲,我们要把他干掉。因为我们上边的,
#测试                                                // SqlSessionTemplate1.java,SqlSessionTemplate2.java.文件已经对这些内容作了处理。
spring.datasource.url=jdbc:mysql://localhost:3306/accenture?useSSL=true
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 更多数据源
custom.datasource.ds1.driver-class-name=oracle.jdbc.driver.OracleDriver
custom.datasource.ds1.url=jdbc:oracle:thin:@localhost:1521:testdb
custom.datasource.ds1.username=user
custom.datasource.ds1.password=pass

spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#url params
spring.holytax.interface-url=http://localohst:8080/hltypt/api/callservice.do
spring.holytax.interface-company=accenture

  3、services 业务处理层

                                 只需要在,方法加上事务@Transactional,因为我们,配置的不同的扫描包,,就相当动态在方法上指定数据源,只不过,,我们这里作了固定的区分。

4、pom.xml 文件  添加对于数据库的驱动支持,

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <!-- 添加oracle jdbc driver -->                //如果maven仓库没有,下载导入本地maven仓库
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.5.0</version>
        </dependency>

 

   

 

posted @ 2017-12-26 22:23  Vip灬cnblog  阅读(19293)  评论(0编辑  收藏  举报