需求说明:从sql语句中截取where条件后的语句
示例:从下面语句中只截取 a.name="xx" and a.age="15";
select * from a where a.name="xx" and a.age="15";
解决办法:用到的是Jsqlparser
1.首先引入对应的依赖
2.直接代码
/**
* 获取SQL中的where后面的条件
*
* @param sql
* @return
* @throws JSQLParserException
*/
public String getWhere(String sql) throws JSQLParserException {
String columnNames = null;
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(sql);
Statement statement = CCJSqlParserUtil.parse(new StringReader(stringBuffer.toString()));
if (statement instanceof Select) {
Select istatement = (Select) statement;
Expression where = ((PlainSelect) istatement.getSelectBody()).getWhere();
columnNames=where.toString();
}
return columnNames;
}