MyBatis控制台显示SQL语句的方法

时间:2020-03-10 22:27:19 类型:JAVA
字号:    


一. mybatis-config.xml”配置文件

<configuration>
	<settings> 
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>
     
</configuration>

二. 使用slf4j输出,在pom.xml中添加依赖

<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.6</version>
</dependency>

配置输出log4j.properties

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.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

显示的SQL样式如下:

Creating a new SqlSession

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6412c7ff] was not registered for synchronization because synchronization is not active

Cache Hit Ratio [SQL_CACHE]: 0.0

JDBC Connection [752559656, URL=jdbc:mysql://localhost:3306/zhuangzi?characterEncoding=utf8, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring

==>  Preparing: SELECT count(0) FROM student 

==> Parameters: 

<==    Columns: count(0)

<==        Row: 23

<==      Total: 1

==>  Preparing: select * from student LIMIT ? 

==> Parameters: 2(Integer)

<==    Columns: id, names, email, sex, blood, hobby, intro, time, views, pic

<==        Row: 21, 王小二, null, 男, A, 跑步,打牌, <<BLOB>>, 1583412502843, 18, 

<==        Row: 22, 小峭, null, 男, A, 跑步,打牌, <<BLOB>>, 1583412535078, 18, 

<==      Total: 2

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6412c7ff]


<