简体   繁体   English

如何在 MyBatis 中记录有关其交易的查询

[英]How can I log queries with regard to the their transactions in MyBatis

I have a spring boot project (2.7) and I'm using Java 17 and I'm using this maven package for Mybatis : mybatis-spring-boot-starter:2.2.2.我有一个 spring boot 项目(2.7),我正在使用 Java 17,我正在为 Mybatis 使用这个 maven 包:mybatis-spring-boot-starter:2.2.2。 And I'm using postgres 14 as my database.我使用 postgres 14 作为我的数据库。

In the logs I enabled Mybatis query logs, but unfortunately still I don't see logs of transactions.在日志中我启用了 Mybatis 查询日志,但不幸的是我仍然没有看到事务日志。 I want to see which queries are part of which transactions.我想看看哪些查询是哪些事务的一部分。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

and this is my application properties file :这是我的应用程序属性文件:

mybatis.typeAliasesPackage=com.example.demo.dto
mybatis.config-location=classpath:mybatis-config.xml
logging.level.org.springframework=WARN
logging.level.com.example.demo.dao.UserMapper=DEBUG
logging.level.com.example.demo.dao.TransactionMapper=DEBUG

spring.datasource.url=jdbc:postgresql://localhost:5432/user
spring.datasource.username=user
spring.datasource.password=password

and here is my mybatis config file:这是我的 mybatis 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="cacheEnabled" value="false"/>
        <setting name="localCacheScope" value="STATEMENT"/>
    </settings>
    <typeAliases>
        <package name="com.example.demo.dto" />
    </typeAliases>


    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
        <mapper resource="mapper/TransactionMapper.xml"/>
    </mappers>
</configuration>

我找到了答案,正如评论中提到的那样,我应该在 application.properties 中添加这个:

logging.level.org.springframework.jdbc=DEBUG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM