ibatis打印出底层执行的所有 Sql

在使用ibatis作为持久层 时 我们调试程序如果能够确切的知道到底执行的Sql语句,对于调试程序是非常有帮助的.

在 com.ibatis.sqlmap.engine.scope.RequestScope.java 这个文件加入相应的打印代码即可 

源码如下

/*
 *  Copyright 2004 Clinton Begin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.ibatis.sqlmap.engine.scope;

import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;

import java.sql.ResultSet;
import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Request based implementation of Scope interface
 */
public class RequestScope extends BaseScope {

  private static final Log log=LogFactory.getLog(RequestScope.class);
  // Used by Any
  private SessionScope session;
  private ErrorContext errorContext;
  private MappedStatement statement;
  private ParameterMap parameterMap;
  private ResultMap resultMap;
  private Sql sql;

  // Used by DynamicSql
  private ParameterMap dynamicParameterMap;
  private String dynamicSql;

  // Used by N+1 Select solution
  private ResultSet resultSet;
  private Map uniqueKeys;
  private boolean rowDataFound;
  private String  currentNestedKey;
 
  /**
   * @return Returns the currentNestedKey.
   */
  public String getCurrentNestedKey() {
 return currentNestedKey;
  }

  /**
   * @param currentNestedKey The currentNestedKey to set.
   */
  public void setCurrentNestedKey(String currentNestedKey) {
 this.currentNestedKey = currentNestedKey;
  }

  /**
   * Default constructor
   */
  public RequestScope() {
    errorContext = new ErrorContext();
    reset();
  }

  /**
   * Get the request's error context
   *
   * @return - the request's error context
   */
  public ErrorContext getErrorContext() {
    return errorContext;
  }

  /**
   * Get the session of the request
   *
   * @return - the session
   */
  public SessionScope getSession() {
    return session;
  }

  /**
   * Set the session for the request
   *
   * @param session - the new session
   */
  public void setSession(SessionScope session) {
    this.session = session;
  }

  /**
   * Get the statement for the request
   *
   * @return - the statement
   */
  public MappedStatement getStatement() {
    return statement;
  }

  /**
   * Set the statement for the request
   *
   * @param statement - the statement
   */
  public void setStatement(MappedStatement statement) {
    this.statement = statement;
  }

  /**
   * Get the parameter map for the request
   *
   * @return - the parameter map
   */
  public ParameterMap getParameterMap() {
    return parameterMap;
  }

  /**
   * Set the parameter map for the request
   * @param parameterMap - the new parameter map
   */
  public void setParameterMap(ParameterMap parameterMap) {
    this.parameterMap = parameterMap;
  }

  /**
   * Get the result map for the request
   *
   * @return - the result map
   */
  public ResultMap getResultMap() {
  return resultMap;
  }

  /**
   * Set the result map for the request
   *
   * @param resultMap - the result map
   */
  public void setResultMap(ResultMap resultMap) {
    this.resultMap = resultMap;
  }

  /**
   * Get the SQL for the request
   *
   * @return - the sql
   */
  public Sql getSql() {
   log.info("getSql()           return sql:"+sql);
    return sql;
  }

  /**
   * Set the SQL for the request
   *
   * @param sql - the sql
   */
  public void setSql(Sql sql) {
    this.sql = sql;
  }

  /**
   * Get the dynamic parameter for the request
   *
   * @return - the dynamic parameter
   */
  public ParameterMap getDynamicParameterMap() {
    return dynamicParameterMap;
  }

  /**
   * Set the dynamic parameter for the request
   *
   * @param dynamicParameterMap - the dynamic parameter
   */
  public void setDynamicParameterMap(ParameterMap dynamicParameterMap) {
    this.dynamicParameterMap = dynamicParameterMap;
  }

  /**
   * Get the dynamic SQL for the request
   *
   * @return - the dynamic SQL
   */
  public String getDynamicSql() {
   log.info("getDynamicSql()                 return dynamicSql:"+dynamicSql);
    return dynamicSql;
  }

  /**
   * Set the dynamic SQL for the request
   * @param dynamicSql - the dynamic SQL
   */
  public void setDynamicSql(String dynamicSql) {
    this.dynamicSql = dynamicSql;
  }

  public ResultSet getResultSet() {
    return resultSet;
  }

  public void setResultSet(ResultSet resultSet) {
    this.resultSet = resultSet;
  }

  public Map getUniqueKeys(ResultMap map) {
    if (uniqueKeys == null) {
      return null;
    }
    return (Map)uniqueKeys.get(map);
  }
 
  public void setUniqueKeys(ResultMap map, Map keys) {
    if (uniqueKeys == null) {
      uniqueKeys = new HashMap();
    }
    this.uniqueKeys.put(map, keys);
  }

  public boolean isRowDataFound() {
    return rowDataFound;
  }

  public void setRowDataFound(boolean rowDataFound) {
    this.rowDataFound = rowDataFound;
  }

  public void reset() {
    super.reset();
    errorContext.reset();
    session = null;
    statement = null;
    parameterMap = null;
    resultMap = null;
    sql = null;
    dynamicParameterMap = null;
    dynamicSql = null;
    resultSet = null;
    uniqueKeys = null;
    rowDataFound = true;
  }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值