mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
fixed: 审计日志中赋值异常的问题 close #I6Z1R8
This commit is contained in:
parent
bf5e1c8bf2
commit
73bc5e0ec5
@ -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> T startAudit(AuditRunnable<T> supplier, BoundSql boundSql) throws SQLException {
|
||||
public static <T> T startAudit(AuditRunnable<T> 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<ParameterMapping> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
|
||||
@ -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 <E> List<E> 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 <E> Cursor<E> 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);
|
||||
}
|
||||
|
||||
|
||||
@ -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<ContextRefreshedEvent> {
|
||||
|
||||
|
||||
// @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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user