当前位置:   article > 正文

springBoot-Mybatis多数据源集成(同时配置mysq和oracle)_springboot集成mysql和oracle

springboot集成mysql和oracle

第一次配置多数据源,理所当然的去网上查了。然后就看见一个感觉挺靠谱的,就这个。
https://segmentfault.com/a/1190000012148813
结果不能用,因为他这个只是实现了,在项目中肯定要封装的,自己封装工作量太大了。果断弃用
不过那里面的思路是正确的,jar包也是正确的

1.jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.adagio</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>multiple-data-sources</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
2.数据源配置
#数据源配置
spring.datasource.primary.jdbc-url=jdbc:oracle:thin:@###,###,###,###:####:accttest
spring.datasource.primary.username=****
spring.datasource.primary.password=*****
spring.datasource.primary.driver-class-name=oracle.jdbc.OracleDriver

spring.datasource.secondary.jdbc-url=jdbc:mysql:// ###,###,###,###:####/acct?useUnicode=true&characterEncoding=utf8
spring.datasource.secondary.username=*****
spring.datasource.secondary.password=*****
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
3.读取配置文件,集成mybatis
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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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
//mapper接口文件位置
@MapperScan(basePackages = "com.asiainfo.comm.commIntface.*.mapper", sqlSessionTemplateRef = "secondaryJdbcTemplate")
public class MysqlDataSourceConfig {

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

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

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

    @Bean(name = "secondaryJdbcTemplate")
    public SqlSessionTemplate secondaryJdbcTemplate(@Qualifier(value = "secondaryJdbcFactory")
                                                                SqlSessionFactory sqlSessionFactory) throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

上面这个是mysql的,oracle同上
多的话可以一直配置

这里可能会有个问题,如果你的SpringBoot和我版本一样的话,应该没事,如果是2.0.0以下的话
import org.springframework.boot.jdbc.DataSourceBuilder;
这个类可能会找不到,2.0以下路径应该是
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;

在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/981725
推荐阅读
相关标签
  

闽ICP备14008679号