Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iBatis, spring, how to log the sql that is executed?

Tags:

I am using iBatis with spring framework. I want to log the sql that iBatis executes when I say something like

Employee e = (Employee) getSqlMapClientTemplate().queryForObject("emp_sql", emp); 

The above line will look for "emp_sql" id in the ibatis sql file that I have. And then run the query corresponding to "emp_sql". I want to log this query.

I have the following log4j xml properties file.

<appender name="sqlLogAppender" class="org.apache.log4j.DailyRollingFileAppender">     <param name="file" value="/disk1/logs/sql.log"/>     <param name="datePattern" value="'-'yyyy-MM-dd'.txt'"/>     <layout class="org.apache.log4j.PatternLayout">         <param name="ConversionPattern" value="%m %n"/>     </layout>    <filter class="org.apache.log4j.varia.LevelRangeFilter">         <param name="LevelMin" value="DEBUG"/>     </filter> </appender>  <logger name="log4j.logger.com.ibatis">     <level value="DEBUG"/>     <appender-ref ref="sqlLogAppender"/> </logger>   <logger name="log4j.logger.java.sql.Connection">     <level value="DEBUG"/>     <appender-ref ref="sqlLogAppender"/> </logger>  <logger name="log4j.logger.java.sql.PreparedStatement">     <level value="DEBUG"/>     <appender-ref ref="sqlLogAppender"/> </logger> 

I still cannot get the sql that the ibatis executed. Is there something wrong with the configuration? Should I just say

<appender name="sqlLogAppender" class="org.apache.log4j.DailyRollingFileAppender">     <param name="file" value="/disk1/logs/sql.log"/>     <param name="datePattern" value="'-'yyyy-MM-dd'.txt'"/>     <layout class="org.apache.log4j.PatternLayout">         <param name="ConversionPattern" value="%m %n"/>     </layout>    <filter class="org.apache.log4j.varia.LevelRangeFilter">         <param name="LevelMin" value="DEBUG"/>     </filter> </appender>  <logger name="log4j.logger.java.sql">     <level value="DEBUG"/>     <appender-ref ref="sqlLogAppender"/> </logger> 

Do I have to use p6spy or something else? Or is there something that I can do in the log4j configuration to get the iBatis sql logs?

like image 433
aadidasu Avatar asked Nov 02 '10 22:11

aadidasu


People also ask

How do I log into MyBatis queries?

To log SQL queries from a specific mapper in mybatis, add the mapper namespace to the logger level. Originally published at cheeky-chinnu.blogspot.com on January 8, 2019.

What is resultMap in MyBatis?

The resultMap element is the most important and powerful element in MyBatis. It's what allows you to do away with 90% of the code that JDBC requires to retrieve data from ResultSet s, and in some cases allows you to do things that JDBC does not even support.

Does MyBatis use prepared statement?

The parameter object has a property called sql . That SQL string will be prepared as a JDBC prepared statement in MyBatis. The SQL string also references a property called id . That property - from the same parameter object - will be used as the value of the prepared statement parameter.


1 Answers

Add the following to your log4j configuration (uncomment what you want to see).

 # SqlMap logging configuration. #log4j.logger.com.ibatis=DEBUG #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG #log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG #log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG #log4j.logger.java.sql=DEBUG #log4j.logger.java.sql.Connection=DEBUG #log4j.logger.java.sql.Statement=DEBUG #log4j.logger.java.sql.PreparedStatement=DEBUG #log4j.logger.java.sql.ResultSet=DEBUG 
like image 158
DwB Avatar answered Sep 19 '22 20:09

DwB