Commit 2c67bd3a09a8086910c31d35de90fcc02aca934d
1 parent
b13dc100
【功能优化】信创数据库:移除 SqlConstants 类,完善 limit 的适配
Showing
5 changed files
with
14 additions
and
42 deletions
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; | @@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.mybatis.config; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | 4 | import cn.iocoder.yudao.framework.common.util.collection.SetUtils; |
| 5 | -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; | ||
| 6 | import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; | 5 | import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; |
| 7 | import com.baomidou.mybatisplus.annotation.DbType; | 6 | import com.baomidou.mybatisplus.annotation.DbType; |
| 8 | import com.baomidou.mybatisplus.annotation.IdType; | 7 | import com.baomidou.mybatisplus.annotation.IdType; |
| @@ -42,9 +41,6 @@ public class IdTypeEnvironmentPostProcessor implements EnvironmentPostProcessor | @@ -42,9 +41,6 @@ public class IdTypeEnvironmentPostProcessor implements EnvironmentPostProcessor | ||
| 42 | // TODO 芋艿:暂时没有找到特别合适的地方,先放在这里 | 41 | // TODO 芋艿:暂时没有找到特别合适的地方,先放在这里 |
| 43 | setJobStoreDriverIfPresent(environment, dbType); | 42 | setJobStoreDriverIfPresent(environment, dbType); |
| 44 | 43 | ||
| 45 | - // 初始化 SQL 静态变量 | ||
| 46 | - SqlConstants.init(dbType); | ||
| 47 | - | ||
| 48 | // 如果非 NONE,则不进行处理 | 44 | // 如果非 NONE,则不进行处理 |
| 49 | IdType idType = getIdType(environment); | 45 | IdType idType = getIdType(environment); |
| 50 | if (idType != IdType.NONE) { | 46 | if (idType != IdType.NONE) { |
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 { | @@ -39,6 +39,10 @@ public enum DbTypeEnum { | ||
| 39 | * SQL Server | 39 | * SQL Server |
| 40 | */ | 40 | */ |
| 41 | SQL_SERVER(DbType.SQL_SERVER, "Microsoft SQL Server", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0"), | 41 | SQL_SERVER(DbType.SQL_SERVER, "Microsoft SQL Server", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0"), |
| 42 | + /** | ||
| 43 | + * SQL Server 2005 | ||
| 44 | + */ | ||
| 45 | + SQL_SERVER2005(DbType.SQL_SERVER2005, "Microsoft SQL Server 2005", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0"), | ||
| 42 | 46 | ||
| 43 | /** | 47 | /** |
| 44 | * 达梦 | 48 | * 达梦 |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/SqlConstants.java deleted
| 1 | -package cn.iocoder.yudao.framework.mybatis.core.enums; | ||
| 2 | - | ||
| 3 | -import com.baomidou.mybatisplus.annotation.DbType; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * SQL相关常量类 | ||
| 7 | - * | ||
| 8 | - * @author 芋道源码 | ||
| 9 | - */ | ||
| 10 | -public class SqlConstants { | ||
| 11 | - | ||
| 12 | - /** | ||
| 13 | - * 数据库的类型 | ||
| 14 | - */ | ||
| 15 | - public static DbType DB_TYPE; | ||
| 16 | - | ||
| 17 | - public static void init(DbType dbType) { | ||
| 18 | - DB_TYPE = dbType; | ||
| 19 | - } | ||
| 20 | - | ||
| 21 | -} |
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; | @@ -5,9 +5,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam; | ||
| 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | import cn.iocoder.yudao.framework.common.pojo.SortablePageParam; | 6 | import cn.iocoder.yudao.framework.common.pojo.SortablePageParam; |
| 7 | import cn.iocoder.yudao.framework.common.pojo.SortingField; | 7 | import cn.iocoder.yudao.framework.common.pojo.SortingField; |
| 8 | -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; | ||
| 9 | import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; | 8 | import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; |
| 10 | import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; | 9 | import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; |
| 10 | +import com.baomidou.mybatisplus.annotation.DbType; | ||
| 11 | import com.baomidou.mybatisplus.core.conditions.Wrapper; | 11 | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| 12 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 12 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 13 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 13 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| @@ -134,11 +134,6 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | @@ -134,11 +134,6 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | ||
| 134 | return selectList(new LambdaQueryWrapper<T>().in(field, values)); | 134 | return selectList(new LambdaQueryWrapper<T>().in(field, values)); |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | - @Deprecated | ||
| 138 | - default List<T> selectList(SFunction<T, ?> leField, SFunction<T, ?> geField, Object value) { | ||
| 139 | - return selectList(new LambdaQueryWrapper<T>().le(leField, value).ge(geField, value)); | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | default List<T> selectList(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2) { | 137 | default List<T> selectList(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2) { |
| 143 | return selectList(new LambdaQueryWrapper<T>().eq(field1, value1).eq(field2, value2)); | 138 | return selectList(new LambdaQueryWrapper<T>().eq(field1, value1).eq(field2, value2)); |
| 144 | } | 139 | } |
| @@ -150,7 +145,8 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | @@ -150,7 +145,8 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | ||
| 150 | */ | 145 | */ |
| 151 | default Boolean insertBatch(Collection<T> entities) { | 146 | default Boolean insertBatch(Collection<T> entities) { |
| 152 | // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 | 147 | // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 |
| 153 | - if (JdbcUtils.isSQLServer(SqlConstants.DB_TYPE)) { | 148 | + DbType dbType = JdbcUtils.getDbType(); |
| 149 | + if (JdbcUtils.isSQLServer(dbType)) { | ||
| 154 | entities.forEach(this::insert); | 150 | entities.forEach(this::insert); |
| 155 | return CollUtil.isNotEmpty(entities); | 151 | return CollUtil.isNotEmpty(entities); |
| 156 | } | 152 | } |
| @@ -165,7 +161,8 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | @@ -165,7 +161,8 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | ||
| 165 | */ | 161 | */ |
| 166 | default Boolean insertBatch(Collection<T> entities, int size) { | 162 | default Boolean insertBatch(Collection<T> entities, int size) { |
| 167 | // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 | 163 | // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 |
| 168 | - if (JdbcUtils.isSQLServer(SqlConstants.DB_TYPE)) { | 164 | + DbType dbType = JdbcUtils.getDbType(); |
| 165 | + if (JdbcUtils.isSQLServer(dbType)) { | ||
| 169 | entities.forEach(this::insert); | 166 | entities.forEach(this::insert); |
| 170 | return CollUtil.isNotEmpty(entities); | 167 | return CollUtil.isNotEmpty(entities); |
| 171 | } | 168 | } |
| @@ -184,10 +181,6 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | @@ -184,10 +181,6 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | ||
| 184 | return Db.updateBatchById(entities, size); | 181 | return Db.updateBatchById(entities, size); |
| 185 | } | 182 | } |
| 186 | 183 | ||
| 187 | - default Boolean insertOrUpdateBatch(Collection<T> collection) { | ||
| 188 | - return Db.saveOrUpdateBatch(collection); | ||
| 189 | - } | ||
| 190 | - | ||
| 191 | default int delete(String field, String value) { | 184 | default int delete(String field, String value) { |
| 192 | return delete(new QueryWrapper<T>().eq(field, value)); | 185 | return delete(new QueryWrapper<T>().eq(field, value)); |
| 193 | } | 186 | } |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/QueryWrapperX.java
| 1 | package cn.iocoder.yudao.framework.mybatis.core.query; | 1 | package cn.iocoder.yudao.framework.mybatis.core.query; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.lang.Assert; | ||
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; | 3 | +import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; |
| 4 | +import com.baomidou.mybatisplus.annotation.DbType; | ||
| 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 6 | import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; | 6 | import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; |
| 7 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | 7 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| @@ -147,8 +147,8 @@ public class QueryWrapperX<T> extends QueryWrapper<T> { | @@ -147,8 +147,8 @@ public class QueryWrapperX<T> extends QueryWrapper<T> { | ||
| 147 | * @return this | 147 | * @return this |
| 148 | */ | 148 | */ |
| 149 | public QueryWrapperX<T> limitN(int n) { | 149 | public QueryWrapperX<T> limitN(int n) { |
| 150 | - Assert.notNull(SqlConstants.DB_TYPE, "获取不到数据库的类型"); | ||
| 151 | - switch (SqlConstants.DB_TYPE) { | 150 | + DbType dbType = JdbcUtils.getDbType(); |
| 151 | + switch (dbType) { | ||
| 152 | case ORACLE: | 152 | case ORACLE: |
| 153 | case ORACLE_12C: | 153 | case ORACLE_12C: |
| 154 | super.le("ROWNUM", n); | 154 | super.le("ROWNUM", n); |
| @@ -157,7 +157,7 @@ public class QueryWrapperX<T> extends QueryWrapper<T> { | @@ -157,7 +157,7 @@ public class QueryWrapperX<T> extends QueryWrapper<T> { | ||
| 157 | case SQL_SERVER2005: | 157 | case SQL_SERVER2005: |
| 158 | super.select("TOP " + n + " *"); // 由于 SQL Server 是通过 SELECT TOP 1 实现限制一条,所以只好使用 * 查询剩余字段 | 158 | super.select("TOP " + n + " *"); // 由于 SQL Server 是通过 SELECT TOP 1 实现限制一条,所以只好使用 * 查询剩余字段 |
| 159 | break; | 159 | break; |
| 160 | - default: | 160 | + default: // MySQL、PostgreSQL、DM 达梦都是采用 LIMIT 实现 |
| 161 | super.last("LIMIT " + n); | 161 | super.last("LIMIT " + n); |
| 162 | } | 162 | } |
| 163 | return this; | 163 | return this; |