<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
@org.junit.Test
public void test() {
String sql = "SELECT ID,NAME FROM USER WHERE DEL = '0' ORDER BY CREATE_TIME";
final DbType dbType = JdbcConstants.MYSQL;
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
final String format = SQLUtils.format(sql, dbType, SQLUtils.DEFAULT_FORMAT_OPTION);
System.out.println(format);
System.out.println("=====================================================");
for (SQLStatement statement : statementList) {
if (statement instanceof SQLSelectStatement) {
final SQLSelectStatement selectStatement = (SQLSelectStatement) statement;
final SQLSelect select = selectStatement.getSelect();
// UNION查询取第一个Select子句
final SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
System.out.println("查询列:" + queryBlock.getSelectList());
System.out.println("表名:" + queryBlock.getFrom());
System.out.println("查询条件:" + queryBlock.getWhere());
}
}
}
参考: