一. mybatis-config.xml”配置文件
<configuration> <settings> <!-- 打印查詢語句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- 配置分頁插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 設(shè)置數(shù)據(jù)庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫--> <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]