Merge pull request #579 from pbnoyz/feat/audit

feat/audit: 审计功能优化
This commit is contained in:
Michael Yang 2025-08-29 09:02:54 +08:00 committed by GitHub
commit 29d82efa64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 112 additions and 46 deletions

View File

@ -25,7 +25,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>
@ -122,7 +122,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>
@ -136,7 +136,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-solon-plugin</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>

View File

@ -25,13 +25,13 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
@ -100,13 +100,6 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -97,11 +97,12 @@ public class AuditManager {
}
@SuppressWarnings("rawtypes")
public static <T> T startAudit(AuditRunnable<T> supplier, Statement statement, BoundSql boundSql, Configuration configuration) throws SQLException {
public static <T> T startAudit(AuditRunnable<T> supplier, String stmtId, Statement statement, BoundSql boundSql, Configuration configuration) throws SQLException {
AuditMessage auditMessage = messageFactory.create();
if (auditMessage == null) {
return supplier.execute();
}
auditMessage.setStmtId(stmtId);
String key = DataSourceKey.get();
if (StringUtil.noText(key)) {
key = FlexGlobalConfig.getDefaultConfig()

View File

@ -95,6 +95,11 @@ public class AuditMessage implements Serializable {
*/
private long elapsedTime;
/**
* MappedStatement ID
*/
private String stmtId;
/**
* 数据库名称
*/
@ -270,6 +275,14 @@ public class AuditMessage implements Serializable {
metas.put(key, value);
}
public String getStmtId() {
return stmtId;
}
public void setStmtId(String stmtId) {
this.stmtId = stmtId;
}
public String getDsName() {
return dsName;
}
@ -293,6 +306,7 @@ public class AuditMessage implements Serializable {
", queryCount=" + queryCount +
", queryTime=" + queryTime +
", elapsedTime=" + elapsedTime +
", stmtId=" + stmtId +
", dsName=" + dsName +
", metas=" + metas +
'}';

View File

@ -46,9 +46,11 @@ public class FlexStatementHandler implements StatementHandler {
private final BoundSql boundSql;
private final boolean auditEnable = AuditManager.isAuditEnable();
private final Configuration configuration;
private final String stmtId;
public FlexStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
configuration = ms.getConfiguration();
stmtId = ms.getId();
switch (ms.getStatementType()) {
case STATEMENT:
delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
@ -83,7 +85,7 @@ public class FlexStatementHandler implements StatementHandler {
AuditManager.startAudit(() -> {
delegate.batch(statement);
return null;
}, statement, boundSql, configuration);
}, stmtId, statement, boundSql, configuration);
} else {
delegate.batch(statement);
}
@ -91,19 +93,19 @@ public class FlexStatementHandler implements StatementHandler {
@Override
public int update(Statement statement) throws SQLException {
return auditEnable ? AuditManager.startAudit(() -> delegate.update(statement), statement, boundSql, configuration)
return auditEnable ? AuditManager.startAudit(() -> delegate.update(statement), stmtId, statement, boundSql, configuration)
: delegate.update(statement);
}
@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
return auditEnable ? AuditManager.startAudit(() -> delegate.query(statement, resultHandler), statement, boundSql, configuration)
return auditEnable ? AuditManager.startAudit(() -> delegate.query(statement, resultHandler), stmtId, statement, boundSql, configuration)
: delegate.query(statement, resultHandler);
}
@Override
public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
return auditEnable ? AuditManager.startAudit(() -> delegate.queryCursor(statement), statement, boundSql, configuration)
return auditEnable ? AuditManager.startAudit(() -> delegate.queryCursor(statement), stmtId, statement, boundSql, configuration)
: delegate.queryCursor(statement);
}

View File

@ -12,7 +12,6 @@
</parent>
<artifactId>mybatis-flex-dependencies</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
@ -61,42 +60,42 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-codegen</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-solon-plugin</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot3-starter</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -35,7 +35,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -13,8 +13,6 @@
<name>mybatis-flex-processor</name>
<artifactId>mybatis-flex-processor</artifactId>
<packaging>jar</packaging>
<version>${revision}</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
@ -25,7 +23,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
</dependencies>

View File

@ -30,7 +30,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -25,7 +25,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
@ -106,7 +106,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
</dependencies>

View File

@ -22,7 +22,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>

View File

@ -24,7 +24,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -29,7 +29,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-loveqq-starter</artifactId>
<version>${revision}</version>
<version>${project.version</version>
</dependency>
<dependency>

View File

@ -24,7 +24,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -22,7 +22,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>${revision}</version>
<version>${project.version</version>
</dependency>
<dependency>

View File

@ -26,7 +26,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-solon-plugin</artifactId>
<version>${revision}</version>
<version>${project.version</version>
</dependency>
<dependency>
@ -97,7 +97,7 @@
<path>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>

View File

@ -24,7 +24,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -58,7 +58,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
@ -85,7 +85,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-test</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
@ -95,6 +95,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</build>

View File

@ -24,13 +24,13 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</dependency>
<dependency>

View File

@ -18,6 +18,9 @@ package com.mybatisflex.test.mapper;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.test.model.Account;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author 王帅
@ -25,4 +28,7 @@ import com.mybatisflex.test.model.Account;
*/
public interface AccountMapper extends BaseMapper<Account> {
@Select("select * from tb_account")
List<Account> selectAccounts();
}

View File

@ -0,0 +1,49 @@
package com.mybatisflex.test;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.AuditMessage;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.test.mapper.AccountMapper;
import com.mybatisflex.test.mapper.TbClassMapper;
import lombok.Getter;
import org.assertj.core.api.WithAssertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class AuditTest implements WithAssertions {
@Autowired
AccountMapper accountMapper;
@Autowired
TbClassMapper tbClassMapper;
@Test
public void test() {
TestMsgCollector collector = new TestMsgCollector();
AuditManager.setMessageCollector(collector);
AuditManager.setAuditEnable(true);
accountMapper.selectAccounts();
tbClassMapper.selectAll();
List<AuditMessage> messages = collector.getMessages();
assertThat(messages.size()).isEqualTo(2);
assertThat(messages.get(0).getStmtId()).isEqualTo("com.mybatisflex.test.mapper.AccountMapper.selectAccounts");
assertThat(messages.get(1).getStmtId()).isEqualTo("com.mybatisflex.test.mapper.TbClassMapper.selectListByQuery");
}
static class TestMsgCollector implements MessageCollector {
@Getter
private final List<AuditMessage> messages = new ArrayList<>();
@Override
public void collect(AuditMessage message) {
messages.add(message);
}
}
}

View File

@ -77,7 +77,7 @@
<path>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>${revision}</version>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>

View File

@ -62,6 +62,9 @@
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>