关于Mybatis拦截器对结果集的拦截

刚学习Mybatis拦截器方面,在网上找了很多关于Mybatis拦截器方面的文章,自己也尝试过写过几个,但是关于结果集的拦截始终没有找到合适的(PS: 不要喷我,毕竟是新手)。也在segmentfault 上提问过,依然没有找到一个易于理解的,后来自己慢慢理解了以后,自己写了个入门的,作为自己的回答。

Mybatis实现过如下需求

查询用户基本信息表,查询结果返回是List<Map<String,Object>>的结果集,对其中的某个字段进行加密

数据表
CREATE TABLE usr_basic_inf(
    USR_ID               VARCHAR(20)       NOT NULL      COMMENT '用户ID,01+18位互斥随机数'    ,
    USR_REAL_NME         VARCHAR(50)       NOT NULL      COMMENT '用户真实姓名'              ,
    CERT_TYPE            VARCHAR(4)                      COMMENT '证件种类'                ,
    CERT_NO              VARCHAR(100)                    COMMENT '证件号码'                ,
    RES_FLD              VARCHAR(300)                    COMMENT '预留字段 '               ,
    PRIMARY KEY(USR_ID)
) COMMENT='用户基础信息表';
数据
USR_IDUSR_REAL_NMECERT_TYPECERT_NORES_FLD
01000000000000000001张三0101101222010199913291(NULL)

使用MybatisExecutor.class'qurey'方法进行拦截,并对返回结果集进行处理

package com.ceabox.interceptor;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
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.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
        RowBounds.class, ResultHandler.class }) })
public class InterceptorForQry implements Interceptor {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed(); //执行请求方法,并将所得结果保存到result中
        if (result instanceof ArrayList) {
            ArrayList resultList = (ArrayList) result;
            for (int i = 0; i < resultList.size(); i++) {
                if (resultList.get(i) instanceof Map) {
                    Map resultMap = (Map) resultList.get(i);
                    resultMap.put("CERT_NO", "这个是加密结果"); //取出相应的字段进行加密
                }
            }
        }
        return result;
    }

    public Object plugin(Object target) {
        System.out.println("this is the proceed ===>>" + target);
        return Plugin.wrap(target, this);
    }

    public void setProperties(Properties arg0) {
        System.out.println("this is the properties ===>>" + arg0);
    }
}
mybatis-config.xml

在mybatis配置文件中注册插件:

<plugins>
    <plugin interceptor="com.ceabox.interceptor.InterceptorForQry"></plugin>
</plugins>
测试输出
{ USR_ID=01000000000000000001, RES_FLD=null, CERT_NO=这个是加密结果, CERT_TYPE=0101, USR_REAL_NME=张三 }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值