在Druid中,可以使用ExportParameterVisitor参数化SQL,并且获得参数列表。

    1. String sql = "select * from t where id = 3 and name = 'abc'";
    2.  
    3. List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    4.  
    5. StringBuilder out = new StringBuilder();
    6. ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    7. for (SQLStatement stmt : stmtList) {
    8. stmt.accept(visitor);
    9. }
    10.  
    11. String paramteredSql = out.toString();
    12. System.out.println(paramteredSql);
    13.  
    14. List<Object> paramters = visitor.getParameters(); // [3, "abc"]
    15. for (Object param : paramters) {
    16. System.out.println(param);
    17. }

    其中输出的paramteredSql变量数值如下:

    1. SELECT *
    2. FROM t
    3. WHERE id = ?
    4. AND name = ?