1、打印SQL语句到控制台
在开发调试过程中,我遇到在调试 ibatis 执行的 SQL 是否正确时,无法查看 ibatis 是否执行了我想要执行的 SQL 语句,现发现一种解决方案就是在 log4j.xml 配置文件中加入以下代码就可以在控制台中打印出 ibatis 执行的 SQL 语句:
<appender name="IBatis" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-4r [%t] %-5p %c %x - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<logger name="com.ibatis" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG" />
<appender-ref ref="IBatis" />
</logger>
<logger name="java.sql.ResultSet" additivity="true">
<level value="DEBUG" />
<appender-ref ref="IBatis" />
</logger>
2、DEBUG调试如何跟踪
jar包:sourceforge.ibatis-2.3.0-sources.jar
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement
protected void executeQueryWithCallback(RequestScope request, Connection conn, Object parameterObject, Object resultObject, RowHandler rowHandler, int skipResults, int maxResults)
throws SQLException {
.....................省略
errorContext.setMoreInfo("Check the SQL statement.");
//在这行打断点
String sqlString = sql.getSql(request, parameterObject);
/////////////////////省略
}