关于SpringBoot集成两个数据库的教程

OracleDataSourceConfigOracleDataSourceConfig

前几天遇到遇到一个需求,需要从mysql库中查询的数据到pg库中,我选择在一个服务中集成两个不同的数据库,废话不多说,直接上教程,教程以Orcle和Postgressql为例

1.首先需要在pom文件中引入这两个库jar包
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.19</version>
        </dependency>
2.编写两个配置类分别为OracleDataSourceConfig和PostgresqlDataSourceConfig
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author :hanwenjian
 * @date :Created in 2023/7/24 16:44
 * 文件说明: </p>
 */
@Configuration
@MapperScan(basePackages = "com.inspur.softwaregroup.communication.nrms.dao.postgresql",sqlSessionTemplateRef ="postgreSqlSessionTemplate")
public class PostgresqlDataSourceConfig {


    private String url = "jdbc:postgresql://127.0.0.1:5432/postgres";
    private String username = "root";
    private String password = "123456";
    private String driverClassName = "org.postgresql.Driver";
    private String mybatisLocations = "classpath*:mapper/postgresql/*Mapper.xml";

    //    创建数据源
    @Bean(name = "postgresqlDS")
    @ConfigurationProperties(prefix = "spring.datasource.postgresql")
    @Primary
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password)
                .build();
        return build;
    }


    // 创建SessionFactory
    @Bean(name = "postgreSqlSessionFactory")
    @Primary
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("postgresqlDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean  bean = new SqlSessionFactoryBean();
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(mybatisLocations));
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    // 创建事务管理器

    @Bean("postgreTransactionManger")
    @Primary
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("postgresqlDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "postgreSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("postgreSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    private Class getType(String type) {
        try {
            return Class.forName(type);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
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.Autowired;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author :hanwenjian
 * @date :Created in 2023/7/24 16:44
 * 文件说明: </p>
 */
@Configuration
@MapperScan(basePackages = "com.inspur.softwaregroup.communication.nrms.dao.oracle",
        sqlSessionTemplateRef ="oracleSqlSessionTemplate")
public class OracleDataSourceConfig {


    private String url = "jdbc:oracle:thin:@127.0.0.1:1521/pdblightbox2";
    private String username = "root";
    private String password = "123456";
    private String driverClassName = "oracle.jdbc.driver.OracleDriver";
    private String mybatisLocations = "classpath*:mapper/orcle/*Mapper.xml";

    //    创建数据源
    @Bean(name = "oracleDS")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")
    @Primary
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password)
                .build();
        return build;
    }


    // 创建SessionFactory
    @Bean(name = "oracleSqlSessionFactory")
    @Primary
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("oracleDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean  bean = new SqlSessionFactoryBean();
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(mybatisLocations));
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    // 创建事务管理器

    @Bean("oracleTransactionManger")
    @Primary
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("oracleDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "oracleSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    private Class getType(String type) {
        try {
            return Class.forName(type);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
3.springboot启动类中的@SpringBootApplication这个注解中需要添加参数
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
4.然后万事具备 写代码 就可以!!!!!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 集成 Mybatis-plus 同样支持集成不同类型的数据库。需要在配置文件中配置数据源和 Mybatis-plus 相关的配置,具体示例代码如下: MySQL: ```properties spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/db_name?characterEncoding=utf-8&useSSL=false spring.datasource.username=username spring.datasource.password=password mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity ``` Oracle: ```properties spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=username spring.datasource.password=password mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity ``` SQLServer: ```properties spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=db_name spring.datasource.username=username spring.datasource.password=password mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity ``` PostgreSQL: ```properties spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/db_name spring.datasource.username=username spring.datasource.password=password mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity ``` 需要注意的是,除了配置不同类型的数据库连接信息外,还需要配置 Mybatis-plus 的 mapper-locations 和 type-aliases-package。其中 mapper-locations 是指定 Mapper 文件的存放位置,type-aliases-package 是指定实体类的包路径,这两个配置项需要根据项目实际情况进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值