diff --git a/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperX.java b/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperX.java index 545834bd8..a15431efa 100644 --- a/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperX.java +++ b/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperX.java @@ -16,15 +16,8 @@ package org.ballcat.mybatisplus.conditions.query; -import java.lang.reflect.Array; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Predicate; - import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper; +import com.baomidou.mybatisplus.core.conditions.ISqlSegment; import com.baomidou.mybatisplus.core.conditions.SharedString; import com.baomidou.mybatisplus.core.conditions.query.Query; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; @@ -33,14 +26,26 @@ import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import net.sf.jsqlparser.expression.Expression; import org.springframework.util.StringUtils; +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; +import java.util.stream.Collectors; + /** * 增加了一些简单条件的 IfPresent 条件 支持,Collection String Object 等等判断是否为空,或者是否为null * * @author Hccake 2021/1/14 * */ +@SuppressWarnings("java:S107") public class LambdaQueryWrapperX extends AbstractLambdaWrapper> implements Query, T, SFunction> { @@ -232,4 +237,46 @@ public LambdaQueryWrapperX notInIfPresent(SFunction column, Collection< return super.notIn(isPresent(values), column, values); } + // region customize + + protected LambdaQueryWrapperX appendSqlSegment(ISqlSegment segment) { + this.appendSqlSegments(() -> "", segment); + return this; + } + + public LambdaQueryWrapperX jsonContains(SFunction column, Object... values) { + return jsonContains(column, Arrays.asList(values)); + } + + public LambdaQueryWrapperX jsonContains(SFunction column, Collection values) { + this.appendSqlSegment(() -> { + String field = this.columnToString(column); + String keyword = "JSON_CONTAINS"; + String content = values.stream() + .map(i -> this.formatParam(null, i)) + .collect(Collectors.joining(",", "(", ")")); + + return String.format("%s(%s,%s)", keyword, field, content); + }); + return this; + } + + public LambdaQueryWrapperX jsonContainsIfPresent(SFunction column, Object... values) { + return maybeDo(isPresent(values), () -> jsonContains(column, values)); + } + + public LambdaQueryWrapperX jsonContainsIfPresent(SFunction column, Collection values) { + return maybeDo(isPresent(values), () -> jsonContains(column, values)); + } + + public LambdaQueryWrapperX addSql(String sql) { + return this.appendSqlSegment(() -> sql); + } + + public LambdaQueryWrapperX addExpression(Expression expression) { + return this.appendSqlSegment(() -> String.format("(%s)", expression.toString())); + } + + // endregion + } diff --git a/mybatis-plus/ballcat-mybatis-plus/src/test/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperXTest.java b/mybatis-plus/ballcat-mybatis-plus/src/test/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperXTest.java new file mode 100644 index 000000000..36476274c --- /dev/null +++ b/mybatis-plus/ballcat-mybatis-plus/src/test/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperXTest.java @@ -0,0 +1,74 @@ +package org.ballcat.mybatisplus.conditions.query; + +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import lombok.Getter; +import lombok.Setter; +import net.sf.jsqlparser.expression.StringValue; +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; +import net.sf.jsqlparser.expression.operators.relational.EqualsTo; +import net.sf.jsqlparser.schema.Column; +import org.apache.ibatis.builder.MapperBuilderAssistant; +import org.ballcat.mybatisplus.alias.TableAlias; +import org.ballcat.mybatisplus.toolkit.WrappersX; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author lingting 2024-01-30 16:12 + */ +class LambdaQueryWrapperXTest { + + @BeforeEach + void before() { + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), Entity.class); + } + + @Test + void test() { + assertContains("AND a.string='addSql'", wrapper().eq(Entity::getString, "eq1").addSql("a.string='addSql'")); + assertContains(" a.string='addSql'", wrapper().addSql("a.string='addSql'")); + + Column column = new Column("a.string"); + assertContains(" (a.string = 'ex1')", wrapper().addExpression(new EqualsTo(column, new StringValue("ex1")))); + + assertContains("AND (a.string = 'ex1' AND a.string = 'ex2')", + wrapper().eq(Entity::getString, "eq1") + .addExpression(new AndExpression(new EqualsTo(column, new StringValue("ex1")), + new EqualsTo(column, new StringValue("ex2"))))); + + assertContains( + "JSON_CONTAINS(a.list,(#{ew.paramNameValuePairs.MPGENVAL1},#{ew.paramNameValuePairs.MPGENVAL2}))", + wrapper().jsonContainsIfPresent(Entity::getList, Arrays.asList("1", "2"))); + assertContains( + " JSON_CONTAINS(a.list,(#{ew.paramNameValuePairs.MPGENVAL1},#{ew.paramNameValuePairs.MPGENVAL2})) AND", + wrapper().jsonContains(Entity::getList, "1", "2").eq(Entity::getString, "eq1")); + } + + void assertContains(String contains, LambdaQueryWrapperX wrapperX) { + String string = wrapperX.getCustomSqlSegment(); + System.out.println(string); + assertTrue(string.contains(contains)); + } + + LambdaQueryWrapperX wrapper() { + return WrappersX.lambdaAliasQueryX(Entity.class); + } + + @Getter + @Setter + @TableAlias("a") + static class Entity { + + private String string; + + private List list; + + } + +} \ No newline at end of file