mybatis-plus多数据源(例子是mysql,sqlserver)

14 篇文章 0 订阅
4 篇文章 0 订阅
  • 注意:本文引用了所有的驱动依赖和mybatisplus依赖,如果已经配置了mybatisplus依赖和驱动依赖,那么核心依赖就一个:dynamic-datasource-spring-boot-starter,可直接跳过第一步中其他依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
    

1.依赖

  • 子工程

(sqlserver驱动没版本号,我也没搞清楚为什么)

        <!--Mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--SqlServer数据库驱动(不用版本号貌似是。。我用的时候未指定)-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>

        <!--mybatis-plus多数据源-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        </dependency>

  • 父工程
     <properties>
		<sqlserver.version>4.0</sqlserver.version>
        <mybatis-plus.version>3.4.1</mybatis-plus.version>
        <mysql-connector.version>8.0.15</mysql-connector.version>
    </properties>
    <dependencies>
			<!--Mysql数据库驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
            <!--mybatis-plus-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <!--mybatis-plus多数据源-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>

2.配置文件中,先配置数据源

spring:
  datasource:
    dynamic:
      primary: db1 #设置默认的数据源或者数据源组,默认值为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false在找不到数据源时使用默认数据源
      datasource:
        db1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://127.0.0.1:3306/db_xxxx?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
          username: xxxx
          password: xxxxxxxx
        db3:
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          url: jdbc:sqlserver://127.0.0.1:1433;databaseName=db_xxxx
          username: xxxx
          password: xxxxxxxx
#这里配置扫描xml
mybatis-plus:
  mapper-locations:
    - classpath:sqlmapper/*.xml
    - classpath:mysqlmapper/*.xml

3.配置类

这个类配置扫描mapper

package com.example.demo.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan({"com.example.demo.sqlmapper", "com.example.demo.mysqlmapper"})
public class MybatisConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

4. 代码中直接使用

    @GetMapping("/test/count")
    @ApiOperation("test")
    @DS("db3")		//有这个注解,会连接db3的数据库配置
    public Integer getCount() {
        return settingService.getCount();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值