SQL console

Mybatis logs SQL statement and actual parameters at debug level as the default. However, SQL statement and parameters are logged at different lines, but we may want to see something that can be executed directly. This is how the original log text may look like:

15:47:50.561 DEBUG o.m.s.t.SpringManagedTransaction         : JDBC Connection [HikariProxyConnection@985487390 wrapping com.mysql.jdbc.JDBC4Connection@182b435b] will not be managed by Spring
15:47:50.569 DEBUG c.s.a.PersonMapper.insertPerson          : ==>  Preparing: INSERT into person (id, name) value (?, ?) 
15:47:50.603 DEBUG c.s.a.PersonMapper.insertPerson          : ==> Parameters: 1528703269807(Long), alex(String)
15:47:50.605 DEBUG c.s.a.PersonMapper.insertPerson          : <==    Updates: 1
15:47:50.605 DEBUG org.mybatis.spring.SqlSessionUtils       : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4648ce9]
15:47:50.605 DEBUG o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
15:47:50.608 DEBUG org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
15:47:50.608 DEBUG org.mybatis.spring.SqlSessionUtils       : SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@33d05366] was not registered for synchronization because synchronization is not active
15:47:50.610 DEBUG o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
15:47:50.610 DEBUG o.m.s.t.SpringManagedTransaction         : JDBC Connection [HikariProxyConnection@2111247080 wrapping com.mysql.jdbc.JDBC4Connection@182b435b] will not be managed by Spring
15:47:50.610 DEBUG c.s.a.P.selectPersonByIdAndName          : ==>  Preparing: select * from person where id = ? and name = ? 
15:47:50.611 DEBUG c.s.a.P.selectPersonByIdAndName          : ==> Parameters: 10(Integer), alex(String)
15:47:50.627 DEBUG c.s.a.P.selectPersonByIdAndName          : <==      Total: 0
15:47:50.627 DEBUG org.mybatis.spring.SqlSessionUtils       : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@33d05366]
15:47:50.628 DEBUG o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
15:47:50.628 DEBUG org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession

Mybatis plugin gives you a simple solution to solve this problem. The plugin will monitor console view when you run any application and analyze the output, an external tab will be added next to console view to show SQL statement concatenated with actual parameters. This is how it looks like:

concatenated sql

Concatenate manually

You can also select some Mybatis log text (You can select as many as you like) and concatenate the SQL contained in the selected text manually or you can copy the concatenated SQL directly to clipboard:

concatenate sql manually

Run SQL

You can run concatenated SQL directly in the SQL tab:

run sql

Go to mapper method

It's also possible to navigate to mapper method that produces the SQL directly from the SQL tab:

go to mapper method

The mapper id may look like 'c.s.PersonMapper.selectPerson', the 'c.s.PersonMapper' part is the mapper interface name which is an abbr name from the log, while the ‘selectPerson’ is the mapper method. The plugin uses only the mapper method part 'selectPerson' to search and navigate to the corresponding mapper method.

Settings

SQL console is disabled as default, you can enable it via IDEA settings → Languages & Frameworks → Mybatis:

sql console settings

Codes Magic © 2013-2018 · Terms