From 24e68a715b85a9dadc67efce63c53d4d8715e066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 8 Jun 2023 14:52:22 +0800 Subject: [PATCH] optimize: move SqlUtil.getSelectOneResult method to MapperUtil --- .../java/com/mybatisflex/core/BaseMapper.java | 6 ++--- .../com/mybatisflex/core/util/MapperUtil.java | 19 ++++++++++++++ .../com/mybatisflex/core/util/SqlUtil.java | 25 ++++++------------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index e1cb9994..0860ee05 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -345,7 +345,7 @@ public interface BaseMapper { * @return entity 数据 */ default T selectOneByQuery(QueryWrapper queryWrapper) { - return SqlUtil.getSelectOneResult(selectListByQuery(queryWrapper)); + return MapperUtil.getSelectOneResult(selectListByQuery(queryWrapper)); } @@ -357,7 +357,7 @@ public interface BaseMapper { * @return 数据内容 */ default R selectOneByQueryAs(QueryWrapper queryWrapper, Class asType) { - return SqlUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType)); + return MapperUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType)); } /** @@ -503,7 +503,7 @@ public interface BaseMapper { * @return 数据量 */ default Object selectObjectByQuery(QueryWrapper queryWrapper) { - return SqlUtil.getSelectOneResult(selectObjectListByQuery(queryWrapper)); + return MapperUtil.getSelectOneResult(selectObjectListByQuery(queryWrapper)); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java index 451b00e4..d08ab41d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java @@ -19,6 +19,8 @@ import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.field.FieldQuery; import com.mybatisflex.core.field.FieldQueryBuilder; import com.mybatisflex.core.query.QueryWrapper; +import org.apache.ibatis.exceptions.TooManyResultsException; +import org.apache.ibatis.session.defaults.DefaultSqlSession; import java.util.Collection; import java.util.HashSet; @@ -60,4 +62,21 @@ public class MapperUtil { } }); } + + + + /** + * 搬运加改造 {@link DefaultSqlSession#selectOne(String, Object)} + */ + public static T getSelectOneResult(List list) { + if (list == null || list.isEmpty()) { + return null; + } + int size = list.size(); + if (size == 1) { + return list.get(0); + } + throw new TooManyResultsException( + "Expected one result (or null) to be returned by selectOne(), but found: " + size); + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java index 584263ba..fedae9d1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java @@ -15,12 +15,8 @@ */ package com.mybatisflex.core.util; -import org.apache.ibatis.exceptions.TooManyResultsException; -import org.apache.ibatis.session.defaults.DefaultSqlSession; - import java.time.LocalDateTime; import java.util.Date; -import java.util.List; import java.util.regex.Matcher; public class SqlUtil { @@ -83,6 +79,12 @@ public class SqlUtil { } + /** + * 替换 sql 中的问号 ? + * @param sql sql 内容 + * @param params 参数 + * @return 完整的 sql + */ public static String replaceSqlParams(String sql, Object[] params) { if (params != null && params.length > 0) { for (Object value : params) { @@ -113,19 +115,6 @@ public class SqlUtil { return sql; } - /** - * 搬运加改造 {@link DefaultSqlSession#selectOne(String, Object)} - */ - public static T getSelectOneResult(List list) { - if (list == null || list.isEmpty()) { - return null; - } - int size = list.size(); - if (size == 1) { - return list.get(0); - } - throw new TooManyResultsException( - "Expected one result (or null) to be returned by selectOne(), but found: " + size); - } + }