Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the sql query executing in mybatis mapper xml to the console

Suppose I am executing below query which is there in my mapper xml:

<select id="getData" parameterType="java.util.HashMap" resultType="java.util.LinkedHashMap">

select * from emp where empId=#{empId}          
    </select>

In the above xml empId is the dynamic value that returns the value from the key of the HashMap that is passed as parameter in above mapper xml in Mybatis.

Is there any way to print the sql with the passed param to the console when the above select query mapped to method getData is ran.

for example I pass data empId =1 I want in console : select * from emp where empId=1

like image 602
SiddP Avatar asked Jan 08 '16 11:01

SiddP


People also ask

How do I get my ResultSet in MyBatis?

Resultset resultset = mybatisMapper. getResults();

Does MyBatis use prepared statement?

MyBatis does four main things: It executes SQL safely and abstracts away all the intricacies of JDBC. It maps parameter objects to JDBC prepared statement parameters. It maps rows in JDBC result sets to objects.


1 Answers

add log4j.properties

log4j.logger.yourmapperpackage=DEBUG

and the sql debug is like this:

2016-01-09 20:51:10,621 DEBUG [com.xxxMapper.insert] - <==>  Preparing: INSERT INTO video_info (content, id, create_time, title, media_url, zhan_count, cover_url) VALUES (?, ?, ?, ?, ?, ?, ?) >
2016-01-09 20:51:10,627 DEBUG [com.xxxMapper.insert] - <==> Parameters: vcontent(String), 0(Long), 2016-01-09 20:51:10.616(Timestamp), vtitle(String), null, 1(Integer), null>
2016-01-09 20:51:10,628 DEBUG [com.xxxMapper.insert] - <<==    Updates: 1>
like image 76
Persia Avatar answered Sep 22 '22 18:09

Persia