From dbdea7b592af5015d26838a79c2c7d74c149695d Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Wed, 27 Dec 2023 14:55:56 +0800 Subject: [PATCH] fix https://gitee.com/best_handsome/mybatis-plus-join/issues/I8RE0R --- .../yulichang/interceptor/MPJInterceptor.java | 12 +--- mybatis-plus-join-test/pom.xml | 30 ++++++++++ .../test/join/LambdaWrapperTest.java | 6 +- .../yulichang/test/join/m/FieldAliasTest.java | 59 +++++++++++++++++++ 4 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/FieldAliasTest.java diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 3d7bf01..0eea4f3 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.core.toolkit.*; import com.github.yulichang.adapter.base.tookit.VersionUtils; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.interfaces.MPJBaseJoin; -import com.github.yulichang.method.MPJResultType; import com.github.yulichang.query.MPJQueryWrapper; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.MPJReflectionKit; @@ -77,19 +76,14 @@ public class MPJInterceptor implements Interceptor { if (CollectionUtils.isNotEmpty(ms.getResultMaps())) { Class entity = MPJTableMapperHelper.getEntity(getMapper(ms.getId(), ms.getResource())); Class type = ms.getResultMaps().get(0).getType(); - if (Objects.nonNull(entity) && Objects.nonNull(type) && entity == type) { + if (Objects.nonNull(entity) && Objects.nonNull(type) + && !MPJReflectionKit.isPrimitiveOrWrapper(type) && entity == type) { rt = type; } } } if (Objects.nonNull(rt)) { - List list = ms.getResultMaps(); - if (CollectionUtils.isNotEmpty(list)) { - ResultMap resultMap = list.get(0); - if (resultMap.getType() == MPJResultType.class) { - args[0] = getMappedStatement(ms, rt, ew); - } - } + args[0] = getMappedStatement(ms, rt, ew); } } } diff --git a/mybatis-plus-join-test/pom.xml b/mybatis-plus-join-test/pom.xml index fd5218d..7506d26 100644 --- a/mybatis-plus-join-test/pom.xml +++ b/mybatis-plus-join-test/pom.xml @@ -104,4 +104,34 @@ + + + + org.codehaus.mojo + flatten-maven-plugin + 1.5.0 + + + true + resolveCiFriendliesOnly + + + + flatten + process-resources + + flatten + + + + flatten.clean + clean + + clean + + + + + + diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java index c6cbb4e..4cc9630 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java @@ -260,7 +260,6 @@ class LambdaWrapperTest { @Test - @SuppressWarnings("unchecked") void testMSCache() { ThreadLocalUtils.set("SELECT t.id,\n" + " t.pid,\n" + @@ -738,7 +737,9 @@ class LambdaWrapperTest { .lt(UserDO::getId, 8)); assert dos.size() == 4; - ThreadLocalUtils.set("SELECT id,pid,`name`,`json`,sex,head_img,create_time,address_id,address_id2,del,create_by,update_by FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?)", + ThreadLocalUtils.set( + "SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, t.create_time, t.address_id, t.address_id2, t.del, t.create_by, t.update_by FROM `user` t WHERE t.del = false AND (t.id > ? AND t.id < ?)", + "SELECT id, pid, `name`, `json`, sex, head_img, create_time, address_id, address_id2, del, create_by, update_by FROM `user` t WHERE t.del = false AND (t.id > ? AND t.id < ?)", "SELECT * FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?) "); List dos1 = userMapper.selectList(new MPJLambdaWrapper() .gt(UserDO::getId, 3) @@ -993,6 +994,7 @@ class LambdaWrapperTest { void joinOrder() { if (VersionUtils.compare(MybatisPlusVersion.getVersion(), "3.4.3") >= 0) { ThreadLocalUtils.set("SELECT id,user_id,name FROM order_t t ORDER BY t.name DESC", + "SELECT t.id, t.user_id, t.name FROM order_t t ORDER BY t.name DESC", "SELECT id,user_id,name FROM order_t t ORDER BY t.name desc"); } else { ThreadLocalUtils.set("SELECT id,user_id,name FROM order_t t", diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/FieldAliasTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/FieldAliasTest.java new file mode 100644 index 0000000..515f366 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/FieldAliasTest.java @@ -0,0 +1,59 @@ +package com.github.yulichang.test.join.m; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.github.yulichang.test.join.entity.AddressDO; +import com.github.yulichang.test.join.entity.UserDO; +import com.github.yulichang.test.join.entity.UserTenantDO; +import com.github.yulichang.test.join.mapper.UserMapper; +import com.github.yulichang.test.join.mapper.UserTenantMapper; +import com.github.yulichang.test.util.Reset; +import com.github.yulichang.toolkit.JoinWrappers; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SpringBootTest +public class FieldAliasTest { + + @Autowired + private UserMapper userMapper; + + @Autowired + private UserTenantMapper userTenantMapper; + + @BeforeEach + void setUp() { + Reset.reset(); + } + + + @Test + void fieldAlias() { + List list = userMapper.selectList(JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + + list.forEach(System.out::println); + + assert list.get(0).getImg() != null; + + } + + @Test + void fieldAlias1() { + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserTenantDO.class) + .selectAll(UserTenantDO.class) + .leftJoin(UserDO.class, UserDO::getId, UserTenantDO::getUuid); + List list = userTenantMapper.selectList(wrapper); + + new LambdaQueryWrapper().getSqlSelect(); + list.forEach(System.out::println); + System.out.println(wrapper.getSqlSelect()); + assert list.get(0).getIdea() != null; + assert list.get(0).getUuid() != null; + } +}