mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
feat func(condition, consumer , else consumer);
This commit is contained in:
parent
a3340741f1
commit
de49069ccc
@ -14,13 +14,13 @@ import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.apt.Column;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.extension.apt.interfaces.CompareIfExists;
|
||||
import com.github.yulichang.extension.apt.interfaces.Func;
|
||||
import com.github.yulichang.extension.apt.interfaces.OnCompare;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.toolkit.MPJSqlInjectionUtils;
|
||||
import com.github.yulichang.toolkit.Ref;
|
||||
import com.github.yulichang.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.extension.apt.interfaces.CompareIfExists;
|
||||
import com.github.yulichang.extension.apt.interfaces.Func;
|
||||
import com.github.yulichang.extension.apt.interfaces.OnCompare;
|
||||
import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.*;
|
||||
import com.github.yulichang.wrapper.segments.AptConsumer;
|
||||
@ -28,7 +28,10 @@ import lombok.Getter;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.*;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
|
||||
@ -477,8 +480,14 @@ public abstract class JoinAbstractWrapper<T, Children extends JoinAbstractWrappe
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return maybeDo(condition, () -> consumer.accept(typedThis));
|
||||
public Children func(boolean condition, Consumer<Children> consumer, Consumer<Children> consumerElse) {
|
||||
if (condition) {
|
||||
consumer.accept(typedThis);
|
||||
}
|
||||
if (!condition && Objects.nonNull(consumerElse)) {
|
||||
consumerElse.accept(typedThis);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -183,5 +183,9 @@ public interface Func<Children> extends FuncLambda<Children> {
|
||||
* @return children
|
||||
* @since 3.3.1
|
||||
*/
|
||||
Children func(boolean condition, Consumer<Children> consumer);
|
||||
default Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return func(condition, consumer, null);
|
||||
}
|
||||
|
||||
Children func(boolean condition, Consumer<Children> consumer, Consumer<Children> consumerElse);
|
||||
}
|
||||
|
@ -499,8 +499,14 @@ public abstract class KtAbstractWrapper<T, Children extends KtAbstractWrapper<T,
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return maybeDo(condition, () -> consumer.accept(typedThis));
|
||||
public Children func(boolean condition, Consumer<Children> consumer, Consumer<Children> consumerElse) {
|
||||
if (condition) {
|
||||
consumer.accept(typedThis);
|
||||
}
|
||||
if (!condition && Objects.nonNull(consumerElse)) {
|
||||
consumerElse.accept(typedThis);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -483,5 +483,9 @@ public interface Func<Children> extends Serializable {
|
||||
* @return children
|
||||
* @since 3.3.1
|
||||
*/
|
||||
Children func(boolean condition, Consumer<Children> consumer);
|
||||
default Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return func(condition, consumer, null);
|
||||
}
|
||||
|
||||
Children func(boolean condition, Consumer<Children> consumer, Consumer<Children> consumerElse);
|
||||
}
|
||||
|
@ -623,8 +623,14 @@ public abstract class JoinAbstractWrapper<T, Children extends JoinAbstractWrappe
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return maybeDo(condition, () -> consumer.accept(typedThis));
|
||||
public Children func(boolean condition, Consumer<Children> consumer, Consumer<Children> consumerElse) {
|
||||
if (condition) {
|
||||
consumer.accept(typedThis);
|
||||
}
|
||||
if (!condition && Objects.nonNull(consumerElse)) {
|
||||
consumerElse.accept(typedThis);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -526,5 +526,9 @@ public interface Func<Children> extends Serializable {
|
||||
* @return children
|
||||
* @since 3.3.1
|
||||
*/
|
||||
Children func(boolean condition, Consumer<Children> consumer);
|
||||
default Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return func(condition, consumer, null);
|
||||
}
|
||||
|
||||
Children func(boolean condition, Consumer<Children> consumer, Consumer<Children> consumerElse);
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
package com.github.yulichang.test.join.unit;
|
||||
|
||||
import com.github.yulichang.test.join.entity.UserDO;
|
||||
import com.github.yulichang.test.util.Reset;
|
||||
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||
import com.github.yulichang.toolkit.JoinWrappers;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class FuncTest {
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
Reset.reset();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void func() {
|
||||
ThreadLocalUtils.set("SELECT t.`json` FROM `user` t WHERE t.del = false");
|
||||
List<UserDO> list = JoinWrappers.lambda(UserDO.class)
|
||||
.func(false, w -> w.select(UserDO::getId))
|
||||
.select(UserDO::getJson)
|
||||
.list();
|
||||
assert list.get(0).getId() == null && list.get(0).getJson() != null;
|
||||
list.forEach(System.out::println);
|
||||
|
||||
ThreadLocalUtils.set("SELECT t.id FROM `user` t WHERE t.del = false");
|
||||
List<UserDO> list1 = JoinWrappers.lambda(UserDO.class)
|
||||
.func(true, w -> w.select(UserDO::getId), w -> w.select(UserDO::getName))
|
||||
.list();
|
||||
assert list1.get(0).getId() != null && list1.get(0).getName() == null;
|
||||
list1.forEach(System.out::println);
|
||||
|
||||
ThreadLocalUtils.set("SELECT t.`name` FROM `user` t WHERE t.del = false");
|
||||
List<UserDO> list2 = JoinWrappers.lambda(UserDO.class)
|
||||
.func(false, w -> w.select(UserDO::getId), w -> w.select(UserDO::getName))
|
||||
.list();
|
||||
assert list2.get(0).getName() != null && list2.get(0).getId() == null;
|
||||
list2.forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user