Search code examples
javajakarta-eeormmybatisibatis

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


Solution

  • 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>