SpringBoot+MybatisPlus 多数据源配置

灰常简单的配置,一看就会,一用就对

前期准备

传送门:SpringBoot接入MybatisPlus

配置相关

pom.xml中添加依赖

		<!--动态数据源-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!--mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--oracle驱动包-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>

application.yml 中修改数据源配置

spring:
  datasource:
    dynamic:
      primary: db1 #设置默认的数据源或者数据源组,默认值即为master
      strict: true #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
      datasource:
        db1:
          url: jdbc:mysql://***.***.***.***:3306/dbname?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: ***
          password: ***
          driverClassName: com.mysql.jdbc.Driver
        db2:
          driverClassName: oracle.jdbc.OracleDriver
          url: jdbc:oracle:thin:@***.***.***.***:1521:dbname
          username: ***
          password: ***
          hikari:
            connectionTimeout: 600000

完结!!使用方法

在需要切换数据源的类或方法上添加@DS()注解;注解中写的内容为在application.yml配置文件中配置的数据库名字,本例中为db1和db2

例子:

package ***.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import ***.bean.entity.EopQuestionImage;
import java.util.List;
/**
 * <p>
 *  Mapper 接口
 * </p>
 * @author Liu xinxing
 * @since 2020-06-10
 */
@DS("db2")
//本例中配置的默认数据源为"db1" 所以,如果不加@DS注解则默认使用db1的数据源
public interface QuestionImageMapper extends BaseMapper<QuestionImage> {
    List<QuestionImage> selectAll();
}

本例中配置的默认数据源为"db1" 所以,如果不加@DS注解则默认使用db1的数据源

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring BootMyBatis Plus多数据源配置MySQL和PostgreSQL,你可以按照以下步骤进行操作: 1. 首先,在你的Spring Boot项目中添加MySQL和PostgreSQL的依赖。在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> ``` 2. 接下来,配置数据源。在`application.properties`或`application.yml`文件中添加以下配置: ```yaml # MySQL 数据源配置 spring.datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_db spring.datasource.mysql.username=root spring.datasource.mysql.password=123456 # PostgreSQL 数据源配置 spring.datasource.postgresql.url=jdbc:postgresql://localhost:5432/postgresql_db spring.datasource.postgresql.username=postgres spring.datasource.postgresql.password=123456 ``` 3. 然后,创建数据源配置类。创建两个数据源的配置类,分别用于MySQL和PostgreSQL。例如,创建名为`MySQLDataSourceConfig`和`PostgreSQLDataSourceConfig`的类,并分别添加`@Configuration`和`@ConfigurationProperties`注解。 ```java @Configuration @ConfigurationProperties(prefix = "spring.datasource.mysql") public class MySQLDataSourceConfig { private String url; private String username; private String password; // 省略 getter 和 setter 方法 } ``` ```java @Configuration @ConfigurationProperties(prefix = "spring.datasource.postgresql") public class PostgreSQLDataSourceConfig { private String url; private String username; private String password; // 省略 getter 和 setter 方法 } ``` 4. 接下来,配置数据源。在`DataSourceConfig`类中,创建两个数据源的`DataSource`实例,并将它们注入到`SqlSessionFactory`中。 ```java @Configuration public class DataSourceConfig { @Bean(name = "mysqlDataSource") @ConfigurationProperties(prefix = "spring.datasource.mysql") public DataSource mysqlDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "postgresqlDataSource") @ConfigurationProperties(prefix = "spring.datasource.postgresql") public DataSource postgresqlDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDataSource, @Qualifier("postgresqlDataSource") DataSource postgresqlDataSource) throws Exception { SqlSessionFactoryBean sessionFactory

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值