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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With