From d56b886db07c73764eeaedd49df26fdee869f60e Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 8 Jun 2023 12:32:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8=20join=20?= =?UTF-8?q?=E8=81=94=E8=A1=A8=E6=9F=A5=E8=AF=A2=E4=B8=80=E4=B8=AA=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=EF=BC=8C=E6=95=B0=E6=8D=AE=E4=B8=A2=E5=A4=B1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 37 +++++++++---------- .../com/mybatisflex/core/util/SqlUtil.java | 24 +++++++++++- 2 files changed, 39 insertions(+), 22 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 1faadd63..d2ee7087 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 @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * 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. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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.mybatisflex.core; @@ -345,8 +345,7 @@ public interface BaseMapper { * @return entity 数据 */ default T selectOneByQuery(QueryWrapper queryWrapper) { - List entities = selectListByQuery(queryWrapper.limit(1)); - return (entities == null || entities.isEmpty()) ? null : entities.get(0); + return SqlUtil.getSelectOneResult(selectListByQuery(queryWrapper)); } @@ -358,8 +357,7 @@ public interface BaseMapper { * @return 数据内容 */ default R selectOneByQueryAs(QueryWrapper queryWrapper, Class asType) { - List entities = selectListByQueryAs(queryWrapper.limit(1), asType); - return (entities == null || entities.isEmpty()) ? null : entities.get(0); + return SqlUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType)); } /** @@ -495,8 +493,7 @@ public interface BaseMapper { * @return 数据量 */ default Object selectObjectByQuery(QueryWrapper queryWrapper) { - List objects = selectObjectListByQuery(queryWrapper.limit(1)); - return objects == null || objects.isEmpty() ? null : objects.get(0); + return SqlUtil.getSelectOneResult(selectObjectListByQuery(queryWrapper)); } 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 68267284..584263ba 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,13 +15,18 @@ */ 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 { - private SqlUtil() {} + private SqlUtil() { + } public static void keepColumnSafely(String column) { if (StringUtil.isBlank(column)) { @@ -78,7 +83,7 @@ public class SqlUtil { } - public static String replaceSqlParams(String sql, Object[] params){ + public static String replaceSqlParams(String sql, Object[] params) { if (params != null && params.length > 0) { for (Object value : params) { // null @@ -108,4 +113,19 @@ 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); + } + } From 21ac56facd5f350a9abdec5903ea29b2ecbee851 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 8 Jun 2023 12:33:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20join=20=E8=81=94=E8=A1=A8=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=AF=B9=E8=B1=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/test/mapper/UserMapperTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java index 55fd2e34..6b936daa 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java @@ -18,7 +18,7 @@ package com.mybatisflex.test.mapper; import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.test.model.UserInfo; -import com.mybatisflex.test.model.UserVO1; +import com.mybatisflex.test.model.UserVO; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -52,10 +52,10 @@ class UserMapperTest { .leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID)) .where(USER.USER_ID.eq(3)); System.out.println(queryWrapper.toSQL()); -// UserVO userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO.class); - UserVO1 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO1.class); -// UserVO2 userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class); -// UserVO3 userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class); + UserVO userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO.class); +// UserVO1 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO1.class); +// UserVO2 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO2.class); +// UserVO3 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO3.class); System.err.println(userVO); } @@ -68,8 +68,8 @@ class UserMapperTest { .leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID)) .where(USER.USER_ID.ge(2)); System.out.println(queryWrapper.toSQL()); -// List userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO.class); - List userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO1.class); + List userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO.class); +// List userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO1.class); // List userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class); // List userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class); userVOS.forEach(System.err::println); From 4bd9ed7ba7ad2781c494d10f8f66ad297b48935f Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 8 Jun 2023 12:33:37 +0800 Subject: [PATCH 3/3] =?UTF-8?q?doc:=20=E5=88=A0=E9=99=A4=E6=B3=A8=E6=84=8F?= =?UTF-8?q?=E4=BA=8B=E9=A1=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/base/field-query.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/docs/zh/base/field-query.md b/docs/zh/base/field-query.md index 980f28e5..e7b1c15d 100644 --- a/docs/zh/base/field-query.md +++ b/docs/zh/base/field-query.md @@ -188,16 +188,3 @@ UserVO{userId='1', userName='admin', roleList=[Role{roleId=1, roleKey='admin', r UserVO{userId='2', userName='ry', roleList=[Role{roleId=2, roleKey='common', roleName='普通角色'}]} UserVO{userId='3', userName='test', roleList=[Role{roleId=1, roleKey='admin', roleName='超级管理员'}, Role{roleId=2, roleKey='common', roleName='普通角色'}]} ``` - -## 特别注意!!! - -使用 `join` 联表查询的时候,只能使用 `selectListXxx` 方法查询 List 数据,不能使用 `selectOneXxx` 方法查询单个数据。 - -```java -default R selectOneByQueryAs(QueryWrapper queryWrapper, Class asType) { - List entities = selectListByQueryAs(queryWrapper.limit(1), asType); - return (entities == null || entities.isEmpty()) ? null : entities.get(0); -} -``` - -因为这个 `selectOneXxx` 方法都是调用的对应的 `selectListXxx` 方法,其中添加了 `queryWrapper.limit(1)` 约束,将数据限制在了一条,而联表查询数据有可能是多条的,这样自动映射就会出现数据丢失。 \ No newline at end of file