search

Home  >  Q&A  >  body text

java - 关于mybatis拦截器,有谁知道怎么对结果集进行拦截,将指定字段查询结果进行格式化

阿神阿神2803 days ago840

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:37:21

    Using the MyBatisresult set interceptor to meet such a requirement:

    Due to frequent changes in project requirements, the project MySQL database stores JSON strings. For example, the user's basic information may change with version upgrades

    Datasheet

    CREATE TABLE `account` (
      `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
      `infos` text NOT NULL COMMENT '用户JSON基本信息',
      `createTime` int(10) unsigned NOT NULL COMMENT '创建时间',
      `updateTime` int(10) unsigned NOT NULL COMMENT '更新时间',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    Data:

    id infos createTime updateTime
    1 {"nickName":"Jan","region_area":"","region_city":"Shenzhen City","region_country":"China","region_province":"Guangdong Province","sex":"Male" ,"signature":"As long as you don't give up, you still have a chance!"} 1467793564 1470795733

    JSONPlugin interceptor

    Use the MyBatis拦截器对ResultSetHandler接口的handleResultSets interceptor to intercept the handleResultSets method of the ResultSetHandler interface:

    package me.xuebai.modules.mybatis;
    
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.ibatis.executor.resultset.ResultSetHandler;
    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 com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.TypeReference;
    import me.xuebai.modules.entity.Account;
    
    @Intercepts({@Signature(
              type= ResultSetHandler.class,
              method = "handleResultSets",
              args = {Statement.class})})
    public class JSONPlugin implements Interceptor {
    
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            Object[] args = invocation.getArgs();
            // 获取到当前的Statement
            Statement stmt =  (Statement) args[0];
            // 通过Statement获得当前结果集
            ResultSet resultSet = stmt.getResultSet();
            List<Object> resultList = new ArrayList<Object>();  
            if(resultSet != null && resultSet.next()) {
                Account account = new Account();
                // infos字段
                String infos = resultSet.getString("infos");
                // 判断是否为空
                if(StringUtils.isNotBlank(infos)) {
                    // fastjson泛型反序列化
                    Map<String, Object> infMap = JSON.parseObject(infos, new TypeReference<Map>(){});
                    account.setInfos(infMap);
                    resultList.add(account);
                }
                // handleResultSets返回结果一定是一个List
                // size为1时,Mybatis会取第一个元素作为接口的返回值。  
                return resultList;
            }
            return invocation.proceed();
        }
    
        @Override
        public Object plugin(Object target) {
             return Plugin.wrap(target, this); 
        }
    
        @Override
        public void setProperties(Properties properties) {
        }
    
    }

    mybatis-config.xml

    Register the plugin in the mybatis configuration file:

    <plugins>
            <plugin interceptor="me.xuebai.modules.mybatis.JSONPlugin"></plugin>
    </plugins>

    Test output

    Account account = accountDao.get(1L);
    // {sex=男, region_city=深圳市, region_area=, nickName=Jan, region_country=中国, signature=只要不放弃就还有机会!, region_province=广东省}
    System.out.println(account.getInfos());

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:37:21

    Using Mybatis to achieve the following requirements

    Query the user’s basic information table. The query result returns a result set of List<Map<String,Object>>, and one of the fields is encrypted

    Datasheet

    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='用户基础信息表';

    Data

    USR_ID USR_REAL_NME CERT_TYPE CERT_NO RES_FLD
    01000000000000000001 Zhang San 0101 101222010199913291 (NULL)

    Use MybatisExecutor.class'qurey' to intercept the 'qurey' method of Executor.class and process the returned result set

    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

    Register the plugin in the mybatis configuration file:

    <plugins>
        <plugin interceptor="com.ceabox.interceptor.InterceptorForQry"></plugin>
    </plugins>

    Test output

    { USR_ID=01000000000000000001, RES_FLD=null, CERT_NO=这个是加密结果, CERT_TYPE=0101, USR_REAL_NME=张三 }

    reply
    0
  • Cancelreply