• MyBatis整合多数据源


    基础环境

    SpringBoot : 3.0.0
    Java: jdk-17.0.5
    Maven: 3.6.1
    
    • 1
    • 2
    • 3

    引入相关jar

    
    <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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>3.0.0-SNAPSHOTversion>
            <relativePath/> 
        parent>
        <groupId>com.gisergroupId>
        <artifactId>gis-java-mpartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>gis-java-mpname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>17java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.3version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-jdbcartifactId>
                    exclusion>
                exclusions>
            dependency>
    
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>dynamic-datasource-spring-boot-starterartifactId>
                <version>3.6.1version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-aopartifactId>
                    exclusion>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-jdbcartifactId>
                    exclusion>
                exclusions>
            dependency>
            
    		
    		<dependency>
    		    <groupId>com.baomidougroupId>
    		    <artifactId>mybatis-plus-generatorartifactId>
    		    <version>3.5.3version>
    		dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
                <version>2.1.3.RELEASEversion>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.16version>
            dependency>
    		
    		<dependency>
    		    <groupId>org.springframework.bootgroupId>
    		    <artifactId>spring-boot-starter-freemarkerartifactId>
    		    <version>3.1.5version>
    		dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-autoconfigureartifactId>
                <version>3.0.0version>
            dependency>
        dependencies>
    
        <build>
            <finalName>gis-mpfinalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.5.4version>
                    <configuration>
                        <mainClass>com.giser.mp.GisJavaMpApplicationTestsmainClass>
                    configuration>
                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
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    数据源等配置

    server:
      port: 9999
      servlet:
        context-path: /
        encoding:
          charset: UTF-8
      
    spring:
      datasource:
        params: useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8
        dynamic:
          primary: master
          strict: false
          datasource:
            master:
              url: jdbc:mysql://ip:port/dbname?${spring.datasource.params}
              username: dbusername
              password: dbpassword
            slave:
              url: jdbc:mysql://ip:port/dbname?${spring.datasource.params}
              username: dbusername
              password: dbpassword
          hikari:
            max-lifetime: 18000
            connection-timeout: 10000
            idle-timeout: 3600000
            min-idle: 4
            max-pool-size: 12
    
    • 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
  • 相关阅读:
    Postgresql源码(75)notify与listen执行流程分析
    马斯克打了个响指,推特50%员工被裁....
    C++ 类和对象(4)构造函数
    android framework之Applicataion启动流程分析(二)
    RabbitMQ-死信交换机和死信队列
    二叉树相关OJ题
    基于安卓android微信小程序的师生答疑交流平app
    计算机毕业设计JAVA便利店系统mybatis+源码+调试部署+系统+数据库+lw
    什么是JWT?深入理解JWT从原理到应用
    通过.sh文件快捷部署jar包到服务器上
  • 原文地址:https://blog.csdn.net/SUNBOYmxbsH/article/details/134539240