From 5c57a0aa6e39d4ff8e3fb13a3b202df631e217bd Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Wed, 20 Mar 2024 17:37:28 +0800 Subject: [PATCH] https://github.com/yulichang/mybatis-plus-join/issues/120 --- .../yulichang/method/MPJBaseMethod.java | 4 +-- .../github/yulichang/toolkit/SqlHelper.java | 14 +++------- .../yulichang/wrapper/interfaces/Chain.java | 27 +++++++------------ .../yulichang/test/join/m/TableAliasTest.java | 10 +++++++ pom.xml | 7 +++-- 5 files changed, 27 insertions(+), 35 deletions(-) diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java index c3c475e..8967940 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java @@ -107,7 +107,7 @@ public interface MPJBaseMethod extends Constants { return filedSqlScript; } String newKeyProperty = newPrefix + tableInfo.getKeyProperty(); - String keySqlScript = ConfigProperties.tableAlias + DOT + tableInfo.getKeyColumn() + EQUALS + + String keySqlScript = "${ew.alias}" + DOT + tableInfo.getKeyColumn() + EQUALS + SqlScriptUtils.safeParam(newKeyProperty); return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false) + NEWLINE + filedSqlScript; @@ -116,7 +116,7 @@ public interface MPJBaseMethod extends Constants { default String getSqlWhere(TableFieldInfo tableFieldInfo, final String prefix) { final String newPrefix = prefix == null ? EMPTY : prefix; // 默认: AND column=#{prefix + el} - String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), ConfigProperties.tableAlias + DOT + + String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), "${ew.alias}" + DOT + tableFieldInfo.getColumn(), newPrefix + tableFieldInfo.getEl()); // 查询的时候只判非空 return convertIf(tableFieldInfo, sqlScript, convertIfProperty(newPrefix, tableFieldInfo.getProperty()), diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SqlHelper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SqlHelper.java index 8ddfc3e..1063c25 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SqlHelper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SqlHelper.java @@ -1,6 +1,5 @@ package com.github.yulichang.toolkit; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.github.yulichang.base.MPJBaseMapper; @@ -12,17 +11,10 @@ import java.util.function.Function; * @since 1.4.4 */ @SuppressWarnings("unchecked") -public class SqlHelper { +public final class SqlHelper { - public static R exec(Class entityClass, Function, R> function) { - Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); - Object mapper = SpringContentUtils.getMapper(entityClass); - Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName()); - return function.apply((BaseMapper) mapper); - } - - public static R execJoin(Class entityClass, Function, R> function) { - Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); + public static R exec(Class entityClass, Function, R> function) { + Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); Object mapper = SpringContentUtils.getMapper(entityClass); Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName()); Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> not extends MPJBaseMapper ", entityClass.getSimpleName()); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Chain.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Chain.java index 5c15dc4..0c83bcd 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Chain.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Chain.java @@ -1,10 +1,6 @@ package com.github.yulichang.wrapper.interfaces; -import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.core.toolkit.Assert; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.github.yulichang.base.MPJBaseMapper; import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.toolkit.SqlHelper; @@ -33,10 +29,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default Long count() { - return SqlHelper.exec(getEntityClass(), mapper -> { - Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> is not extends MPJBaseMapper", mapper.getClass().getSimpleName()); - return ((MPJBaseMapper) mapper).selectJoinCount((MPJBaseJoin) this); - }); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinCount((MPJBaseJoin) this)); } /** @@ -47,7 +40,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default T one() { - return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectOne((Wrapper) this)); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinOne(getEntityClass(), (MPJBaseJoin) this)); } /** @@ -58,7 +51,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default R one(Class resultType) { - return SqlHelper.execJoin(getEntityClass(), mapper -> mapper.selectJoinOne(resultType, (MPJBaseJoin) this)); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinOne(resultType, (MPJBaseJoin) this)); } /** @@ -69,8 +62,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default T first() { - List list = list(); - return CollectionUtils.isEmpty(list) ? null : list.get(0); + return list().stream().findFirst().orElse(null); } /** @@ -81,8 +73,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default R first(Class resultType) { - List list = list(resultType); - return CollectionUtils.isEmpty(list) ? null : list.get(0); + return list(resultType).stream().findFirst().orElse(null); } /** @@ -92,7 +83,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default List list() { - return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectList((Wrapper) this)); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinList(getEntityClass(), (MPJBaseJoin) this)); } /** @@ -102,7 +93,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default List list(Class resultType) { - return SqlHelper.execJoin(getEntityClass(), mapper -> mapper.selectJoinList(resultType, (MPJBaseJoin) this)); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinList(resultType, (MPJBaseJoin) this)); } /** @@ -112,7 +103,7 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default

> P page(P page) { - return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectPage(page, (Wrapper) this)); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinPage(page, getEntityClass(), (MPJBaseJoin) this)); } /** @@ -122,6 +113,6 @@ public interface Chain { * JoinWrappers.lambda(User.class)
*/ default > P page(P page, Class resultType) { - return SqlHelper.execJoin(getEntityClass(), mapper -> mapper.selectJoinPage(page, resultType, (MPJBaseJoin) this)); + return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinPage(page, resultType, (MPJBaseJoin) this)); } } diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/TableAliasTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/TableAliasTest.java index d1fb385..56573b1 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/TableAliasTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/TableAliasTest.java @@ -84,4 +84,14 @@ public class TableAliasTest { List dos = userMapper.selectJoinList(UserDO.class, wrapper); dos.forEach(System.out::println); } + + @Test + void tableAlias3() { + ThreadLocalUtils.set("SELECT aaa.id, aaa.pid, aaa.`name`, aaa.`json`, aaa.sex, aaa.head_img, aaa.create_time, " + + "aaa.address_id, aaa.address_id2, aaa.del, aaa.create_by, aaa.update_by FROM `user` aaa WHERE aaa.`name` = ? AND aaa.del = false"); + UserDO userDO = new UserDO(); + userDO.setName("aaa"); + MPJLambdaWrapper wrapper = JoinWrappers.lambda("aaa", userDO); + wrapper.list(); + } } diff --git a/pom.xml b/pom.xml index 882de63..cb2c761 100644 --- a/pom.xml +++ b/pom.xml @@ -62,20 +62,20 @@ org.springframework spring-aop - 5.3.30 + 5.3.32 provided org.jetbrains.kotlin kotlin-stdlib-jdk8 - 1.9.0 + 1.9.22 provided org.springframework.boot spring-boot-configuration-processor true - 2.7.17 + 2.7.18 @@ -87,7 +87,6 @@ flatten-maven-plugin 1.5.0 - true resolveCiFriendliesOnly