Mybatis如何打印sql语句
生活随笔
收集整理的這篇文章主要介紹了
Mybatis如何打印sql语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、SpringMVC集成mybatis配置
首先,添加mybatis的配置文件mybatis-config.xml,可以原樣復制
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><!-- 打印查詢語句 --><setting name="logImpl" value="STDOUT_LOGGING" /></settings> </configuration>其次,在sqlSessionFactory配置中添加mybatis的配置文件
<!--3 會話工廠bean sqlSessionFactoryBean --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數據源 --><property name="dataSource" ref="datasource"></property><!-- 別名 --><property name="typeAliasesPackage" value="com.leo.model"></property><!-- mybatis的配置文件 --><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- sql映射文件路徑 --><property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property></bean>效果如下:
==> Preparing: SELECT ID id, NAME name, GENDER gender,AGE age,REMARKS remarks FROM USER_INFO ==> Parameters: <== Columns: id, name, gender, age, remarks <== Row: 3, 曉玲, 0, 22, 工程師 <== Row: 4, 曉玲, 0, 24, 工程師 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1465f846] 2020-05-28 18:07:59.713 INFO com.leo.controller.HelloController:119 [http-apr-8080-exec-2] - [UserInfo{id=3, name='曉玲', gender='0', age='22', remarks='工程師'}, UserInfo{id=4, name='曉玲', gender='0', age='24', remarks='工程師'}] 2020-05-28 18:07:59.717 INFO com.leo.interceptor.HandlerInterceptor1:34 [http-apr-8080-exec-2] - HandlerInterceptor1 postHandle 2020-05-28 18:07:59.718 INFO com.leo.interceptor.HandlerInterceptor1:39 [http-apr-8080-exec-2] - HandlerInterceptor1 afterCompletion 2020-05-28 18:07:59.718 INFO com.leo.interceptor.HandlerInterceptor1:42 [http-apr-8080-exec-2] - HandlerInterceptor1 過濾的接口耗時:9ms如果是集成log4j2配置文件mybatis-config.xml如下所示
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><!-- 打印查詢語句 --><setting name="logImpl" value="LOG4J2" /></settings> </configuration>2、Springboot集成mybatis配置
如果是Springboot的繼承mybatis
logging.level.com.leo.mapper=debuglogging.level,后面的路徑指的是mybatis對應的方法接口所在的包
如果是mybatis-plus可做如下配置
#mybatis-plus配置控制臺打印完整帶參數SQL語句 mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl總結
以上是生活随笔為你收集整理的Mybatis如何打印sql语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java标签Label,如何不使用con
- 下一篇: JavaWeb项目中如何扩展一个Requ