-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ WrapperX 允许手动添加sql, 增加json操作支持示例方法
- Loading branch information
Showing
2 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...-plus/src/test/java/org/ballcat/mybatisplus/conditions/query/LambdaQueryWrapperXTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
} |