Skip to content

Commit

Permalink
✨ WrapperX 允许手动添加sql, 增加json操作支持示例方法
Browse files Browse the repository at this point in the history
  • Loading branch information
lingting committed Jan 30, 2024
1 parent d425614 commit 180720d
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<T> extends AbstractLambdaWrapper<T, LambdaQueryWrapperX<T>>
implements Query<LambdaQueryWrapperX<T>, T, SFunction<T, ?>> {

Expand Down Expand Up @@ -232,4 +237,46 @@ public LambdaQueryWrapperX<T> notInIfPresent(SFunction<T, ?> column, Collection<
return super.notIn(isPresent(values), column, values);
}

// region customize

protected LambdaQueryWrapperX<T> appendSqlSegment(ISqlSegment segment) {
this.appendSqlSegments(() -> "", segment);
return this;
}

public LambdaQueryWrapperX<T> jsonContains(SFunction<T, ?> column, Object... values) {
return jsonContains(column, Arrays.asList(values));
}

public LambdaQueryWrapperX<T> jsonContains(SFunction<T, ?> column, Collection<Object> 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<T> jsonContainsIfPresent(SFunction<T, ?> column, Object... values) {
return maybeDo(isPresent(values), () -> jsonContains(column, values));
}

public LambdaQueryWrapperX<T> jsonContainsIfPresent(SFunction<T, ?> column, Collection<Object> values) {
return maybeDo(isPresent(values), () -> jsonContains(column, values));
}

public LambdaQueryWrapperX<T> addSql(String sql) {
return this.appendSqlSegment(() -> sql);
}

public LambdaQueryWrapperX<T> addExpression(Expression expression) {
return this.appendSqlSegment(() -> String.format("(%s)", expression.toString()));
}

// endregion

}
Original file line number Diff line number Diff line change
@@ -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<Entity> wrapperX) {
String string = wrapperX.getCustomSqlSegment();
System.out.println(string);
assertTrue(string.contains(contains));
}

LambdaQueryWrapperX<Entity> wrapper() {
return WrappersX.lambdaAliasQueryX(Entity.class);
}

@Getter
@Setter
@TableAlias("a")
static class Entity {

private String string;

private List<String> list;

}

}

0 comments on commit 180720d

Please sign in to comment.