Home > Java > javaTutorial > body text

How does SpringBoot print the execution sql problem of mybatis?

WBOY
Release: 2023-05-15 22:55:04
forward
8414 people have browsed it

    SpringBoot prints the execution sql of mybatis

    1. Usage scenarios

    should be to track the back-end SQL statements during the development process. What caused the error. The executed SQL statements need to be printed out during the Debug process. So you need to configure SpringBoot and Mybatis to print SQL statements.

    2. Specific implementation

    Two ways to configure in application.properties(yml):

    • 1. logging.level.dao package name (dao package)=debug

    • 2. mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    2.1. Plan 1 prints all mybatis SQL

    The following is the yaml file configuration, and the properties file can be translated

    mybatis
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    Copy after login

    2.2. Specify the location where the mapper file is located Package

    logging:
      level:
        cn.com.demos.*.mapper: trace # 改成你的mapper文件所在包路径
    Copy after login

    How does SpringBoot print the execution sql problem of mybatis?

    For example, I use the second method as follows:

    The mybatis configuration in application.yml is modified as follows:

    How does SpringBoot print the execution sql problem of mybatis?

    You can see the execution effect as follows:

    How does SpringBoot print the execution sql problem of mybatis?

    SpringBoot turns on mybatis’s sql printing

    When debugging a Java project locally, in order to view the specific interaction with the database more intuitively, sometimes SQL printing is required.

    Solution

    Solution 1:

    springboot does not enable mybatis log output by default, and you need to manually configure it to enable debug level printing.

    Since SpringBoot has introduced spring-boot-starter-logging by default, you only need to configure it, as follows:

    logging.level.cn.com.**.web.mapper=debug
    Copy after login

    Description: "cn.com.**.web.mapper" is the mapper package path.

    Option 2:

    Add log configuration

    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    Copy after login

    After configuration, the sql sent by mybatis will be output on the console. It is recommended to use the first way.

    The above is the detailed content of How does SpringBoot print the execution sql problem of mybatis?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template