# 示例代码
【pom.xml】
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.4.3.1</version>
</dependency>
【MyJSqlParserTest.java】
package com.chz.myJSqlParser;
public class MyJSqlParserTest {
public static void main(String[] args) throws JSQLParserException
{
Select stmt = (Select) CCJSqlParserUtil.parse(" SELECT col1 AS a, col2 AS b" +
" FROM table_name " +
" WHERE col_1 = 10" +
" AND col_2 = 20");
System.out.println("before ::: " + stmt.toString());
PlainSelect plainSelect = (PlainSelect)stmt.getSelectBody();
Expression whereExpression = plainSelect.getWhere();
whereExpression = andTenantIdExpression(whereExpression, 1L);
whereExpression = andDeletedExpression(whereExpression, true);
plainSelect.setWhere(whereExpression);
System.out.println("after ::: " + stmt);
}
private static BinaryExpression andTenantIdExpression(Expression where, Long tenantId) {
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("tenant_id"));
equalsTo.setRightExpression(new LongValue(tenantId));
if (null != where) {
if (where instanceof OrExpression) {
return new AndExpression(equalsTo, new Parenthesis(where));
} else {
return new AndExpression(equalsTo, where);
}
}
return equalsTo;
}
private static Expression andDeletedExpression(Expression where, Boolean deleted)
{
IsBooleanExpression isBooleanExpression = new IsBooleanExpression();
isBooleanExpression.setLeftExpression(new Column("deleted"));
isBooleanExpression.setIsTrue(deleted);
if (null != where) {
if (where instanceof OrExpression) {
return new AndExpression(isBooleanExpression, new Parenthesis(where));
} else {
return new AndExpression(isBooleanExpression, where);
}
}
return isBooleanExpression;
}
}
运行【MyJSqlParserTest】
# 其它参考资料:
https://juejin.cn/post/7072190387342802981