mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
SelectFunc 字段别名
This commit is contained in:
parent
0db41de3da
commit
f45a807339
@ -16,7 +16,7 @@ import java.util.Arrays;
|
|||||||
public class FuncArgs {
|
public class FuncArgs {
|
||||||
|
|
||||||
public SelectFunc.Arg[] accept(KProperty<?>... kProperty) {
|
public SelectFunc.Arg[] accept(KProperty<?>... kProperty) {
|
||||||
return Arrays.stream(kProperty).map(i -> new SelectFunc.Arg(KtUtils.ref(i), i.getName())).toArray(SelectFunc.Arg[]::new);
|
return Arrays.stream(kProperty).map(i -> new SelectFunc.Arg(KtUtils.ref(i), i.getName(), false, null)).toArray(SelectFunc.Arg[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -367,10 +367,10 @@ public class MPJLambdaWrapper<T> extends JoinAbstractLambdaWrapper<T, MPJLambdaW
|
|||||||
return String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias();
|
return String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias();
|
||||||
} else {
|
} else {
|
||||||
return String.format(i.getFunc().getSql(), Arrays.stream(args).map(arg -> {
|
return String.format(i.getFunc().getSql(), Arrays.stream(args).map(arg -> {
|
||||||
String prefixByClass = tableList.getPrefixByClass(arg.getClazz());
|
String pf = arg.isHasTableAlias() ? arg.getTableAlias() : tableList.getPrefixByClass(arg.getClazz());
|
||||||
Map<String, SelectCache> mapField = ColumnCache.getMapField(arg.getClazz());
|
Map<String, SelectCache> mapField = ColumnCache.getMapField(arg.getClazz());
|
||||||
SelectCache cache = mapField.get(arg.getProp());
|
SelectCache cache = mapField.get(arg.getProp());
|
||||||
return prefixByClass + StringPool.DOT + cache.getColumn();
|
return pf + StringPool.DOT + cache.getColumn();
|
||||||
}).toArray()) + Constant.AS + i.getAlias();
|
}).toArray()) + Constant.AS + i.getAlias();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -201,28 +201,43 @@ public interface Query<Children> extends Serializable {
|
|||||||
return getChildren();
|
return getChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default <S> Children selectFunc(BaseFuncEnum funcEnum, String index, SFunction<S, ?> column, String alias) {
|
||||||
|
Class<?> aClass = LambdaUtils.getEntityClass(column);
|
||||||
|
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(aClass);
|
||||||
|
getSelectColum().add(new SelectFunc(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias, funcEnum, true, index));
|
||||||
|
return getChildren();
|
||||||
|
}
|
||||||
|
|
||||||
default <S, X> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column, SFunction<X, ?> alias) {
|
default <S, X> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column, SFunction<X, ?> alias) {
|
||||||
return selectFunc(funcEnum, column, LambdaUtils.getName(alias));
|
return selectFunc(funcEnum, column, LambdaUtils.getName(alias));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default <S, X> Children selectFunc(BaseFuncEnum funcEnum, String index, SFunction<S, ?> column, SFunction<X, ?> alias) {
|
||||||
|
return selectFunc(funcEnum, index, column, LambdaUtils.getName(alias));
|
||||||
|
}
|
||||||
|
|
||||||
default <S> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column) {
|
default <S> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column) {
|
||||||
return selectFunc(funcEnum, column, column);
|
return selectFunc(funcEnum, column, column);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default <S> Children selectFunc(BaseFuncEnum funcEnum, String index, SFunction<S, ?> column) {
|
||||||
|
return selectFunc(funcEnum, index, column, column);
|
||||||
|
}
|
||||||
|
|
||||||
default <X> Children selectFunc(BaseFuncEnum funcEnum, Object column, SFunction<X, ?> alias) {
|
default <X> Children selectFunc(BaseFuncEnum funcEnum, Object column, SFunction<X, ?> alias) {
|
||||||
return selectFunc(funcEnum, column, LambdaUtils.getName(alias));
|
return selectFunc(funcEnum, column, LambdaUtils.getName(alias));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
default <X> Children selectFunc(String sql, Function<SelectFunc.Func, SFunction<?, ?>[]> column, String alias) {
|
default <X> Children selectFunc(String sql, Function<SelectFunc.Func, SFunction<?, ?>[]> column, String alias) {
|
||||||
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new SelectFunc.Func()),
|
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(SelectFunc.Func.func),
|
||||||
isHasAlias(), getAlias()));
|
isHasAlias(), getAlias()));
|
||||||
return getChildren();
|
return getChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
default <X, S> Children selectFunc(String sql, Function<SelectFunc.Func, SFunction<?, ?>[]> column, SFunction<S, ?> alias) {
|
default <X, S> Children selectFunc(String sql, Function<SelectFunc.Func, SFunction<?, ?>[]> column, SFunction<S, ?> alias) {
|
||||||
getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql,
|
getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql,
|
||||||
column.apply(new SelectFunc.Func()), isHasAlias(), getAlias()));
|
column.apply(SelectFunc.Func.func), isHasAlias(), getAlias()));
|
||||||
return getChildren();
|
return getChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.toolkit.LambdaUtils;
|
import com.github.yulichang.toolkit.LambdaUtils;
|
||||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||||
import lombok.Data;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.apache.ibatis.type.TypeHandler;
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
@ -71,7 +71,15 @@ public class SelectFunc implements Select {
|
|||||||
public SelectFunc(String alias, Integer index, BaseFuncEnum func, SFunction<?, ?>[] args, boolean hasTableAlias, String tableAlias) {
|
public SelectFunc(String alias, Integer index, BaseFuncEnum func, SFunction<?, ?>[] args, boolean hasTableAlias, String tableAlias) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.column = null;
|
this.column = null;
|
||||||
this.args = Arrays.stream(args).map(i -> new Arg(LambdaUtils.getEntityClass(i), LambdaUtils.getName(i))).toArray(Arg[]::new);
|
this.args = Arrays.stream(args).map(i -> {
|
||||||
|
boolean ins = i instanceof Fun;
|
||||||
|
if (ins) {
|
||||||
|
Fun<?, ?> f = (Fun<?, ?>) i;
|
||||||
|
return new Arg(LambdaUtils.getEntityClass(f.getFunc()), LambdaUtils.getName(f.getFunc()), true, f.getAlias());
|
||||||
|
} else {
|
||||||
|
return new Arg(LambdaUtils.getEntityClass(i), LambdaUtils.getName(i), false, null);
|
||||||
|
}
|
||||||
|
}).toArray(Arg[]::new);
|
||||||
this.cache = null;
|
this.cache = null;
|
||||||
this.hasAlias = true;
|
this.hasAlias = true;
|
||||||
this.alias = alias;
|
this.alias = alias;
|
||||||
@ -149,16 +157,13 @@ public class SelectFunc implements Select {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
public static class Arg {
|
public static class Arg {
|
||||||
public Class<?> clazz;
|
private final Class<?> clazz;
|
||||||
|
private final String prop;
|
||||||
public String prop;
|
private final boolean hasTableAlias;
|
||||||
|
private final String tableAlias;
|
||||||
public Arg(Class<?> clazz, String prop) {
|
|
||||||
this.clazz = clazz;
|
|
||||||
this.prop = prop;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -168,6 +173,8 @@ public class SelectFunc implements Select {
|
|||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public static class Func {
|
public static class Func {
|
||||||
|
|
||||||
|
public static final Func func = new Func();
|
||||||
|
|
||||||
public final <A> SFunction<?, ?>[] accept(SFunction<A, ?> a) {
|
public final <A> SFunction<?, ?>[] accept(SFunction<A, ?> a) {
|
||||||
return new SFunction[]{a};
|
return new SFunction[]{a};
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.github.yulichang.test.join.m;
|
||||||
|
|
||||||
|
import com.github.yulichang.test.join.entity.AddressDO;
|
||||||
|
import com.github.yulichang.test.join.entity.UserDO;
|
||||||
|
import com.github.yulichang.test.join.mapper.UserMapper;
|
||||||
|
import com.github.yulichang.test.util.Reset;
|
||||||
|
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||||
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||||
|
import com.github.yulichang.wrapper.segments.Fun;
|
||||||
|
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 org.springframework.jdbc.BadSqlGrammarException;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class FuncAliasTest {
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
Reset.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void funcAlias() {
|
||||||
|
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, count(ad.id) AS id, count(addr.id) AS id, " +
|
||||||
|
"if(ad.user_id < 5, addr.user_id, ad.user_id + 100) AS id FROM `user` t " +
|
||||||
|
"LEFT JOIN address ad ON (ad.user_id = t.id) LEFT JOIN address addr ON (addr.user_id = t.id) " +
|
||||||
|
"WHERE t.del = false AND ad.del = false AND addr.del = false");
|
||||||
|
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
|
||||||
|
.selectAll(UserDO.class)
|
||||||
|
.selectFunc(() -> "count(%s)", "ad", AddressDO::getId)
|
||||||
|
.selectFunc(() -> "count(%s)", "addr", AddressDO::getId)
|
||||||
|
.selectFunc("if(%s < 5,%s,%s + 100)", arg -> arg.accept(
|
||||||
|
Fun.f("ad", AddressDO::getUserId),
|
||||||
|
Fun.f("addr", AddressDO::getUserId),
|
||||||
|
Fun.f("ad", AddressDO::getUserId)), UserDO::getId)
|
||||||
|
.leftJoin(AddressDO.class, "ad", AddressDO::getUserId, UserDO::getId)
|
||||||
|
.leftJoin(AddressDO.class, "addr", AddressDO::getUserId, UserDO::getId);
|
||||||
|
|
||||||
|
try {
|
||||||
|
userMapper.selectJoinList(UserDO.class, wrapper);
|
||||||
|
} catch (BadSqlGrammarException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user