在默认条件下,SpringBoot整合Mybatis不会开启日志打印功能,但是有时候需要进行查看。
- 查看sql语句
- 查看查询结果
- 查看是否是在同一个会话当中
例子
想要通过日志查看这个多对多查询结果是不是在同一个sql会话当中。
@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
说明是在同一个会话当中。
参考
参考链接



