From 258a432a78f50295d6229ab5f59f55787c1ddf27 Mon Sep 17 00:00:00 2001 From: Qiheng He Date: Mon, 15 Dec 2025 22:57:16 +0800 Subject: [PATCH] Use MySQL instead of H2Database in ShardingSphere integration --- .github/workflows/ci.yml | 8 +-- .mvn/wrapper/maven-wrapper.properties | 20 +----- README.md | 32 +++++++-- mvnw | 50 ++++++++++++-- mvnw.cmd | 56 ++++++++++++--- pom.xml | 9 +++ .../jdbc/v4/spring/MapperLayerTests.java | 17 +++-- .../pom.xml | 69 +++++++++++++------ .../v5/core/common/TcMySQLDatabaseType.java | 41 +++++++++++ .../jdbc/v5/core/dto/UserDto.java | 25 ------- .../jdbc/v5/core/mapper/UserMapper.java | 1 + ...here.infra.database.core.type.DatabaseType | 12 ++++ .../src/main/resources/application.yml | 10 +-- .../src/main/resources/config.yaml | 24 +++---- .../src/main/resources/db/schema.sql | 2 +- .../jdbc/v5/core/MapperLayerTests.java | 26 +++---- .../pom.xml | 54 ++++++++------- .../config/MyDataSourceConfiguration.java | 8 +-- .../jdbc/v5/spring/dto/UserDto.java | 25 ------- .../jdbc/v5/spring/mapper/TOrderMapper.java | 2 +- .../jdbc/v5/spring/mapper/UserMapper.java | 1 + .../service/impl/TOrderServiceImpl.java | 5 +- .../src/main/resources/application.yml | 10 +-- .../src/main/resources/db/schema.sql | 2 +- .../jdbc/v5/spring/MapperLayerTests.java | 19 +++-- 25 files changed, 323 insertions(+), 205 deletions(-) create mode 100644 third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/common/TcMySQLDatabaseType.java delete mode 100644 third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/dto/UserDto.java create mode 100644 third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType delete mode 100644 third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/dto/UserDto.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3808158..84d9265 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: cache: 'maven' - name: Build all test with Maven run: | - ./mvnw -T1C -B -e clean test + ./mvnw -T1C -e clean test test-minimum-hotspot-jdk-ci: name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -50,6 +50,6 @@ jobs: ./mvnw -am -pl orm-samples/mybatis-sample -T1C -B -e clean test - name: Build third-part test with Maven run: | - ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample -T1C -B -e clean test - ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample -T1C -B -e clean test - ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample -T1C -B -e clean test + ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample -T1C -e clean test + ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample -T1C -e clean test + ./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample -T1C -e clean test diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties index d58dfb7..c0bcafe 100644 --- a/.mvn/wrapper/maven-wrapper.properties +++ b/.mvn/wrapper/maven-wrapper.properties @@ -1,19 +1,3 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -wrapperVersion=3.3.2 +wrapperVersion=3.3.4 distributionType=only-script -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip diff --git a/README.md b/README.md index edaf131..d003cb4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # 演示例子 -大部分数据库连接为 H2Database,仅供测试。 +大部分数据库连接为 `h2database` 或通过 `testcontainers-java` 启动的 `mysql-server`,仅供测试。讨论不同情况, -除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 [OpenJDK 8, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试。 +1. `com.baomidou:springboot3-sample` 可在 [OpenJDK 17, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试 +2. 除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 [OpenJDK 8, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试 所有单元测试在 Github Actions 完成验证。你可能希望参考 [位于 Github Actions 的 CI 文件](./.github/workflows/ci.yml)。 @@ -29,10 +30,15 @@ ## Contributing 我们欢迎社区的贡献。围绕此 git 的讨论与协作应通过 https://github.com/baomidou/dynamic-datasource/issues 进行。 - -针对 IDE,项目的语言级别应设置为 JDK 8,对于单独的 `com.baomidou:springboot3-sample` 子模块,语言级别应设置为 JDK 17 。 在提交 Pull Request 之前, 请在本地通过 [OpenJDK 17, OpenJDK 21] 的 JDK 范围下完成此命令的验证。 -我们鼓励通过 `SDKMAN!` 切换到 `21.0.2-graalce` 来验证。 +我们鼓励通过 `SDKMAN!` 或 `version-fox/vfox` 切换到 `21.0.2-graalce` 来验证。 + +针对 IntelliJ IDEA 等 IDE,项目的语言级别应设置为 JDK 8,对于单独的 `com.baomidou:springboot3-sample` 子模块,语言级别应设置为 JDK 17。 +下文讨论不同情况下可能的测试流程, + +### Ubuntu 24.04 + +假设 `SDKMAN!` 和 `Docker Engine` 已安装,在 Bash 执行如下命令, ```shell sdk install java 21.0.2-graalce @@ -42,3 +48,19 @@ git clone git@github.com:dynamic-datasource/dynamic-datasource-samples.git cd ./dynamic-datasource-samples/ ./mvnw -T1C -e clean test ``` + +### Windows 11 + +假设 `PowerShell/PowerShell`, `version-fox/vfox`, `git-for-windows/git` 和 `rancher-sandbox/rancher-desktop` 已安装, +在 `PowerShell 7` 执行如下命令, + +```shell +rdctl start --application.start-in-background --container-engine.name=moby --kubernetes.enabled=false +vfox add java +vfox install java@21.0.2-graalce +vfox use --global java@21.0.2-graalce + +git clone git@github.com:dynamic-datasource/dynamic-datasource-samples.git +cd ./dynamic-datasource-samples/ +./mvnw -T1C -e clean test +``` diff --git a/mvnw b/mvnw index 19529dd..bd8896b 100755 --- a/mvnw +++ b/mvnw @@ -19,7 +19,7 @@ # ---------------------------------------------------------------------------- # ---------------------------------------------------------------------------- -# Apache Maven Wrapper startup batch script, version 3.3.2 +# Apache Maven Wrapper startup batch script, version 3.3.4 # # Optional ENV vars # ----------------- @@ -105,14 +105,17 @@ trim() { printf "%s" "${1}" | tr -d '[:space:]' } +scriptDir="$(dirname "$0")" +scriptName="$(basename "$0")" + # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties while IFS="=" read -r key value; do case "${key-}" in distributionUrl) distributionUrl=$(trim "${value-}") ;; distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; esac -done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" -[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" +done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" case "${distributionUrl##*/}" in maven-mvnd-*bin.*) @@ -130,7 +133,7 @@ maven-mvnd-*bin.*) distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" ;; maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; -*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; esac # apply MVNW_REPOURL and calculate MAVEN_HOME @@ -227,7 +230,7 @@ if [ -n "${distributionSha256Sum-}" ]; then echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 exit 1 elif command -v sha256sum >/dev/null; then - if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then distributionSha256Result=true fi elif command -v shasum >/dev/null; then @@ -252,8 +255,41 @@ if command -v unzip >/dev/null; then else tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" fi -printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" -mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +actualDistributionDir="" + +# First try the expected directory name (for regular distributions) +if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then + if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then + actualDistributionDir="$distributionUrlNameMain" + fi +fi + +# If not found, search for any directory with the Maven executable (for snapshots) +if [ -z "$actualDistributionDir" ]; then + # enable globbing to iterate over items + set +f + for dir in "$TMP_DOWNLOAD_DIR"/*; do + if [ -d "$dir" ]; then + if [ -f "$dir/bin/$MVN_CMD" ]; then + actualDistributionDir="$(basename "$dir")" + break + fi + fi + done + set -f +fi + +if [ -z "$actualDistributionDir" ]; then + verbose "Contents of $TMP_DOWNLOAD_DIR:" + verbose "$(ls -la "$TMP_DOWNLOAD_DIR")" + die "Could not find Maven distribution directory in extracted archive" +fi + +verbose "Found extracted Maven distribution directory: $actualDistributionDir" +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" clean || : exec_maven "$@" diff --git a/mvnw.cmd b/mvnw.cmd index 249bdf3..92450f9 100644 --- a/mvnw.cmd +++ b/mvnw.cmd @@ -19,7 +19,7 @@ @REM ---------------------------------------------------------------------------- @REM ---------------------------------------------------------------------------- -@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM Apache Maven Wrapper startup batch script, version 3.3.4 @REM @REM Optional ENV vars @REM MVNW_REPOURL - repo url base for downloading maven distribution @@ -40,7 +40,7 @@ @SET __MVNW_ARG0_NAME__= @SET MVNW_USERNAME= @SET MVNW_PASSWORD= -@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) +@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*) @echo Cannot start maven from wrapper >&2 && exit /b 1 @GOTO :EOF : end batch / begin powershell #> @@ -73,16 +73,30 @@ switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { # apply MVNW_REPOURL and calculate MAVEN_HOME # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ if ($env:MVNW_REPOURL) { - $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } - $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" + $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')" } $distributionUrlName = $distributionUrl -replace '^.*/','' $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' -$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" + +$MAVEN_M2_PATH = "$HOME/.m2" if ($env:MAVEN_USER_HOME) { - $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" + $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME" +} + +if (-not (Test-Path -Path $MAVEN_M2_PATH)) { + New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null +} + +$MAVEN_WRAPPER_DISTS = $null +if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { + $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists" +} else { + $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists" } -$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' + +$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain" +$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { @@ -134,7 +148,33 @@ if ($distributionSha256Sum) { # unzip and move Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null -Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +$actualDistributionDir = "" + +# First try the expected directory name (for regular distributions) +$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain" +$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD" +if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) { + $actualDistributionDir = $distributionUrlNameMain +} + +# If not found, search for any directory with the Maven executable (for snapshots) +if (!$actualDistributionDir) { + Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object { + $testPath = Join-Path $_.FullName "bin/$MVN_CMD" + if (Test-Path -Path $testPath -PathType Leaf) { + $actualDistributionDir = $_.Name + } + } +} + +if (!$actualDistributionDir) { + Write-Error "Could not find Maven distribution directory in extracted archive" +} + +Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir" +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null try { Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null } catch { diff --git a/pom.xml b/pom.xml index 9b723c1..b77ca83 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,8 @@ 2.2.224 2.7.18 1.18.30 + + 1.21.3 @@ -53,6 +55,13 @@ pom import + + org.testcontainers + testcontainers-bom + ${testcontainers-bom.version} + pom + import + org.springdoc springdoc-openapi-ui diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/MapperLayerTests.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/MapperLayerTests.java index 0e141d3..07cfd6e 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/MapperLayerTests.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/MapperLayerTests.java @@ -33,7 +33,6 @@ /** * TODO Unit tests for ShardingSphere 5.4.0 need to be synchronized. - * Tracked on When a logic database uses both `SHARDING` and `READWRITE_SPLITTING` features, CRUD operations on table throw `NoSuchTableException` */ @SpringBootTest @Disabled("TODO wait for com.baomidou:dynamic-datasource-spring-boot-starter:4.2.0 release") @@ -54,21 +53,21 @@ void after() { @Test void whenRequestToFindAll() { - assertEquals(tOrderMapper.findAll().size(), 0); + assertEquals(0, tOrderMapper.findAll().size()); } @Test void whenRequestToAddByNameAndUserId() { int firstNumberOfAffectedRows = tOrderMapper.addAll(101L, "Bright", 114514L); int secondNumberOfAffectedRows = tOrderMapper.addAll(102L, "Jordan", 114515L); - assertEquals(firstNumberOfAffectedRows + secondNumberOfAffectedRows, 2); - assertEquals(tOrderMapper.findAll().size(), 2); + assertEquals(2, firstNumberOfAffectedRows + secondNumberOfAffectedRows); + assertEquals(2, tOrderMapper.findAll().size()); } @Test void whenRequestToAddByNameAndUserIdWithPrimaryKey() { List emptyState = tOrderMapper.findAll(); - assertEquals(emptyState.size(), 0); + assertEquals(0, emptyState.size()); assertThrows(UncategorizedSQLException.class, () -> { tOrderMapper.addByNameAndUserId("Bright", 114514L); tOrderMapper.addByNameAndUserId("Jordan", 114515L); @@ -84,8 +83,8 @@ void whenRequestToDeleteByIdTest() { tOrderMapper.addAll(105L, "Michael", 114518L); tOrderMapper.addAll(106L, "Tony", 114519L); int numberOfAffectedRows = tOrderMapper.deleteById(114514L); - assertEquals(numberOfAffectedRows, 1); - assertEquals(tOrderMapper.findAll().size(), 5); + assertEquals(1, numberOfAffectedRows); + assertEquals(5, tOrderMapper.findAll().size()); } @Test @@ -97,7 +96,7 @@ void whenRequestToDeleteAll() { tOrderMapper.addAll(105L, "Michael", 114518L); tOrderMapper.addAll(106L, "Tony", 114519L); int numberOfAffectedRows = tOrderMapper.deleteAll(); - assertEquals(numberOfAffectedRows, 6); - assertEquals(tOrderMapper.findAll().size(), 0); + assertEquals(6, numberOfAffectedRows); + assertEquals(0, tOrderMapper.findAll().size()); } } diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/pom.xml b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/pom.xml index 25c094b..1aa0b66 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/pom.xml +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/pom.xml @@ -8,31 +8,31 @@ 1.0.0 4.0.0 - shardingsphere-jdbc-5.x-core-sample - 5.5.2 + 33.4.6-jre + 2.2 - + + + + org.yaml + snakeyaml + ${snakeyaml.version} + + + com.google.guava + guava + ${guava.version} + + + org.springframework.boot spring-boot-starter-web - - org.springdoc - springdoc-openapi-ui - - - com.baomidou - dynamic-datasource-spring-boot-starter - - - org.apache.shardingsphere - shardingsphere-jdbc - ${shardingsphere.version} - org.mybatis.spring.boot mybatis-spring-boot-starter @@ -42,10 +42,15 @@ h2 runtime + + com.mysql + mysql-connector-j + runtime + org.projectlombok lombok - provided + true org.springframework.boot @@ -53,9 +58,33 @@ test - org.yaml - snakeyaml - 2.2 + org.mybatis.spring.boot + mybatis-spring-boot-starter-test + ${mybatis-spring-boot-starter.version} + test + + + org.testcontainers + junit-jupiter + test + + + + org.apache.shardingsphere + shardingsphere-jdbc + ${shardingsphere.version} + + + com.baomidou + dynamic-datasource-spring-boot-starter + + + org.testcontainers + mysql + + + org.springdoc + springdoc-openapi-ui diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/common/TcMySQLDatabaseType.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/common/TcMySQLDatabaseType.java new file mode 100644 index 0000000..1660959 --- /dev/null +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/common/TcMySQLDatabaseType.java @@ -0,0 +1,41 @@ +/* + * Copyright © ${project.inceptionYear} organization baomidou + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.baomidou.samples.shardingsphere.jdbc.v5.core.common; + +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; + +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; + +public final class TcMySQLDatabaseType implements DatabaseType { + + @Override + public Collection getJdbcUrlPrefixes() { + return Collections.singleton("jdbc:tc:mysql:"); + } + + @Override + public Optional getTrunkDatabaseType() { + return Optional.of(TypedSPILoader.getService(DatabaseType.class, "MySQL")); + } + + @Override + public String getType() { + return "TC-MySQL"; + } +} diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/dto/UserDto.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/dto/UserDto.java deleted file mode 100644 index 2f46cae..0000000 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/dto/UserDto.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright © ${project.inceptionYear} organization baomidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.baomidou.samples.shardingsphere.jdbc.v5.core.dto; - -import lombok.Data; - -@Data -public class UserDto { - private Integer id; - private String name; - private Integer age; -} diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/mapper/UserMapper.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/mapper/UserMapper.java index adec0c4..6b86e81 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/mapper/UserMapper.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/mapper/UserMapper.java @@ -24,6 +24,7 @@ import java.util.List; +@SuppressWarnings("SqlNoDataSourceInspection") @Component public interface UserMapper { @Select("select * from t_user") diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType new file mode 100644 index 0000000..56addb3 --- /dev/null +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.type.DatabaseType @@ -0,0 +1,12 @@ +# Copyright © ${project.inceptionYear} organization baomidou +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +com.baomidou.samples.shardingsphere.jdbc.v5.core.common.TcMySQLDatabaseType diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/application.yml b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/application.yml index 852fa8d..be84b95 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/application.yml +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/application.yml @@ -4,18 +4,18 @@ spring: dynamic: datasource: master: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:baomidou_master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' username: sa password: "" - url: jdbc:h2:mem:baomidou_master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - driver-class-name: org.h2.Driver test: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:baomidou_test;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' username: sa password: "" - url: jdbc:h2:mem:baomidou_test;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - driver-class-name: org.h2.Driver shardingSphere: - url: jdbc:shardingsphere:classpath:config.yaml driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver + url: jdbc:shardingsphere:classpath:config.yaml logging: level: com.baomidou: debug diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/config.yaml b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/config.yaml index e675caf..2040ba6 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/config.yaml +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/config.yaml @@ -1,23 +1,17 @@ dataSources: + # TODO The use of `test_hostname` is related to https://github.com/testcontainers/testcontainers-java/issues/4121 shardingmaster: dataSourceClassName: com.zaxxer.hikari.HikariDataSource - driverClassName: org.h2.Driver - jdbcUrl: jdbc:h2:mem:master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - username: sa - password: + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + jdbcUrl: jdbc:tc:mysql:8.4.7-oraclelinux9://test_hostname/master?TC_INITSCRIPT=db/schema.sql shardingslave0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource - driverClassName: org.h2.Driver - jdbcUrl: jdbc:h2:mem:slave1;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - username: sa - password: + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + jdbcUrl: jdbc:tc:mysql:8.4.7-oraclelinux9://test_hostname/slave1?TC_INITSCRIPT=db/schema.sql shardingslave1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource - driverClassName: org.h2.Driver - jdbcUrl: jdbc:h2:mem:slave2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - username: sa - password: - + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + jdbcUrl: jdbc:tc:mysql:8.4.7-oraclelinux9://test_hostname/slave2?TC_INITSCRIPT=db/schema.sql rules: - !SHARDING tables: @@ -34,8 +28,7 @@ rules: baomidou_inline: type: INLINE props: - # TODO Affected by https://github.com/apache/shardingsphere/issues/27955 . - algorithm-expression: t_order$->{ORDER_ID % 2} + algorithm-expression: t_order$->{order_id % 2} allow-range-query-with-inline-sharding: true keyGenerators: baomidou_snowflake: @@ -54,6 +47,5 @@ rules: - !SINGLE tables: - "*.*" - props: sql-show: true diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/db/schema.sql b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/db/schema.sql index 4f33599..1968c05 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/db/schema.sql +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/main/resources/db/schema.sql @@ -21,4 +21,4 @@ CREATE TABLE `t_order1` `order_id` BIGINT PRIMARY KEY, `name` VARCHAR(255), `user_id` BIGINT -); \ No newline at end of file +); diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/MapperLayerTests.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/MapperLayerTests.java index 04d71a1..8fdf8d3 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/MapperLayerTests.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/core/MapperLayerTests.java @@ -23,12 +23,11 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.dao.DataIntegrityViolationException; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; @SpringBootTest public class MapperLayerTests { @@ -48,25 +47,22 @@ void after() { @Test void whenRequestToFindAll() { - assertEquals(tOrderMapper.findAll().size(), 0); + assertEquals(0, tOrderMapper.findAll().size()); } @Test void whenRequestToAddByNameAndUserId() { int firstNumberOfAffectedRows = tOrderMapper.addByNameAndUserId("Bright", 114514L); int secondNumberOfAffectedRows = tOrderMapper.addByNameAndUserId("Jordan", 114515L); - assertEquals(firstNumberOfAffectedRows + secondNumberOfAffectedRows, 2); - assertEquals(tOrderMapper.findAll().size(), 2); + assertEquals(2, firstNumberOfAffectedRows + secondNumberOfAffectedRows); + assertEquals(2, tOrderMapper.findAll().size()); } - /** - * TODO Tracked on When a logic database uses both `SHARDING` and `READWRITE_SPLITTING` features, CRUD operations on table throw `NoSuchTableException` - */ @Test void whenRequestToAddByNameAndUserIdWithPrimaryKey() { List emptyState = tOrderMapper.findAll(); - assertEquals(emptyState.size(), 0); - assertThrows(DataIntegrityViolationException.class, () -> { + assertEquals(0, emptyState.size()); + assertDoesNotThrow(() -> { tOrderMapper.addAll(114514L, "Bright", 114514L); tOrderMapper.addAll(114515L, "Jordan", 114515L); }); @@ -81,8 +77,8 @@ void whenRequestToDeleteById() { tOrderMapper.addByNameAndUserId("Michael", 114518L); tOrderMapper.addByNameAndUserId("Tony", 114519L); int numberOfAffectedRows = tOrderMapper.deleteById(114514L); - assertEquals(numberOfAffectedRows, 1); - assertEquals(tOrderMapper.findAll().size(), 5); + assertEquals(1, numberOfAffectedRows); + assertEquals(5, tOrderMapper.findAll().size()); } @Test @@ -94,7 +90,7 @@ void whenRequestToDeleteAll() { tOrderMapper.addByNameAndUserId("Michael", 114518L); tOrderMapper.addByNameAndUserId("Tony", 114519L); int numberOfAffectedRows = tOrderMapper.deleteAll(); - assertEquals(numberOfAffectedRows, 6); - assertEquals(tOrderMapper.findAll().size(), 0); + assertEquals(6, numberOfAffectedRows); + assertEquals(0, tOrderMapper.findAll().size()); } -} \ No newline at end of file +} diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/pom.xml b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/pom.xml index 0be4bb2..27c6ceb 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/pom.xml +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/pom.xml @@ -8,31 +8,26 @@ 1.0.0 4.0.0 - shardingsphere-jdbc-5.x-spring-sample - 5.2.1 + + 1.33 - + + + + org.yaml + snakeyaml + ${snakeyaml.version} + + + org.springframework.boot spring-boot-starter-web - - org.springdoc - springdoc-openapi-ui - - - com.baomidou - dynamic-datasource-spring-boot-starter - - - org.apache.shardingsphere - shardingsphere-jdbc-core-spring-boot-starter - ${shardingsphere.version} - org.mybatis.spring.boot mybatis-spring-boot-starter @@ -45,19 +40,32 @@ org.projectlombok lombok - provided + true org.springframework.boot spring-boot-starter-test test - - org.yaml - snakeyaml - 1.33 + org.mybatis.spring.boot + mybatis-spring-boot-starter-test + ${mybatis-spring-boot-starter.version} + test - - \ No newline at end of file + + org.apache.shardingsphere + shardingsphere-jdbc-core-spring-boot-starter + ${shardingsphere.version} + + + com.baomidou + dynamic-datasource-spring-boot-starter + + + org.springdoc + springdoc-openapi-ui + + + diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/config/MyDataSourceConfiguration.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/config/MyDataSourceConfiguration.java index c2789b8..e88faba 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/config/MyDataSourceConfiguration.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/config/MyDataSourceConfiguration.java @@ -38,10 +38,10 @@ public class MyDataSourceConfiguration { /** * 1. 建议 spring-boot 2.5.0 以下版本或者发现不加 `@Lazy`, `DataSource shardingSphereDataSource` 是 null 的情况都打开 `@Lazy` - * 2. Compared with using SpringBoot Starter, if you encounter such problems, - * you should directly use ShardingSphere's JDBC Driver to configure it as a JDBC data source, that is, - * use `org.apache.shardingsphere:shardingsphere-jdbc-core:5.2.1` instead of `org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.2.1`. - * For more information see JDBC Driver + * 2. 与使用 Spring Boot Starter 相比,如果开发者遇到此类问题, + * 开发者应该直接使用 ShardingSphere 的 JDBC Driver 将其配置为 JDBC 数据源,即: + * 使用 `org.apache.shardingsphere:shardingsphere-jdbc-core:5.2.1` 而不是 `org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.2.1`。 + * 更多信息请参阅 JDBC Driver * * @see org.springframework.context.annotation.Lazy * @see org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/dto/UserDto.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/dto/UserDto.java deleted file mode 100644 index a3a2d87..0000000 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/dto/UserDto.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright © ${project.inceptionYear} organization baomidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.baomidou.samples.shardingsphere.jdbc.v5.spring.dto; - -import lombok.Data; - -@Data -public class UserDto { - private Integer id; - private String name; - private Integer age; -} diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/TOrderMapper.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/TOrderMapper.java index 639e88e..5342492 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/TOrderMapper.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/TOrderMapper.java @@ -24,8 +24,8 @@ import java.util.List; +@SuppressWarnings("SqlNoDataSourceInspection") @Component -@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection", "SqlResolve", "SqlWithoutWhere", "UnusedReturnValue"}) public interface TOrderMapper { @Select("select * from t_order") List findAll(); diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/UserMapper.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/UserMapper.java index d65f7ef..ecaefe1 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/UserMapper.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/mapper/UserMapper.java @@ -24,6 +24,7 @@ import java.util.List; +@SuppressWarnings("SqlNoDataSourceInspection") @Component public interface UserMapper { @Select("select * from t_user") diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/service/impl/TOrderServiceImpl.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/service/impl/TOrderServiceImpl.java index 94f206b..680ce8b 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/service/impl/TOrderServiceImpl.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/service/impl/TOrderServiceImpl.java @@ -25,7 +25,7 @@ import java.util.stream.IntStream; /** - * TODO Unit tests for ShardingSphere 5.4.0 need to be synchronized + * TODO Unit tests for ShardingSphere 5.5.2 need to be synchronized */ @Service public class TOrderServiceImpl implements TOrderService { @@ -44,8 +44,7 @@ public List findAll() { @Override @DS("shardingSphere") public List addAll() { - IntStream.range(0, 5) - .forEach(i -> tOrderMapper.addAll(i + 114514L, "测试" + i, (long) i)); + IntStream.range(0, 5).forEach(i -> tOrderMapper.addAll(i + 114514L, "测试" + i, (long) i)); return tOrderMapper.findAll(); } } \ No newline at end of file diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/application.yml b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/application.yml index b3c9045..b91c603 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/application.yml +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/application.yml @@ -58,15 +58,15 @@ spring: dynamic: datasource: master: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:baomidou_master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' username: sa password: "" - url: jdbc:h2:mem:baomidou_master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - driver-class-name: org.h2.Driver test: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:baomidou_test;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' username: sa password: "" - url: jdbc:h2:mem:baomidou_test;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql' - driver-class-name: org.h2.Driver logging: level: - com.baomidou: debug \ No newline at end of file + com.baomidou: debug diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/db/schema.sql b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/db/schema.sql index 4f33599..1968c05 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/db/schema.sql +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/main/resources/db/schema.sql @@ -21,4 +21,4 @@ CREATE TABLE `t_order1` `order_id` BIGINT PRIMARY KEY, `name` VARCHAR(255), `user_id` BIGINT -); \ No newline at end of file +); diff --git a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/MapperLayerTests.java b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/MapperLayerTests.java index f280a9f..04c1a21 100644 --- a/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/MapperLayerTests.java +++ b/third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v5/spring/MapperLayerTests.java @@ -31,8 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; /** - * TODO Unit tests for ShardingSphere 5.4.0 need to be synchronized. - * Tracked on When a logic database uses both `SHARDING` and `READWRITE_SPLITTING` features, CRUD operations on table throw `NoSuchTableException` + * TODO Unit tests for ShardingSphere 5.5.2 need to be synchronized. */ @SpringBootTest public class MapperLayerTests { @@ -52,21 +51,21 @@ void after() { @Test void whenRequestToFindAll() { - assertEquals(tOrderMapper.findAll().size(), 0); + assertEquals(0, tOrderMapper.findAll().size()); } @Test void whenRequestToAddByNameAndUserId() { int firstNumberOfAffectedRows = tOrderMapper.addAll(101L, "Bright", 114514L); int secondNumberOfAffectedRows = tOrderMapper.addAll(102L, "Jordan", 114515L); - assertEquals(firstNumberOfAffectedRows + secondNumberOfAffectedRows, 2); - assertEquals(tOrderMapper.findAll().size(), 2); + assertEquals(2, firstNumberOfAffectedRows + secondNumberOfAffectedRows); + assertEquals(2, tOrderMapper.findAll().size()); } @Test void whenRequestToAddByNameAndUserIdWithPrimaryKey() { List emptyState = tOrderMapper.findAll(); - assertEquals(emptyState.size(), 0); + assertEquals(0, emptyState.size()); assertThrows(UncategorizedSQLException.class, () -> { tOrderMapper.addByNameAndUserId("Bright", 114514L); tOrderMapper.addByNameAndUserId("Jordan", 114515L); @@ -82,8 +81,8 @@ void whenRequestToDeleteByIdTest() { tOrderMapper.addAll(105L, "Michael", 114518L); tOrderMapper.addAll(106L, "Tony", 114519L); int numberOfAffectedRows = tOrderMapper.deleteById(114514L); - assertEquals(numberOfAffectedRows, 1); - assertEquals(tOrderMapper.findAll().size(), 5); + assertEquals(1, numberOfAffectedRows); + assertEquals(5, tOrderMapper.findAll().size()); } @Test @@ -95,7 +94,7 @@ void whenRequestToDeleteAll() { tOrderMapper.addAll(105L, "Michael", 114518L); tOrderMapper.addAll(106L, "Tony", 114519L); int numberOfAffectedRows = tOrderMapper.deleteAll(); - assertEquals(numberOfAffectedRows, 6); - assertEquals(tOrderMapper.findAll().size(), 0); + assertEquals(6, numberOfAffectedRows); + assertEquals(0, tOrderMapper.findAll().size()); } } \ No newline at end of file