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

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

参考
参考链接

### 配置 MyBatis 日志输出 在 Spring Boot 项目中整合 MyBatis 并启用日志输出,主要涉及以下几个方面的配置: #### 1. 添加依赖 确保在 `pom.xml` 中引入 MyBatis 的 Starter 依赖,并根据 Spring Boot 的版本选择合适的版本号。例如,Spring Boot 3.4.x 需要使用 MyBatis Starter 3.0.x+,以兼容 Jakarta EE 9+ 规范[^1]。以下是一个典型的依赖配置: ```xml <!-- MyBatis Spring Boot Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> ``` #### 2. 配置数据源和 MyBatis 在 `application.yml` 或 `application.properties` 中配置数据库连接信息以及 MyBatis 的相关参数。其中,`mybatis.configuration.log-impl` 用于指定日志实现类,推荐使用 `org.apache.ibatis.logging.stdout.StdOutImpl` 以直接输出 SQL 到控制台: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mybatis_demo?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.demo.entity configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` #### 3. 启用 Mapper 扫描 确保 MyBatis 的 Mapper 接口能够被正确扫描和注册。可以在主类上使用 `@MapperScan` 注解,指定包含 Mapper 接口的包路径: ```java import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 4. 验证日志输出 启动项目后,观察控制台是否输出了 SQL 语句。如果配置正确,MyBatis 将在执行数据库操作时打印 SQL 语句及其参数,便于调试和优化。 --- ### 相关问题 1. 如何在 Spring Boot 项目中使用 `@MapperScan` 注解? 2. Spring Boot 3.4.x 项目中如何选择兼容的 MyBatis Starter 版本? 3. 如何通过 `application.yml` 配置 MyBatis 的 XML 映射文件路径? 4. 如何在 Spring Boot 中配置 MyBatis 的驼峰命名转换? 5. 如何在 Spring Boot 项目中整合 MyBatis 并实现多数据源配置?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值