前言

在平常使用mybatis时, 即使开启了日志打印, 打印出来的sql也是预编译语句和参数两行
在这里插入图片描述
我们要去数据库中去执行sql的时候, 还需要自己去将参数拼接进去,
现在我们可以通过拦截器来实现打印完整的sql,结果如下

HanSql : select a.id, a.title, a.opera_time as operaTime, c.id as comment_id, c.context as comment_context, c.blog_id as comment_blogId from blog a left join comment c on a.id = c.blog_id WHERE title like concat('%', 'csdn' ,'%')  and a.id in (  1  ,  2  ) and c.id in (  1  ,  2  ,  3  )  and opera_time like concat('%', '2022-05-28' ,'%')   and opera_time like concat('%', '2022-05-28 00:00:00' ,'%')   and opera_time like concat('%', '2022-05-28 00:00:00' ,'%')

前置要求

为了节约代码量, 降低代码的复杂程度,需要遵守一下要求

  • 引入hutool工具包
        <!--hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.4</version>
        </dependency>

  • 动态sql , foreach中的#{}中的值必须是传入对象 + “Item”, 如下:
    @Param(“list”) List list —>
<foreach collection="list" index="index" open="(" close=")" separator="," item="listItem">
     #{listItem}
</foreach>
  • spring.profiles.active = dev, 也就是开发环境才起作用
  • yyyy-MM-dd格式的日期用LocalDate.java 接收
  • yyyy-MM-dd HH:mm:ss格式的日期用LocalDateTime.java 接收

项目路径

在这里插入图片描述

git地址

https://gitee.com/hctrl/mybatis-project.git

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐