springboot 配置多数据源

第一步:编写application.yml配置文件

spring:
  datasource:
    system:
      jdbc-url: jdbc:oracle:thin:@localhost:1521/orcl
      driver-class-name: oracle.jdbc.OracleDriver
      username: system
      password: 1234
      initial-size: 5
      min-idle: 5
      max-active: 20
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
    kllogt:
      jdbc-url: jdbc:oracle:thin:@localhost:1521/orcl
      driver-class-name: oracle.jdbc.OracleDriver
      username: kllogt
      password: kllogt
      initial-size: 5
      min-idle: 5
      max-active: 20
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
logging:
  level:
    org.springframework.web: debug

第二部编写两个数据源的配置类

配置类SystemResource

package www.it.com.springbootresource.util;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "www.it.com.springbootresource.system.dao", sqlSessionFactoryRef = "SystemSqlSessionFactory")
public class SystemResource {
    // 将这个对象放入Spring容器中
    @Bean(name = "SystemDataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.system")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "SystemSqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为SystemDataSource的对象
    public SqlSessionFactory SystemSqlSessionFactory(@Qualifier("SystemDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        VFS.addImplClass(SpringBootVFS.class);//mybatis的facroty需要加载SpringBoot独特的虚拟文件系统,才能识别类路
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
        //bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                //new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/m1/*.xml"));
        return bean.getObject();
    }
    @Bean("SystemSqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate Systemsqlsessiontemplate(
            @Qualifier("SystemSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

KllogtResource配置类

package www.it.com.springbootresource.util;

import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
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 javax.sql.DataSource;

/**
 * @author wangjie
 * @date 2019/11/3 16:36
 * @description
 * @company 石文软件有限公司
 */
//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "www.it.com.springbootresource.kllogt.dao", sqlSessionFactoryRef = "KllogtSqlSessionFactory")
public class KllogtResource {
    @Bean(name = "KllogtDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.kllogt")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "KllogtSqlSessionFactory")
    public SqlSessionFactory KllogtSqlSessionFactory(@Qualifier("KllogtDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        VFS.addImplClass(SpringBootVFS.class);//mybatis的facroty需要加载SpringBoot独特的虚拟文件系统,才能识别类路
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
       // bean.setMapperLocations(
        //        new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/m2/*.xml"));
        return bean.getObject();
    }
    @Bean("KllogtSqlSessionTemplate")
    public SqlSessionTemplate Kllogtsqlsessiontemplate(
            @Qualifier("KllogtSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

}

项目结构

自己的pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>www.it.cpm</groupId>
    <artifactId>springboot-resource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-resource</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

原文地址:https://www.cnblogs.com/wang66a/p/12069287.html

时间: 2024-07-30 21:30:05

springboot 配置多数据源的相关文章

springboot配置多数据源

在做项目的过程中难免会遇到这种情况:一个项目需要两个数据库中的数据,希望这篇文章能给遇到这些问题的小伙伴一点帮助 第一步:将两个数据源的mapper接口和xml文件分别放入不同的文件夹下: 第二步:在application.yml文件中加入双数据源,一定要指定主数据源,不然会报错 spring:   datasource:     primary:       driver-class-name: com.mysql.jdbc.Driver       url: url地址       user

SpringBoot配置Druid数据源

在我刚开始接触JDBC的时候,用的是DriveManager驱动来连接数据库的.而现在大多是用DataSource. 这里先简单说一下区别: 1.datasource是与连接池获取连接,而DriverManager是获取与数据库的连接!DriverManager类的主要作用是管理注册到DriverManager中的JDBC驱动程序,并根据需要使用JDBC驱动程序建立与数据服务器的网络连接.但是建立与数据库的连接是一项较耗资源的工作,频繁的进行数据库连接建立操作会产生较大的系统开销,为了解决上述问

springboot配置多数据源(JdbcTemplate方式)

在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host.需要使用多种数据库(MySql.Oracle.SqlServer...) 如果使用springboot开发,可做如下配置: Config: import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; imp

SpringBoot配置 druid 数据源配置 慢SQL记录

spring: datasource: url: jdbc:mysql://127.0.0.12:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull username: root password: root druid: initialSize: 5 application: name: message-center secur

springboot配置多数据源mongodb

参考大佬的文章 https://juejin.im/entry/5ab304dd51882555825241b3 原文地址:https://www.cnblogs.com/zhoujl-5071/p/10427174.html

SpringBoot Mybatis双数据源的配置

近期因为公司项目的需要,用上了maven和Springboot,对于java开发这块,早闻maven是个好东西,但一直没有去用,感觉用maven帮我们自己做了太多的事情,一个项目跑起来都不知道背后做了些什么,现在想想,可能那个时候脑子进水了吧. Springboot作为Spring的简约版(我是这么叫的,没有任何依据),将原来Spring需要做的配置文件,改为了注解,提供了大量的***start组件来帮我们做了很多初始化的处理. 但是今天我想要分享的不是maven如何搭建一个helloword的

SpringBoot+Druid+Mybatis配置多数据源

我们在开发一个项目的时候,可能会遇到需要对多个数据库进行读写的需求,这时候就得在项目中配置多个数据源了.在Java项目的开发中,目前最常用的数据操作框架是 Mybatis,开发框架也都基本用上了SpringBoot.而Druid号称最好的数据库连接池,自然也是被广泛使用. 所以本文将演示一下,SpringBoot+Druid+Mybatis如何去配置多数据源.首先在IDEA中创建一个SpringBoot工程: 选择一些基本的包: 完成创建: pom.xml配置的依赖如下: <dependenci

springboot之多数据源配置JdbcTemplate

springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.

springboot+mybatis+Druid配置多数据源(mysql+postgre)

springboot+mybatis+Druid配置多数据源(mysql+postgre)引入pom依赖设置application多数据源config配置db1config配置(主数据库配置)db2config配置(其他数据库)事务处理mapper层 springboot+mybatis+Druid配置多数据源(mysql+postgre) 参考资料: 第八章 springboot + mybatis + 多数据源 springboot + mybatis + druid + 多数据源 spri