ibatis动态获取执行SQL和参数

动态SQL的最好做法是在日志中记录一下执行的SQL,如下为在执行过程中获取SQL和参数的方案

    /**
     * 查询总条数
     */
    public int queryCountByScriptSQL(Map parmMap)
            throws LasBusinessException {
        try {
            // debug("queryCountByScriptSQL in");
            List res = getSqlMapClientTemplate().queryForList(
                    "valAdded.queryCountByScriptSQL", parmMap);
            SqlMapExecutorDelegate delegate = ((ExtendedSqlMapClient) (getSqlMapClientTemplate()
                    .getSqlMapClient())).getDelegate();
            MappedStatement ms = delegate
                    .getMappedStatement("valAdded.queryCountByScriptSQL");
            Sql sql = ms.getSql();
            RequestScope requestScope = new RequestScope();
            requestScope.setStatement(ms);
            String sqlStr = sql.getSql(requestScope, parmMap);
            // ParameterMap p = sql.getParameterMap(requestScope, parmMap);
            Object[] sqlParam = sql.getParameterMap(requestScope, parmMap)
                    .getParameterObjectValues(requestScope, parmMap);
            System.out.println("----------->" + sqlStr);
            for (int i = 0; i < sqlParam.length; i++) {
                System.out.println("----参数[" + (i + 1) + "]------->" + sqlParam[i]);
            }
            // System.out.println("----------->"+p.toString());
            return Integer.parseInt(res.get(0).toString());
        } catch (DataAccessException e) {
            if (e.getErrorCode().equals(DB_LOCK_NOWAIT_CODE)) {
                throw new LasBusinessException(DB_LOCK_NOWAIT_MESSAGE, e);
            } else {
                throw new LasAppException("ElisLas系统异常", e);
            }
        } catch (Exception e) {
            throw new LasAppException("ElisLas系统异常", e);
        }
    }

你可能感兴趣的:(Web)