mybatis插件怎么获取 Mapper 接口方法@Param注解 Map类型参数,MyBatis 3.5.10 版本

mybatis插件怎么获取 Mapper 接口方法@Param注解 Map类型参数,MyBatis 3.5.10 版本

 

1.在 MyBatis 的配置文件中配置插件。

<!-- 配置插件 -->
<plugins>
    <plugin interceptor="com.example.MyPlugin">
        <!-- 设置插件属性 -->
    </plugin>
</plugins>

2.编写插件

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.Configuration;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class MyPlugin implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 获取 Mapper 接口方法的参数值
        Object parameter = invocation.getArgs()[1];

        // 获取 Mapper 接口方法对象
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Configuration configuration = mappedStatement.getConfiguration();
        Class<?> mapperInterface = Class.forName(mappedStatement.getId().substring(0, mappedStatement.getId().lastIndexOf(".")));
        String methodName = mappedStatement.getId().substring(mappedStatement.getId().lastIndexOf(".") + 1);
        Method method = findMethod(mapperInterface, methodName);

        // 获取 Mapper 接口方法的参数名称数组
        String[] paramNames = getParameterNames(method);

        // 判断参数是否为 Map 类型
        if (parameter instanceof Map) {
            Map<?, ?> paramMap = (Map<?, ?>) parameter;

            // 遍历参数名称数组
            for (String paramName : paramNames) {
                // 根据参数名称获取对应的值
                Object paramValue = paramMap.get(paramName);

                // 处理参数值
                if (paramValue != null) {
                    // 进行需要的操作
                    // ...
                }
            }
        }

        // 继续执行原始的方法逻辑
        return invocation.proceed();
    }

    private Method findMethod(Class<?> mapperInterface, String methodName) {
        for (Method method : mapperInterface.getMethods()) {
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        throw new IllegalArgumentException("Cannot find method " + methodName + " in Mapper interface " + mapperInterface);
    }

    private String[] getParameterNames(Method method) {
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        List<String> ls = new ArrayList<>();
// 遍历参数注解获取 @Param 注解的参数名称
        for (int i = 0; i < parameterAnnotations.length; i++) {
            Annotation[] annotations = parameterAnnotations[i];
            for (Annotation annotation : annotations) {
                if (annotation.annotationType() == Param.class) {
                    Param paramAnnotation = (Param) annotation;
                    String paramName = paramAnnotation.value();
                    ls.add(paramName);
                    break;
                }
            }
        }
        return ls.toArray(new String[0]);
    }

    @Override
    public Object plugin(Object target) {
        // 对目标对象创建代理 
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // 设置插件属性  
    }
}

 

posted @ 2023-09-07 10:33  _Phoenix  阅读(279)  评论(0编辑  收藏  举报