Skip to content

ExportParameterVisitor_demo_cn

高铁 edited this page Jul 29, 2016 · 1 revision

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

String sql = "select * from t where id = 3 and name = 'abc'";

List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);

StringBuilder out = new StringBuilder();
ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
for (SQLStatement stmt : stmtList) {
    stmt.accept(visitor);
}

String paramteredSql = out.toString();
System.out.println(paramteredSql);

List<Object> paramters = visitor.getParameters(); // [3, "abc"]
for (Object param : paramters) {
    System.out.println(param);
}

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

SELECT *
FROM t
WHERE id = ?
	AND name = ?
Clone this wiki locally