Mybatis如何打印sql语句

导读:本篇文章讲解 Mybatis如何打印sql语句,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/72695.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!