mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
fix: 解决 XML 审计无法打印参数以及审计参数顺序问题,关闭 https://gitee.com/mybatis-flex/mybatis-flex/issues/I9TBG2 和 https://gitee.com/mybatis-flex/mybatis-flex/issues/IAUFRN
This commit is contained in:
parent
d865f95af7
commit
9532289d7d
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -18,12 +18,12 @@ package com.mybatisflex.core.audit;
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
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;
|
||||
|
||||
@ -123,39 +123,46 @@ public class AuditManager {
|
||||
} finally {
|
||||
auditMessage.setElapsedTime(clock.getTick() - auditMessage.getQueryTime());
|
||||
auditMessage.setQuery(boundSql.getSql());
|
||||
|
||||
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
|
||||
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
|
||||
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(statement, ((Map<?, ?>) parameter).get(FlexConsts.SQL_ARGS));
|
||||
} else if (((Map<?, ?>) parameter).containsKey("collection")) {
|
||||
Collection collection = (Collection) ((Map<?, ?>) parameter).get("collection");
|
||||
auditMessage.addParams(statement, collection.toArray());
|
||||
} else if (((Map<?, ?>) parameter).containsKey("array")) {
|
||||
auditMessage.addParams(statement, ((Map<?, ?>) parameter).get("array"));
|
||||
} else {
|
||||
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(statement, value);
|
||||
// 实现 XML 与 QueryWrapper 参数解析互不干涉
|
||||
|
||||
if (CollectionUtil.isNotEmpty(parameterMappings)) {
|
||||
// 组装 XML 中的 #{user.age} 参数
|
||||
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(statement, value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 组装 QueryWrapper 里面的 age = ? 参数
|
||||
// parameter 的组装请查看 ParamNameResolver#getNamedParams(Object[]) 方法
|
||||
if (parameter instanceof Map) {
|
||||
Map<?, ?> map = (Map<?, ?>) parameter;
|
||||
if (map.containsKey(FlexConsts.SQL_ARGS)) {
|
||||
auditMessage.addParams(statement, map.get(FlexConsts.SQL_ARGS));
|
||||
} else if (map.containsKey("collection")) {
|
||||
Collection collection = (Collection) map.get("collection");
|
||||
auditMessage.addParams(statement, collection.toArray());
|
||||
} else if (map.containsKey("array")) {
|
||||
auditMessage.addParams(statement, map.get("array"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
messageCollector.collect(auditMessage);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user