IBatis如何获取解析后的SQL语句和占位符参数
?获取IBatis中指定SQLID的SQL解析结果(根据传入参数解析后的SQL语句和占位符参数数组)
?
SqlMapClient本身是没有方法获取SQL的解析结果的,必须将SqlMapClient对象强制转换成SqlMapClientImpl类型,然后传入SQLID和参数就可以获取到了,具体如下代码(sqlId为要解析的SQL对应的ID,params为传入的参数):
?
?
SqlMapClientImpl sci = (SqlMapClientImpl)this.sqlMapClient;MappedStatement ms = sci.getMappedStatement(sqlId);Sql sql = ms.getSql(); SessionScope sessionScope = new SessionScope(); sessionScope.incrementRequestStackDepth(); StatementScope statementScope = new StatementScope(sessionScope); ms.initRequest(statementScope); ms.getCacheKey(statementScope, params); String sqlString = sql.getSql(statementScope, params); Object[] sqlParam = sql.getParameterMap(statementScope, params).getParameterObjectValues(statementScope, params);