baomidou/dynamic-datasource

repository·master·Indexed 26 days ago

https://github.com/baomidou/dynamic-datasource

A Spring Boot starter for rapid integration of multiple data sources. It supports data source grouping, read-write splitting, dynamic addition/removal of data sources, and distributed transactions via Seata. Provides specific starter modules for Spring Boot 1.5.x ~ 2.x.x (JDK 8+), Spring Boot 3.x.x (JDK 17+), and Spring Boot 4.x.x (JDK 17+). Features include the @DS annotation for switching data sources and support for GraalVM native image compilation in Spring Boot 3 and 4 starters.

Tokens
3.9K
Snippets
15
Records
24
Agent score
89%

What's inside dynamic-datasource

  1. Understand the Project Module Structure

    master

    The project is organized into 6 distinct modules. Depending on your Spring Boot version and requirements, you may need to target specific modules:

    1. dynamic-datasource-creator: Core datasource creator functionality
    2. dynamic-datasource-spring: Spring framework integration
    3. dynamic-datasource-spring-boot-common: Common Spring Boot autoconfiguration
    4. dynamic-datasource-spring-boot-starter: Spring Boot 2.x starter
    5. dynamic-datasource-spring-boot3-starter: Spring Boot 3.x starter (Java 17+)
    6. dynamic-datasource-spring-boot4-starter: Spring Boot 4.x starter (Java 17+)
  2. Quick Start with Gradle

    master

    Use the Gradle wrapper (./gradlew) to perform common build operations. This replaces the previous Maven (mvnw) workflow.

    # Build the project
    ./gradlew build
    
    # Clean build artifacts
    ./gradlew clean
    
    # Build without running tests
    ./gradlew build -x test
    
    # Publish to local Maven repository
    ./gradlew publishToMavenLocal
  3. Manage multiple JDK environments in Gradle

    master

    If you need to switch between different JDK versions for builds, you can specify the JDK path using the JAVA_HOME environment variable or via a Gradle system property.

    # Using JAVA_HOME to specify JDK
    export JAVA_HOME=/path/to/jdk-17
    ./gradlew build
    
    # Or using Gradle's JVM parameter
    ./gradlew build -Dorg.gradle.java.home=/path/to/jdk-17
  4. Select the correct starter module based on Spring Boot version

    master

    Choose the appropriate dependency based on your Spring Boot and JDK versions:

    • Spring Boot 1.5.x ~ 2.x.x (JDK 8+): Use dynamic-datasource-spring-boot-starter (Version 1.0.0+).
    • Spring Boot 3.x.x (JDK 17+): Use dynamic-datasource-spring-boot3-starter (Version 4.0.0+).
    • Spring Boot 4.x.x (JDK 17+): Use dynamic-datasource-spring-boot4-starter (Version 4.5.0+).
  5. Configure JDK compatibility for Spring Boot starters

    master

    Ensure each module uses the correct JDK version required by its Spring Boot target. Use both java {} configuration and options.release to ensure bytecode and API level consistency.

    Module TypeSpring BootJDK VersionConfiguration
    2.x Starter2.xJDK 8sourceCompatibility = 1.8
    3.x Starter3.xJDK 17sourceCompatibility = 17 + options.release = 17
    4.x Starter4.xJDK 17sourceCompatibility = 17 + options.release = 17
    // Spring Boot 2.x Starter - JDK 8
    java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    
    // Spring Boot 3.x/4.x Starter - JDK 17
    java {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    
    tasks.withType(JavaCompile).configureEach {
        options.release = 17
    }
  6. Manage Spring Boot dependencies per submodule

    master

    To avoid version conflicts (e.g., javax.servlet vs jakarta.servlet), do not apply io.spring.dependency-management globally in the root project. Instead, apply the plugin and import the corresponding Spring Boot BOM within each specific submodule.

    Example for Spring Boot 2.x, 3.x, and 4.x starters:

    // dynamic-datasource-spring-boot-starter (Spring Boot 2.x)
    apply plugin: 'io.spring.dependency-management'
    
    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-dependencies:${springBoot2Version}"
        }
    }
    
    // dynamic-datasource-spring-boot3-starter (Spring Boot 3.x)
    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-dependencies:${springBoot3Version}"
        }
    }
    
    // dynamic-datasource-spring-boot4-starter (Spring Boot 4.x)
    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-dependencies:${springBoot4Version}"
        }
    }
  7. Set up IDE Support for Gradle

    master

    The project can be opened in the following IDEs:

    • IntelliJ IDEA: Automatically detects the Gradle build upon opening the project.
    • Eclipse: Requires the Buildship plugin. Install via Help → Eclipse Marketplace, then use File → Import → Gradle → Existing Gradle Project.
    • VS Code: Install the "Gradle for Java" extension from the marketplace.