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) {
        // 設置插件屬性  
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章