SpringBoot开启mybatis的日志打印

在默认条件下,SpringBoot整合Mybatis不会开启日志打印功能,但是有时候需要进行查看。

  • 查看sql语句
  • 查看查询结果
  • 查看是否是在同一个会话当中

例子

想要通过日志查看这个多对多查询结果是不是在同一个sql会话当中。

/**
     * 一对多查询进行封装 但是要进行多次查询,但是在同一个sqlsession当中
     * @param id
     * @return
     */
    @Results(id = "result", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "release_time", property = "releaseTime"),
            @Result(column = "director", property = "director"),
            @Result(column = "duration", property = "duration"),
            @Result(column = "type", property = "type"),
            @Result(column = "region", property = "region"),
            @Result(column = "db_score", property = "db_score"),
            @Result(column = "image_address", property = "imageAddress"),
            @Result(column = "description", property = "description"),
            @Result(column = "bt_seed", property = "bt_seed"),
            @Result(column = "addTime", property = "addTime"),
            @Result( property = "channelList", column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.ll.videowebsite.mapper.ChannelMapper.loadByVideoId")
            ),
            @Result( property = "actorList", column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.ll.videowebsite.mapper.ActorMapper.loadByVideoId")
            ),
            @Result( property = "commentList", column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.ll.videowebsite.mapper.CommentMapper.loadByVideoId")
            ),
            @Result( property = "shortVideoList", column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.ll.videowebsite.mapper.ShortVideoMapper.loadAll")
            ),
            @Result( property = "imageList", column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.ll.videowebsite.mapper.ImageMapper.loadAll")
            )
    })
    @Select("SELECT * FROM videoDetails WHERE id = #{id}")
    VideoDetails loadById1(int id);

解决方法:

方法一

在application 中配置如下信息。

# mybatis日志打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

控制台输出结果:

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@31912bd6] was not registered for synchronization because synchronization is not active
2022-05-24 16:46:51.309  INFO 1972 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-05-24 16:46:51.640  INFO 1972 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1058939125 wrapping com.mysql.cj.jdbc.ConnectionImpl@34c82ec] will not be managed by Spring
==>  Preparing: SELECT * FROM videoDetails WHERE id = ?
==> Parameters: 2(Integer)
<==    Columns: id, name, image_address, director, release_time, duration, type, region, db_score, description, bt_seed, addTime
<==        Row: 2, 超能陆战队, 暂无资源, 约翰·拉塞特, 2020-12-01, 102, 1, 4, 9.8, 《超能陆战队》主要讲述充气机器人大白与天才少年小宏联手菜鸟小伙伴组建超能战队,共同打击犯罪阴谋的故事。, null, 2022-05-18
====>  Preparing: SELECT * FROM channels WHERE id IN (SELECT c_id FROM video_channels_table WHERE v_id = ?)
====> Parameters: 2(Integer)
<====      Total: 0
====>  Preparing: SELECT * FROM actors WHERE id IN (SELECT c_id FROM video_actors_table WHERE v_id = ?)
====> Parameters: 2(Integer)
<====    Columns: id, name, image_address
<====        Row: 5, 斯科特·埃德希特/郝祥海, NULL
<====        Row: 6, 斯科特·埃德希特/郝祥海, NULL
<====        Row: 7, 斯科特·埃德希特/郝祥海, NULL
<====        Row: 8, 斯科特·埃德希特/郝祥海, NULL
<====      Total: 4
====>  Preparing: SELECT * FROM comments WHERE id IN (SELECT c_id FROM video_comments_table WHERE v_id = ?)
====> Parameters: 2(Integer)
<====    Columns: id, user_id, score, comment, addTime
<====        Row: 6, 1, 8, NULL, 2022-05-18 12:19:32
<====        Row: 7, 1, 8, NULL, 2022-05-18 12:19:32
<====        Row: 8, 1, 8, NULL, 2022-05-18 12:19:32
<====        Row: 9, 1, 8, NULL, 2022-05-18 12:19:32
<====        Row: 10, 1, 8, NULL, 2022-05-18 12:19:32
<====      Total: 5
====>  Preparing: SELECT * FROM shortvideo
====> Parameters: 
<====      Total: 0
====>  Preparing: SELECT * FROM image
====> Parameters: 
<====      Total: 0
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@31912bd6]

Process finished with exit code -1

说明是在同一个会话当中。

参考
参考链接

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值