yulichang 2023-05-10 10:23:26 +08:00
parent 69fa6a8bc2
commit df769aa863
7 changed files with 73 additions and 14 deletions

View File

@ -49,4 +49,9 @@ public class MybatisPlusJoinProperties {
* 映射查询最大深度 * 映射查询最大深度
*/ */
private int mappingMaxCount = 5; private int mappingMaxCount = 5;
/**
* 子查询别名
*/
private String subQueryAlias = "st";
} }

View File

@ -49,6 +49,12 @@
"defaultValue": 5, "defaultValue": 5,
"type": "java.lang.Integer", "type": "java.lang.Integer",
"description": "映射查询最大深度." "description": "映射查询最大深度."
},
{
"name": "mybatis-plus-join.sub-query-alias",
"defaultValue": "st",
"type": "java.lang.String",
"description": "子查询表别名."
} }
], ],
"hints": [ "hints": [

View File

@ -38,4 +38,8 @@ public class ConfigProperties {
* TableInfo适配器 * TableInfo适配器
*/ */
public static ITableInfoAdapter tableInfoAdapter = AdapterHelper.getTableInfoAdapter(); public static ITableInfoAdapter tableInfoAdapter = AdapterHelper.getTableInfoAdapter();
/**
* 子查询别名
*/
public static String subQueryAlias = "st";
} }

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -13,7 +14,12 @@ import java.util.stream.Collectors;
* @author yulichang * @author yulichang
*/ */
@Data @Data
public class TableList { public class TableList implements Serializable {
/**
* 上级
*/
private TableList parent;
/** /**
* 所有关联的的表 * 所有关联的的表
@ -65,7 +71,7 @@ public class TableList {
} }
Node node = getByClassFirst(clazz); Node node = getByClassFirst(clazz);
if (Objects.isNull(node)) { if (Objects.isNull(node)) {
return alias; return Objects.isNull(parent) ? alias : parent.getPrefixByClassParent(clazz);
} }
return node.isHasAlias() ? node.getAlias() : (node.getAlias() + node.getIndex()); return node.isHasAlias() ? node.getAlias() : (node.getAlias() + node.getIndex());
} }
@ -104,7 +110,7 @@ public class TableList {
} }
} }
} }
return alias; return Objects.isNull(parent) ? alias : parent.getPrefixByClassParent(clazz);
} }
Node node = getByIndex(index); Node node = getByIndex(index);
Node dg = dg(node, node.getClazz()); Node dg = dg(node, node.getClazz());
@ -118,7 +124,7 @@ public class TableList {
if (list.size() == 1) { if (list.size() == 1) {
Node n = list.get(0); Node n = list.get(0);
if (n.getClazz() == node.getClazz()) { if (n.getClazz() == node.getClazz()) {
return alias; return Objects.isNull(parent) ? alias : parent.getPrefixByClassParent(clazz);
} else { } else {
return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex()); return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex());
} }
@ -128,9 +134,9 @@ public class TableList {
return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex()); return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex());
} }
} }
return alias; return Objects.isNull(parent) ? alias : parent.getPrefixByClassParent(clazz);
} else { } else {
return alias; return Objects.isNull(parent) ? alias : parent.getPrefixByClassParent(clazz);
} }
} }
} }
@ -150,7 +156,11 @@ public class TableList {
public String getPrefixByClass(Class<?> clazz) { public String getPrefixByClass(Class<?> clazz) {
Node node = getByClassFirst(clazz); Node node = getByClassFirst(clazz);
if (Objects.isNull(node)) { if (Objects.isNull(node)) {
return alias; if (Objects.equals(rootClass, clazz)) {
return alias;
} else {
return Objects.isNull(parent) ? alias : parent.getPrefixByClassParent(clazz);
}
} else { } else {
return node.hasAlias ? node.getAlias() : (node.getAlias() + node.getIndex()); return node.hasAlias ? node.getAlias() : (node.getAlias() + node.getIndex());
} }
@ -165,6 +175,21 @@ public class TableList {
return node.hasAlias ? node.getAlias() : (node.getAlias() + node.getIndex()); return node.hasAlias ? node.getAlias() : (node.getAlias() + node.getIndex());
} }
public String getPrefixByClassParent(Class<?> clazz) {
if (Objects.equals(clazz, rootClass)) {
return alias;
}
Node node = getByClassFirst(clazz);
if (Objects.isNull(node)) {
if (Objects.nonNull(parent)) {
return parent.getPrefixByClassParent(clazz);
} else {
return alias;
}
}
return node.hasAlias ? node.getAlias() : (node.getAlias() + node.getIndex());
}
private Node getByIndex(int index) { private Node getByIndex(int index) {
return all.stream().filter(i -> i.getIndex() == index).findFirst().orElse(null); return all.stream().filter(i -> i.getIndex() == index).findFirst().orElse(null);
} }
@ -194,7 +219,7 @@ public class TableList {
} }
@Data @Data
public static class Node { public static class Node implements Serializable {
/** /**
* 关联表类型 * 关联表类型

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.TableList; import com.github.yulichang.toolkit.TableList;
@ -183,12 +184,20 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
* 子查询 * 子查询
*/ */
public <E, F> MPJLambdaWrapper<T> selectSub(Class<E> clazz, Consumer<MPJLambdaWrapper<E>> consumer, SFunction<F, ?> alias) { public <E, F> MPJLambdaWrapper<T> selectSub(Class<E> clazz, Consumer<MPJLambdaWrapper<E>> consumer, SFunction<F, ?> alias) {
return selectSub(clazz, ConfigProperties.subQueryAlias, consumer, alias);
}
/**
* 子查询
*/
public <E, F> MPJLambdaWrapper<T> selectSub(Class<E> clazz, String st, Consumer<MPJLambdaWrapper<E>> consumer, SFunction<F, ?> alias) {
MPJLambdaWrapper<E> wrapper = new MPJLambdaWrapper<E>(null, clazz, SharedString.emptyString(), paramNameSeq, paramNameValuePairs, MPJLambdaWrapper<E> wrapper = new MPJLambdaWrapper<E>(null, clazz, SharedString.emptyString(), paramNameSeq, paramNameValuePairs,
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
new TableList(), null, null, null, null) { new TableList(), null, null, null, null) {
}; };
String st = "st";
wrapper.tableList.setAlias(st); wrapper.tableList.setAlias(st);
wrapper.tableList.setRootClass(clazz);
wrapper.tableList.setParent(this.tableList);
wrapper.alias = st; wrapper.alias = st;
wrapper.subTableAlias = st; wrapper.subTableAlias = st;
consumer.accept(wrapper); consumer.accept(wrapper);

View File

@ -5,13 +5,15 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.wrapper.enums.BaseFuncEnum; import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandler;
import java.io.Serializable;
/** /**
* 查询列 * 查询列
* *
* @author yulichang * @author yulichang
* @since 1.3.10 * @since 1.3.10
*/ */
public interface Select { public interface Select extends Serializable {
Class<?> getClazz(); Class<?> getClazz();

View File

@ -1017,15 +1017,23 @@ class LambdaWrapperTest {
*/ */
@Test @Test
void sub() { void sub() {
ThreadLocalUtils.set("SELECT ( SELECT st.id FROM `user` st WHERE st.del=false AND (st.id <= ?) limit 1 ) AS pid FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del=false AND t1.del=false AND (t.id <= ?)"); ThreadLocalUtils.set("SELECT ( SELECT st.id FROM `user` st WHERE st.del=false AND (st.id = t.id) limit 1 ) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del=false AND t1.del=false AND (t.id <= ?)");
MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class) MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
.selectSub(UserDO.class, w -> w.select(UserDO::getId) .selectSub(UserDO.class, w -> w.select(UserDO::getId)
.le(UserDO::getId, 1000) .eq(UserDO::getId, UserDO::getId)
.last("limit 1"), UserDO::getPid) .last("limit 1"), UserDO::getId)
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
.le(UserDO::getId, 100); .le(UserDO::getId, 100);
wrapper.list(); wrapper.list();
System.out.println(1);
ThreadLocalUtils.set("SELECT ( SELECT st.id FROM address st WHERE st.del=false AND (st.id = t.id) limit 1 ) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del=false AND t1.del=false AND (t.id <= ?)");
MPJLambdaWrapper<UserDO> wrapper1 = JoinWrappers.lambda(UserDO.class)
.selectSub(AddressDO.class, w -> w.select(AddressDO::getId)
.eq(AddressDO::getId, UserDO::getId)
.last("limit 1"), UserDO::getId)
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
.le(UserDO::getId, 100);
wrapper1.list();
} }