【MyBatis】拦截查询结果同时动态替换

说明

项目中需要用到响应时替换某些字段的某些值。

代码

package xxx.xxx.xx;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 属性值替换
 *
 * @author behappyto.cn
 */
@Slf4j
@Intercepts({@Signature(type = Executor.class, method = "query"
        , args = {MappedStatement.class, Object.class, RowBounds.class,
        ResultHandler.class}),
        @Signature(type = Executor.class, method = "query"
                , args = {MappedStatement.class, Object.class, RowBounds.class,
                ResultHandler.class, CacheKey.class, BoundSql.class})})
public class PropertyInterceptor implements Interceptor {

    /**
     * 当前的值可以放到配置文件、配置中心或者其他方式动态获取
     */
    private final static HashMap<String, String> TABLE_COLUMN = new HashMap<>();

    static {
        TABLE_COLUMN.put("实体类名", "属性名");
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object obj = invocation.proceed();
        try {
            if (ObjectUtils.allNull(obj)) {
                return obj;
            }
            if (!(obj instanceof List)) {
                return obj;
            }
            List<Object> objectList = (List<Object>) obj;
            String newValue = "替换的值"
            if (StringUtils.isBlank(newValue)) {
                return objectList;
            }
            List<Object> respList = new ArrayList<>();
            for (Object object : objectList) {
                respList.add(this.getObject(newValue, object));
            }
            return respList;
        } catch (Exception exception) {
            log.error("ex", exception);
            return obj;
        }
    }

    /**
     * 转换对象
     *
     * @param domain 需要替换的域名
     * @param object 数据库的对象
     * @return 返回 转换后的对象信息
     * @throws IllegalAccessException 异常
     */
    private Object getObject(String domain, Object object) throws IllegalAccessException {
        Class<?> aClass = object.getClass();
        String tableEntry = aClass.getSimpleName();
        String columnField = TABLE_COLUMN.get(tableEntry);
        if (StringUtils.isNotBlank(columnField)) {
            Field[] fields = ReflectUtil.getFields(aClass);
            Optional<Field> optional = Arrays.stream(fields).filter(item ->
                    item.getName().equals(columnField)).findFirst();
            if (!optional.isPresent()) {
                return object;
            }
            Field field = optional.get();
            Object obj = field.get(tableEntry);
            if (obj instanceof String) {
                field.set(field.getName(), MessageFormat.format((String) obj, domain));
            }
        }
        return object;
    }
}

相关推荐

  1. MyBatis拦截查询结果同时动态替换

    2023-12-14 03:36:02       37 阅读
  2. MyBatis之关联查询

    2023-12-14 03:36:02       35 阅读
  3. 2. Mybatis案例(查询

    2023-12-14 03:36:02       38 阅读
  4. Mybatis多表查询

    2023-12-14 03:36:02       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-14 03:36:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-14 03:36:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-14 03:36:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-14 03:36:02       20 阅读

热门阅读

  1. 20230907docker安装MySQL配置

    2023-12-14 03:36:02       36 阅读
  2. linux 13-2day 日志轮转 日志目录 轮转参数

    2023-12-14 03:36:02       42 阅读
  3. libtorch常用函数记录

    2023-12-14 03:36:02       40 阅读
  4. 猎界新能源

    2023-12-14 03:36:02       46 阅读
  5. MATLAB Sub2ind下标值转化

    2023-12-14 03:36:02       48 阅读
  6. 配置Ubuntu18.04使iptables规则重启系统后仍然有效

    2023-12-14 03:36:02       48 阅读
  7. 计算机网络期末考试A卷及答案

    2023-12-14 03:36:02       38 阅读
  8. 嵌入式C语言(6)——数组

    2023-12-14 03:36:02       34 阅读
  9. 59. 螺旋矩阵 II

    2023-12-14 03:36:02       43 阅读