本文主要是介绍java:使用JSqlParser给sql语句增加tenant_id和deleted条件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# 示例代码
【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
这篇关于java:使用JSqlParser给sql语句增加tenant_id和deleted条件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!