From 2c67bd3a09a8086910c31d35de90fcc02aca934d Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 21 Sep 2024 20:04:22 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91=E4=BF=A1=E5=88=9B=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20SqlConstants=20=E7=B1=BB=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E5=96=84=20limit=20=E7=9A=84=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IdTypeEnvironmentPostProcessor.java | 4 ---- .../mybatis/core/enums/DbTypeEnum.java | 4 ++++ .../mybatis/core/enums/SqlConstants.java | 21 ------------------- .../mybatis/core/mapper/BaseMapperX.java | 17 +++++---------- .../mybatis/core/query/QueryWrapperX.java | 10 ++++----- 5 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/SqlConstants.java diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/IdTypeEnvironmentPostProcessor.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/IdTypeEnvironmentPostProcessor.java index 56a450a45..53f0d4089 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/IdTypeEnvironmentPostProcessor.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/IdTypeEnvironmentPostProcessor.java @@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.mybatis.config; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.util.collection.SetUtils; -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.IdType; @@ -42,9 +41,6 @@ public class IdTypeEnvironmentPostProcessor implements EnvironmentPostProcessor // TODO 芋艿:暂时没有找到特别合适的地方,先放在这里 setJobStoreDriverIfPresent(environment, dbType); - // 初始化 SQL 静态变量 - SqlConstants.init(dbType); - // 如果非 NONE,则不进行处理 IdType idType = getIdType(environment); if (idType != IdType.NONE) { diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java index 974986fca..cecc12d46 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java @@ -39,6 +39,10 @@ public enum DbTypeEnum { * SQL Server */ SQL_SERVER(DbType.SQL_SERVER, "Microsoft SQL Server", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0"), + /** + * SQL Server 2005 + */ + SQL_SERVER2005(DbType.SQL_SERVER2005, "Microsoft SQL Server 2005", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0"), /** * 达梦 diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/SqlConstants.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/SqlConstants.java deleted file mode 100644 index d775f17c7..000000000 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/SqlConstants.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.mybatis.core.enums; - -import com.baomidou.mybatisplus.annotation.DbType; - -/** - * SQL相关常量类 - * - * @author 芋道源码 - */ -public class SqlConstants { - - /** - * 数据库的类型 - */ - public static DbType DB_TYPE; - - public static void init(DbType dbType) { - DB_TYPE = dbType; - } - -} 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 3137885d6..01f214230 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 @@ -5,9 +5,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam; 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; @@ -134,11 +134,6 @@ public interface BaseMapperX extends MPJBaseMapper { return selectList(new LambdaQueryWrapper().in(field, values)); } - @Deprecated - default List selectList(SFunction leField, SFunction geField, Object value) { - return selectList(new LambdaQueryWrapper().le(leField, value).ge(geField, value)); - } - default List selectList(SFunction field1, Object value1, SFunction field2, Object value2) { return selectList(new LambdaQueryWrapper().eq(field1, value1).eq(field2, value2)); } @@ -150,7 +145,8 @@ public interface BaseMapperX extends MPJBaseMapper { */ default Boolean insertBatch(Collection entities) { // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 - if (JdbcUtils.isSQLServer(SqlConstants.DB_TYPE)) { + DbType dbType = JdbcUtils.getDbType(); + if (JdbcUtils.isSQLServer(dbType)) { entities.forEach(this::insert); return CollUtil.isNotEmpty(entities); } @@ -165,7 +161,8 @@ public interface BaseMapperX extends MPJBaseMapper { */ default Boolean insertBatch(Collection entities, int size) { // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 - if (JdbcUtils.isSQLServer(SqlConstants.DB_TYPE)) { + DbType dbType = JdbcUtils.getDbType(); + if (JdbcUtils.isSQLServer(dbType)) { entities.forEach(this::insert); return CollUtil.isNotEmpty(entities); } @@ -184,10 +181,6 @@ public interface BaseMapperX extends MPJBaseMapper { return Db.updateBatchById(entities, size); } - default Boolean insertOrUpdateBatch(Collection collection) { - return Db.saveOrUpdateBatch(collection); - } - default int delete(String field, String value) { return delete(new QueryWrapper().eq(field, value)); } diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/QueryWrapperX.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/QueryWrapperX.java index eec4172f1..57862ebb0 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/QueryWrapperX.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/QueryWrapperX.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.mybatis.core.query; -import cn.hutool.core.lang.Assert; -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; +import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; +import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; @@ -147,8 +147,8 @@ public class QueryWrapperX extends QueryWrapper { * @return this */ public QueryWrapperX limitN(int n) { - Assert.notNull(SqlConstants.DB_TYPE, "获取不到数据库的类型"); - switch (SqlConstants.DB_TYPE) { + DbType dbType = JdbcUtils.getDbType(); + switch (dbType) { case ORACLE: case ORACLE_12C: super.le("ROWNUM", n); @@ -157,7 +157,7 @@ public class QueryWrapperX extends QueryWrapper { case SQL_SERVER2005: super.select("TOP " + n + " *"); // 由于 SQL Server 是通过 SELECT TOP 1 实现限制一条,所以只好使用 * 查询剩余字段 break; - default: + default: // MySQL、PostgreSQL、DM 达梦都是采用 LIMIT 实现 super.last("LIMIT " + n); } return this;