如何从ibatis中获取SQL
以下是在网上搜到的办法:
package com.seven.dbTools.ibatis;import java.util.HashMap;import java.util.Map;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;import com.ibatis.sqlmap.engine.mapping.sql.Sql;import com.ibatis.sqlmap.engine.scope.RequestScope;import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;public class IbatisUtil extends SqlMapClientDaoSupport {/** * get sql from ibatis :从ibatis中获取sql已经动态参数 * @param sqlid : sqlMap中的id * @param parameterMap : 参数map * @return */public Map<String, Object[]> getSqlAndParametersFromIbatis(String sqlid,Map parameterMap) {String sql = null;SqlMapClientImpl sqlmap = (SqlMapClientImpl) this.getSqlMapClient();MappedStatement stmt = sqlmap.getMappedStatement(sqlid);Sql stmtSql = stmt.getSql();RequestScope requestScope = new RequestScope();requestScope.setStatement(stmt);ParameterMap dynamicParameterMap = stmtSql.getParameterMap(requestScope, parameterMap);Object[] values = null;if (dynamicParameterMap != null) {values = dynamicParameterMap.getParameterObjectValues(requestScope,parameterMap);}sql = stmtSql.getSql(requestScope, parameterMap);Map<String, Object[]> sqlMap = new HashMap<String, Object[]>();sqlMap.put(sql, values);return sqlMap;}}以上代码得到SQL和动态的参数。