From 73bc5e0ec5a44ae4f77e5d2399fceb8697183f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 26 Apr 2023 16:45:48 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20=E5=AE=A1=E8=AE=A1=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=AD=E8=B5=8B=E5=80=BC=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20close=20#I6Z1R8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/audit/AuditManager.java | 27 +++++++++++++++---- .../core/audit/ConsoleMessageCollector.java | 6 ----- .../core/mybatis/FlexStatementHandler.java | 12 +++++---- .../mybatisflex/test/SampleApplication.java | 23 +++++++++++++++- .../test/mapper/MyAccountMapper.java | 2 +- 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditManager.java index 19880470..f5560540 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditManager.java @@ -17,10 +17,16 @@ package com.mybatisflex.core.audit; import com.mybatisflex.core.FlexConsts; import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.ParameterMapping; +import org.apache.ibatis.mapping.ParameterMode; +import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.ParamNameResolver; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandlerRegistry; import java.sql.SQLException; import java.util.Collection; +import java.util.List; import java.util.Map; /** @@ -80,7 +86,7 @@ public class AuditManager { } } - public static T startAudit(AuditRunnable supplier, BoundSql boundSql) throws SQLException { + public static T startAudit(AuditRunnable supplier, BoundSql boundSql, Configuration configuration) throws SQLException { AuditMessage auditMessage = MessageFactory.create(); if (auditMessage == null) { return supplier.execute(); @@ -99,11 +105,11 @@ public class AuditManager { auditMessage.setQuery(boundSql.getSql()); Object parameter = boundSql.getParameterObject(); - /** parameter 的组装请查看 getNamedParams 方法 * @see ParamNameResolver#getNamedParams(Object[]) */ if (parameter instanceof Map) { + TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); if (((Map) parameter).containsKey(FlexConsts.SQL_ARGS)) { auditMessage.addParams(((Map) parameter).get(FlexConsts.SQL_ARGS)); } else if (((Map) parameter).containsKey("collection")) { @@ -112,9 +118,20 @@ public class AuditManager { } else if (((Map) parameter).containsKey("array")) { auditMessage.addParams(((Map) parameter).get("array")); } else { - for (int i = 1; i <= 100; i++) { - if (((Map) parameter).containsKey(ParamNameResolver.GENERIC_NAME_PREFIX + i)) { - auditMessage.addParams(((Map) parameter).get(ParamNameResolver.GENERIC_NAME_PREFIX + i)); + List parameterMappings = boundSql.getParameterMappings(); + for (ParameterMapping parameterMapping : parameterMappings) { + if (parameterMapping.getMode() != ParameterMode.OUT) { + Object value; + String propertyName = parameterMapping.getProperty(); + if (boundSql.hasAdditionalParameter(propertyName)) { + value = boundSql.getAdditionalParameter(propertyName); + } else if (typeHandlerRegistry.hasTypeHandler(parameter.getClass())) { + value = parameter; + } else { + MetaObject metaObject = configuration.newMetaObject(parameter); + value = metaObject.getValue(propertyName); + } + auditMessage.addParams(value); } } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java index c8adcbe6..ceb28488 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ConsoleMessageCollector.java @@ -15,12 +15,6 @@ */ package com.mybatisflex.core.audit; -import com.mybatisflex.core.util.DateUtil; - -import java.time.LocalDateTime; -import java.util.Date; -import java.util.List; -import java.util.regex.Matcher; public class ConsoleMessageCollector implements MessageCollector { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexStatementHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexStatementHandler.java index 0516c717..7246a443 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexStatementHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexStatementHandler.java @@ -25,6 +25,7 @@ import org.apache.ibatis.executor.statement.SimpleStatementHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @@ -44,9 +45,10 @@ public class FlexStatementHandler implements StatementHandler { private final StatementHandler delegate; private final BoundSql boundSql; private final boolean auditEnable = AuditManager.isAuditEnable(); + private final Configuration configuration; public FlexStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { - + configuration = ms.getConfiguration(); switch (ms.getStatementType()) { case STATEMENT: delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); @@ -82,7 +84,7 @@ public class FlexStatementHandler implements StatementHandler { AuditManager.startAudit(() -> { delegate.batch(statement); return null; - }, boundSql); + }, boundSql, configuration); } else { delegate.batch(statement); } @@ -90,19 +92,19 @@ public class FlexStatementHandler implements StatementHandler { @Override public int update(Statement statement) throws SQLException { - return auditEnable ? AuditManager.startAudit(() -> delegate.update(statement), boundSql) + return auditEnable ? AuditManager.startAudit(() -> delegate.update(statement), boundSql,configuration) : delegate.update(statement); } @Override public List query(Statement statement, ResultHandler resultHandler) throws SQLException { - return auditEnable ? AuditManager.startAudit(() -> delegate.query(statement, resultHandler), boundSql) + return auditEnable ? AuditManager.startAudit(() -> delegate.query(statement, resultHandler), boundSql, configuration) : delegate.query(statement, resultHandler); } @Override public Cursor queryCursor(Statement statement) throws SQLException { - return auditEnable ? AuditManager.startAudit(() -> delegate.queryCursor(statement), boundSql) + return auditEnable ? AuditManager.startAudit(() -> delegate.queryCursor(statement), boundSql, configuration) : delegate.queryCursor(statement); } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java index fd4cf813..4e211840 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java @@ -15,14 +15,21 @@ */ package com.mybatisflex.test; +import com.mybatisflex.core.audit.AuditManager; +import com.mybatisflex.core.audit.ConsoleMessageCollector; +import com.mybatisflex.core.audit.MessageCollector; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.ContextRefreshedEvent; @SpringBootApplication +@Configuration @MapperScan("com.mybatisflex.test.mapper") -public class SampleApplication implements CommandLineRunner { +public class SampleApplication implements CommandLineRunner, ApplicationListener { // @Resource @@ -40,4 +47,18 @@ public class SampleApplication implements CommandLineRunner { } + + + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + System.out.println("onApplicationEvent"); + //开启审计功能 + AuditManager.setAuditEnable(true); + +//设置 SQL 审计收集器 + MessageCollector collector = new ConsoleMessageCollector(); + AuditManager.setMessageCollector(collector); + } + } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java index d390bd0d..9ba13aea 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java @@ -9,6 +9,6 @@ public interface MyAccountMapper extends AccountMapper { Account selectByName(@Param("name") String name); - @Select("select * from tb_account where id = #{id}") + @Select("select * from tb_account where id = #{id} and id =#{id}") Account selectById(@Param("id") Object id); }