In my project i have
<bean id="ABCSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ABCDataSource" />
<property name="mapperLocations">
<list>
<value>classpath:com/myco/dao/XYZMapper.xml</value>
</list>
</property>
<bean>
and
log4j.logger.java.sql.Connection=debug, stdout, abclog
log4j.logger.java.sql.PreparedStatement=debug, stdout, abclog
log4j.logger.java.sql=debug, stdout, abclog
log4j.logger.org.mybatis=debug, stdout, abclog
log4j.logger.org.apache.ibatis=debug, stdout, abclog
I dont see the SQL queries when i run the applicartion in log Wanted to know what am i missing
saw this post how to configure log4j for Mybatis to print my SQL suggesting to change mybatis class configuration but not sure how to do with spring SqlSessionFactoryBean
To log SQL queries from a specific mapper in mybatis, add the mapper namespace to the logger level.
To use MyBatis with Spring you need at least two things defined in the Spring application context: an SqlSessionFactory and at least one mapper interface. Notice that the SqlSessionFactory requires a DataSource . This can be any DataSource and should be configured just like any other Spring database connection.
MyBatis is an open source persistence framework which simplifies the implementation of database access in Java applications. It provides the support for custom SQL, stored procedures and different types of mapping relations. Simply put, it's an alternative to JDBC and Hibernate.
MyBatis is one of the most commonly used open-source frameworks for implementing SQL databases access in Java applications.
Another shortcut is to set the debug level of your mybatis mappers to true in your application.properties file:
logging.level.<packageName>.mapper=DEBUG
Example log printed in console or your log file:
2020-03-03 09:41:27.057 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample : ==> Preparing: SELECT count(*) FROM MessageRecivers WHERE ((ReciverId = ? and ReadStats = ? and ReciverMessageFolder <> ?))
2020-03-03 09:41:27.066 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample : ==> Parameters: 58(Long), 0(Short), 1(Short)
2020-03-03 09:41:27.473 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample : <== Total: 1
Quoting from an answer of how to configure logback for Mybatis to print my SQL, I'm not sure if this will work for you in entirety. It provides the Spring config for logging. This approach worked for me.
To log SQL statements for particular mybatis mapper set DEBUG (TRACE to see query parameters and results) level for logger with fully qualified mapper name
<logger name="com.mycompany.myapp.mapper.MyMapper" level="DEBUG"/>
You can log all SQL statements from all mappers if they are in the same package like this
<logger name="com.mycompany.myapp.mapper" level="DEBUG"/>
Give it a go, if the problem is still there. Good luck!
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