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=debug

logging.level,后面的路径指的是mybatis对应的方法接口所在的包

如果是mybatis-plus可做如下配置

#mybatis-plus配置控制台打印完整带参数SQL语句
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leo825...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值