mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
8cf32c2381
@ -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='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='普通角色'}]}
|
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> R selectOneByQueryAs(QueryWrapper queryWrapper, Class<R> asType) {
|
|
||||||
List<R> entities = selectListByQueryAs(queryWrapper.limit(1), asType);
|
|
||||||
return (entities == null || entities.isEmpty()) ? null : entities.get(0);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
因为这个 `selectOneXxx` 方法都是调用的对应的 `selectListXxx` 方法,其中添加了 `queryWrapper.limit(1)` 约束,将数据限制在了一条,而联表查询数据有可能是多条的,这样自动映射就会出现数据丢失。
|
|
||||||
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
* <p>
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* <p>
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.core;
|
package com.mybatisflex.core;
|
||||||
|
|
||||||
@ -346,8 +346,7 @@ public interface BaseMapper<T> {
|
|||||||
* @return entity 数据
|
* @return entity 数据
|
||||||
*/
|
*/
|
||||||
default T selectOneByQuery(QueryWrapper queryWrapper) {
|
default T selectOneByQuery(QueryWrapper queryWrapper) {
|
||||||
List<T> entities = selectListByQuery(queryWrapper.limit(1));
|
return SqlUtil.getSelectOneResult(selectListByQuery(queryWrapper));
|
||||||
return (entities == null || entities.isEmpty()) ? null : entities.get(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -359,8 +358,7 @@ public interface BaseMapper<T> {
|
|||||||
* @return 数据内容
|
* @return 数据内容
|
||||||
*/
|
*/
|
||||||
default <R> R selectOneByQueryAs(QueryWrapper queryWrapper, Class<R> asType) {
|
default <R> R selectOneByQueryAs(QueryWrapper queryWrapper, Class<R> asType) {
|
||||||
List<R> entities = selectListByQueryAs(queryWrapper.limit(1), asType);
|
return SqlUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType));
|
||||||
return (entities == null || entities.isEmpty()) ? null : entities.get(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -507,8 +505,7 @@ public interface BaseMapper<T> {
|
|||||||
* @return 数据量
|
* @return 数据量
|
||||||
*/
|
*/
|
||||||
default Object selectObjectByQuery(QueryWrapper queryWrapper) {
|
default Object selectObjectByQuery(QueryWrapper queryWrapper) {
|
||||||
List<Object> objects = selectObjectListByQuery(queryWrapper.limit(1));
|
return SqlUtil.getSelectOneResult(selectObjectListByQuery(queryWrapper));
|
||||||
return objects == null || objects.isEmpty() ? null : objects.get(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,13 +15,18 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.util;
|
package com.mybatisflex.core.util;
|
||||||
|
|
||||||
|
import org.apache.ibatis.exceptions.TooManyResultsException;
|
||||||
|
import org.apache.ibatis.session.defaults.DefaultSqlSession;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
public class SqlUtil {
|
public class SqlUtil {
|
||||||
|
|
||||||
private SqlUtil() {}
|
private SqlUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
public static void keepColumnSafely(String column) {
|
public static void keepColumnSafely(String column) {
|
||||||
if (StringUtil.isBlank(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) {
|
if (params != null && params.length > 0) {
|
||||||
for (Object value : params) {
|
for (Object value : params) {
|
||||||
// null
|
// null
|
||||||
@ -108,4 +113,19 @@ public class SqlUtil {
|
|||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搬运加改造 {@link DefaultSqlSession#selectOne(String, Object)}
|
||||||
|
*/
|
||||||
|
public static <T> T getSelectOneResult(List<T> 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ package com.mybatisflex.test.mapper;
|
|||||||
|
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.test.model.UserInfo;
|
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.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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))
|
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID))
|
||||||
.where(USER.USER_ID.eq(3));
|
.where(USER.USER_ID.eq(3));
|
||||||
System.out.println(queryWrapper.toSQL());
|
System.out.println(queryWrapper.toSQL());
|
||||||
// UserVO userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO.class);
|
UserVO userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO.class);
|
||||||
UserVO1 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO1.class);
|
// UserVO1 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO1.class);
|
||||||
// UserVO2 userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class);
|
// UserVO2 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO2.class);
|
||||||
// UserVO3 userVO = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class);
|
// UserVO3 userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO3.class);
|
||||||
System.err.println(userVO);
|
System.err.println(userVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,8 +68,8 @@ class UserMapperTest {
|
|||||||
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID))
|
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID))
|
||||||
.where(USER.USER_ID.ge(2));
|
.where(USER.USER_ID.ge(2));
|
||||||
System.out.println(queryWrapper.toSQL());
|
System.out.println(queryWrapper.toSQL());
|
||||||
// List<UserVO> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO.class);
|
List<UserVO> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO.class);
|
||||||
List<UserVO1> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO1.class);
|
// List<UserVO1> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO1.class);
|
||||||
// List<UserVO2> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class);
|
// List<UserVO2> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO2.class);
|
||||||
// List<UserVO3> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class);
|
// List<UserVO3> userVOS = userMapper.selectListByQueryAs(queryWrapper, UserVO3.class);
|
||||||
userVOS.forEach(System.err::println);
|
userVOS.forEach(System.err::println);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user