From af5520db46ef6b8000d66708ac17088389a5b17d Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 17 Sep 2024 20:39:34 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=BC=BA=E9=99=B7=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91=E5=85=A8=E5=B1=80=EF=BC=9A=E5=AE=8C=E5=96=84=E5=AF=B9?= =?UTF-8?q?=20SQLServer=202015=20=E7=9A=84=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/core/mapper/BaseMapperX.java | 7 +++--- .../mybatis/core/util/JdbcUtils.java | 22 +++++++++++++++++++ .../service/db/DatabaseTableServiceImpl.java | 5 +---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java index 99a6c5147..3137885d6 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java @@ -6,8 +6,8 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.SortablePageParam; import cn.iocoder.yudao.framework.common.pojo.SortingField; import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; +import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; -import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -22,7 +22,6 @@ import org.apache.ibatis.annotations.Param; import java.util.Collection; import java.util.List; -import java.util.Objects; /** * 在 MyBatis Plus 的 BaseMapper 的基础上拓展,提供更多的能力 @@ -151,7 +150,7 @@ public interface BaseMapperX extends MPJBaseMapper { */ default Boolean insertBatch(Collection entities) { // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 - if (Objects.equals(SqlConstants.DB_TYPE, DbType.SQL_SERVER)) { + if (JdbcUtils.isSQLServer(SqlConstants.DB_TYPE)) { entities.forEach(this::insert); return CollUtil.isNotEmpty(entities); } @@ -166,7 +165,7 @@ public interface BaseMapperX extends MPJBaseMapper { */ default Boolean insertBatch(Collection entities, int size) { // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 - if (Objects.equals(SqlConstants.DB_TYPE, DbType.SQL_SERVER)) { + if (JdbcUtils.isSQLServer(SqlConstants.DB_TYPE)) { entities.forEach(this::insert); return CollUtil.isNotEmpty(entities); } diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java index c4894cad0..1e7e805c4 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.framework.mybatis.core.util; +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; import cn.iocoder.yudao.framework.common.util.spring.SpringUtils; import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum; import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; @@ -58,4 +59,25 @@ public class JdbcUtils { } } + /** + * 判断 JDBC 连接是否为 SQLServer 数据库 + * + * @param url JDBC 连接 + * @return 是否为 SQLServer 数据库 + */ + public static boolean isSQLServer(String url) { + DbType dbType = getDbType(url); + return isSQLServer(dbType); + } + + /** + * 判断 JDBC 连接是否为 SQLServer 数据库 + * + * @param url JDBC 连接 + * @return 是否为 SQLServer 数据库 + */ + public static boolean isSQLServer(DbType dbType) { + return ObjectUtils.equalsAny(dbType, DbType.SQL_SERVER, DbType.SQL_SERVER2005); + } + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java index 6d3882208..289cae1e3 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java @@ -5,7 +5,6 @@ import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; -import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; @@ -18,7 +17,6 @@ import org.springframework.stereotype.Service; import java.util.Comparator; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; /** @@ -49,12 +47,11 @@ public class DatabaseTableServiceImpl implements DatabaseTableService { // 获得数据源配置 DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId); Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId); - DbType dbType = JdbcUtils.getDbType(config.getUrl()); // 使用 MyBatis Plus Generator 解析表结构 DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(config.getUrl(), config.getUsername(), config.getPassword()); - if (Objects.equals(dbType, DbType.SQL_SERVER)) { // 特殊:SQLServer jdbc 非标准,参见 https://github.com/baomidou/mybatis-plus/issues/5419 + if (JdbcUtils.isSQLServer(config.getUrl())) { // 特殊:SQLServer jdbc 非标准,参见 https://github.com/baomidou/mybatis-plus/issues/5419 dataSourceConfigBuilder.databaseQueryClass(SQLQuery.class); } StrategyConfig.Builder strategyConfig = new StrategyConfig.Builder().enableSkipView(); // 忽略视图,业务上一般用不到