jsqlparser使用記錄---生成sql語句
jsqlparser可以從0開始構建sql語句,也可以在原有sql語句的基礎上修改,非常好用
,jar包在上篇文章jsqlparser使用記錄—解析sql語句
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.util.SelectUtils;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main1 {
public static void main(String[] args) {
createSelect();
changeSelect();
createInsert();
createUpdate();
createDelete();
}
public static void createSelect() {
System.out.println("==================================================建立查詢====================================================");
PlainSelect plainSelect = new PlainSelect();
//建立查詢的表
Table table = new Table("table");
table.setAlias(new Alias("t"));
plainSelect.setFromItem(table);
//建立查詢的列
List<String> selectColumnsStr = Arrays.asList("f1", "f2");
List<SelectItem> expressionItemList = selectColumnsStr.stream().map(item -> {
SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
selectExpressionItem.setExpression(new Column(table, item));
return (SelectItem) selectExpressionItem;
}).collect(Collectors.toList());
SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
selectExpressionItem.setAlias(new Alias("count"));
Function function = new Function();
function.setName("count");
ExpressionList expressionList = new ExpressionList();
expressionList.setExpressions(Arrays.asList(new Column(table, "f1")));
function.setParameters(expressionList);
selectExpressionItem.setExpression(function);
expressionItemList.add(selectExpressionItem);
plainSelect.setSelectItems(expressionItemList);
AtomicInteger atomicInteger = new AtomicInteger(1);
List<Join> joinList = Stream.of(new String[2]).map(item -> {
Join join = new Join();
join.setLeft(true);
Table joinTable = new Table();
joinTable.setName("table" + atomicInteger.incrementAndGet());
joinTable.setAlias(new Alias("t" + atomicInteger.get()));
join.setRightItem(joinTable);
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column(table, "f1"));
equalsTo.setRightExpression(new Column(joinTable, "f2"));
join.setOnExpression(equalsTo);
return join;
}).collect(Collectors.toList());
plainSelect.setJoins(joinList);
//條件
EqualsTo leftEqualsTo = new EqualsTo();
leftEqualsTo.setLeftExpression(new Column(table, "f1"));
StringValue stringValue = new StringValue("1222121");
leftEqualsTo.setRightExpression(stringValue);
plainSelect.setWhere(leftEqualsTo);
EqualsTo rightEqualsTo = new EqualsTo();
rightEqualsTo.setLeftExpression(new Column(table, "f2"));
StringValue stringValue1 = new StringValue("122212111111");
rightEqualsTo.setRightExpression(stringValue1);
OrExpression orExpression = new OrExpression(leftEqualsTo, rightEqualsTo);
plainSelect.setWhere(orExpression);
//分組
GroupByElement groupByElement = new GroupByElement();
groupByElement.setGroupByExpressions(Arrays.asList(new Column(table, "f1")));
plainSelect.setGroupByElement(groupByElement);
System.out.println(plainSelect);
//排序
OrderByElement orderByElement = new OrderByElement();
orderByElement.setAsc(true);
orderByElement.setExpression(new Column(table, "f1"));
OrderByElement orderByElement1 = new OrderByElement();
orderByElement1.setAsc(false);
orderByElement1.setExpression(new Column(table, "f2"));
//分頁
Limit limit = new Limit();
limit.setRowCount(new LongValue(2));
limit.setOffset(new LongValue(10));
plainSelect.setLimit(limit);
plainSelect.setOrderByElements(Arrays.asList(orderByElement, orderByElement1));
System.out.println(SQLFormatterUtil.format(plainSelect.toString()));
System.out.println("==================================================建立查詢====================================================");
}
//在原有的sql基礎上改
public static void changeSelect() {
System.out.println("==================================================改變原有查詢====================================================");
CCJSqlParserManager parserManager = new CCJSqlParserManager();
try {
Select select = (Select) (parserManager.parse(new StringReader("select * from table")));
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
//建立查詢的表
Table table = new Table("table");
table.setAlias(new Alias("t"));
plainSelect.setFromItem(table);
//建立查詢的列
List<String> selectColumnsStr = Arrays.asList("f1", "f2");
List<SelectItem> expressionItemList = selectColumnsStr.stream().map(item -> {
SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
selectExpressionItem.setExpression(new Column(table, item));
return (SelectItem) selectExpressionItem;
}).collect(Collectors.toList());
SelectExpressionItem selectExpressionItem = new SelectExpressionItem();
selectExpressionItem.setAlias(new Alias("count"));
Function function = new Function();
function.setName("count");
ExpressionList expressionList = new ExpressionList();
expressionList.setExpressions(Arrays.asList(new Column(table, "f1")));
function.setParameters(expressionList);
selectExpressionItem.setExpression(function);
expressionItemList.add(selectExpressionItem);
plainSelect.setSelectItems(expressionItemList);
AtomicInteger atomicInteger = new AtomicInteger(1);
List<Join> joinList = Stream.of(new String[2]).map(item -> {
Join join = new Join();
join.setLeft(true);
Table joinTable = new Table();
joinTable.setName("table" + atomicInteger.incrementAndGet());
joinTable.setAlias(new Alias("t" + atomicInteger.get()));
join.setRightItem(joinTable);
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column(table, "f1"));
equalsTo.setRightExpression(new Column(joinTable, "f2"));
join.setOnExpression(equalsTo);
return join;
}).collect(Collectors.toList());
plainSelect.setJoins(joinList);
//條件
EqualsTo leftEqualsTo = new EqualsTo();
leftEqualsTo.setLeftExpression(new Column(table, "f1"));
StringValue stringValue = new StringValue("1222121");
leftEqualsTo.setRightExpression(stringValue);
EqualsTo rightEqualsTo = new EqualsTo();
rightEqualsTo.setLeftExpression(new Column(table, "f2"));
StringValue stringValue1 = new StringValue("122212111111");
rightEqualsTo.setRightExpression(stringValue1);
OrExpression orExpression = new OrExpression(leftEqualsTo, rightEqualsTo);
plainSelect.setWhere(orExpression);
//分組
GroupByElement groupByElement = new GroupByElement();
groupByElement.setGroupByExpressions(Arrays.asList(new Column(table, "f1")));
plainSelect.setGroupByElement(groupByElement);
System.out.println(plainSelect);
//排序
OrderByElement orderByElement = new OrderByElement();
orderByElement.setAsc(true);
orderByElement.setExpression(new Column(table, "f1"));
OrderByElement orderByElement1 = new OrderByElement();
orderByElement1.setAsc(false);
orderByElement1.setExpression(new Column(table, "f2"));
//分頁
Limit limit = new Limit();
limit.setRowCount(new LongValue(2));
limit.setOffset(new LongValue(10));
plainSelect.setLimit(limit);
plainSelect.setOrderByElements(Arrays.asList(orderByElement, orderByElement1));
System.out.println(SQLFormatterUtil.format(plainSelect.toString()));
} catch (JSQLParserException e) {
e.printStackTrace();
}
System.out.println("==================================================改變原有查詢====================================================");
}
//建立插入sql語句
public static void createInsert() {
System.out.println("==================================================建立插入語句====================================================");
Insert insert = new Insert();
Table table = new Table();
table.setName("table");
insert.setTable(table);
insert.setColumns(Arrays.asList(
new Column(table, "f1"),
new Column(table, "f2"),
new Column(table, "f3")
));
MultiExpressionList multiExpressionList = new MultiExpressionList();
multiExpressionList.addExpressionList(Arrays.asList(
new StringValue("1"),
new StringValue("2"),
new StringValue("3")
));
insert.setItemsList(multiExpressionList);
System.out.println(insert);
System.out.println("==================================================建立插入語句====================================================");
}
//建立插入sql語句
public static void createUpdate() {
System.out.println("==================================================建立更新語句====================================================");
Update update = new Update();
Table table = new Table();
table.setName("table");
update.setTable(table);
update.setColumns(Arrays.asList(
new Column(table, "f1"),
new Column(table, "f2"),
new Column(table, "f3")
));
update.setExpressions(Arrays.asList(
new StringValue("1"),
new StringValue("6"),
new StringValue("2")
));
//條件
EqualsTo leftEqualsTo = new EqualsTo();
leftEqualsTo.setLeftExpression(new Column(table, "f1"));
StringValue stringValue = new StringValue("1222121");
leftEqualsTo.setRightExpression(stringValue);
EqualsTo rightEqualsTo = new EqualsTo();
rightEqualsTo.setLeftExpression(new Column(table, "f2"));
StringValue stringValue1 = new StringValue("122212111111");
rightEqualsTo.setRightExpression(stringValue1);
OrExpression orExpression = new OrExpression(leftEqualsTo, rightEqualsTo);
update.setWhere(orExpression);
System.out.println(update);
System.out.println("==================================================建立更新語句====================================================");
}
//建立插入sql語句
public static void createDelete() {
System.out.println("==================================================建立刪除語句====================================================");
Delete delete = new Delete();
Table table = new Table();
table.setName("table");
delete.setTable(table);
//條件
EqualsTo leftEqualsTo = new EqualsTo();
leftEqualsTo.setLeftExpression(new Column(table, "f1"));
StringValue stringValue = new StringValue("1222121");
leftEqualsTo.setRightExpression(stringValue);
EqualsTo rightEqualsTo = new EqualsTo();
rightEqualsTo.setLeftExpression(new Column(table, "f2"));
StringValue stringValue1 = new StringValue("122212111111");
rightEqualsTo.setRightExpression(stringValue1);
OrExpression orExpression = new OrExpression(leftEqualsTo, rightEqualsTo);
delete.setWhere(orExpression);
System.out.println(delete);
System.out.println("==================================================建立刪除語句====================================================");
}
}
相關文章
- Java中如何解析SQL語句、格式化SQL語句、生成SQL語句?JavaSQL
- 使用SQL語句去掉重複記錄的兩種方法SQL
- MySQL 記錄所有執行了的 sql 語句MySql
- 使用DataSource-Proxy在Spring Boot中記錄SQL語句 - Vlad MihalceaSpring BootSQL
- 定時生成分月表sql語句SQL
- [20230329]記錄除錯sql語句遇到的問題.txt除錯SQL
- 使用sql語句查詢平均值,使用sql語句查詢資料總條數, not in 篩選語句的使用SQL
- excel表結構生成powerDesigner模型,生成建表語句sqlExcel模型SQL
- SQL語句SQL
- EFCore常規操作生成的SQL語句一覽SQL
- 解析 SQL 的 java 類庫 jsqlparserSQLJavaJS
- Fastapi sqlalchemy DBApi 直接使用sql語句ASTAPISQL
- 【SQL】Oracle SQL join on語句and和where使用區別SQLOracle
- 【SQL】9 SQL INSERT INTO 語句SQL
- 【SQL】10 SQL UPDATE 語句SQL
- 【SQL】11 SQL DELETE 語句SQLdelete
- 自定義註解例項實現SQL語句生成SQL
- SQL SELECT 語句SQL
- sql常用語句SQL
- 4.3.2 關於使用SQL語句建立CDBSQL
- Sql語句實現不同記錄同一屬性列的差值計算SQL
- SQL INSERT INTO 語句詳解:插入新記錄、多行插入和自增欄位SQL
- Hibernate/JPA如何保證不生成多餘的SQL語句?SQL
- Oracle執行語句跟蹤 使用sql trace實現語句追蹤OracleSQL
- 1.3. SQL 語句SQL
- Oracle基本SQL語句OracleSQL
- Sql語句小整理SQL
- SQL語句優化SQL優化
- SQL語句IN的用法SQL
- SQL 語句學習SQL
- [20211009]使用bash計算sql語句的sql_id.txtSQL
- 記一個實用的sql查詢語句SQL
- Go 條件語句 - Go 學習記錄Go
- mysql快速新增百萬條記錄的語句MySql
- IDEA如何快速複製日誌生成sql語句,太妙啦IdeaSQL
- Oracle SQL精妙SQL語句講解OracleSQL
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase
- SQL語言基礎(SELECT語句)SQL