java - 关于mybatis拦截器,有谁知道怎么对结果集进行拦截,将指定字段查询结果进行格式化
阿神
阿神 2017-04-18 10:35:21
0
2
854
阿神
阿神

闭关修行中......

membalas semua(2)
小葫芦

Menggunakan MyBatis pemintas set hasil untuk memenuhi keperluan sedemikian:

Disebabkan perubahan yang kerap dalam keperluan projek, pangkalan data MySQL projek menyimpan rentetan JSON Sebagai contoh, maklumat asas pengguna mungkin berubah dengan peningkatan versi

Helaian data

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":"深圳市","region_country":"中国","region_province":"广东省","sex":"男","signature":"只要不放弃就还有机会!"} 1467793564 1470795733

Pemintas JSONPlugin

Gunakan pemintas MyBatis untuk memintas kaedah ResultSetHandler antara muka handleResultSets:

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

Daftar pemalam dalam fail konfigurasi mybatis:

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

Keluaran ujian

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

Menggunakan Mybatis untuk mencapai keperluan berikut

Soal jadual maklumat asas pengguna Hasil pertanyaan mengembalikan set hasil Senarai<Peta<String,Objek>>, dan salah satu medan disulitkan

Helaian data

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 张三 0101 101222010199913291 (NULL)

Gunakan Mybatis untuk memintas Executor.class kaedah 'qurey' dan memproses set hasil yang dikembalikan

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

Daftar pemalam dalam fail konfigurasi mybatis:

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

Keluaran ujian

{ USR_ID=01000000000000000001, RES_FLD=null, CERT_NO=这个是加密结果, CERT_TYPE=0101, USR_REAL_NME=张三 }
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan