0、官网
1、引入依赖
<dependencies>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
2、新增
3、删除
@Test
public void buildDelete(){
Delete delete = new Delete();
delete.setTable(new Table("t_user"));
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("username"));
equalsTo.setRightExpression(new StringValue("张三"));
delete.setWhere(equalsTo);
System.out.println(delete);
}
4、查询
Select select = SelectUtils.buildSelectFromTable(new Table("t_user"));
PlainSelect plainSelect = select.getPlainSelect();
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("username"));
equalsTo.setRightExpression(new StringValue("李四"));
plainSelect.setWhere(equalsTo);
LikeExpression likeExpression = new LikeExpression();
likeExpression.setLeftExpression(new Column("username"));
likeExpression.setRightExpression(new StringValue("张%"));
plainSelect.setWhere(likeExpression);
AndExpression andExpression = new AndExpression();
andExpression.setLeftExpression(equalsTo);
andExpression.setRightExpression(likeExpression);
plainSelect.setWhere(andExpression);
Between between = new Between();
between.setBetweenExpressionStart(new LongValue(18));
between.setBetweenExpressionEnd(new LongValue(30));
between.setLeftExpression(new Column("age"));
OrExpression orExpression = new OrExpression();
orExpression.setLeftExpression(andExpression);
orExpression.setRightExpression(between);
plainSelect.setWhere(orExpression);
System.out.println(plainSelect);
5、修改
@Test
public void buildUpdate(){
Update update = new Update();
update.setTable(new Table("t_user"));
update.addUpdateSet(new Column("username"), new StringValue("张三"));
update.addUpdateSet(new Column("age"), new StringValue("18"));
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("username"));
equalsTo.setRightExpression(new StringValue("李四"));
update.setWhere(equalsTo);
System.out.println(update);
}