84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
闭关修行中......
用MyBatis结果集拦截器做过这样一个需求:
MyBatis
由于项目需求经常变动,项目MySQL数据库都是存放JSON字符串,例如:用户的基本信息随着版本升级可能会有变动
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;
数据:
使用MyBatis拦截器对ResultSetHandler接口的handleResultSets方法进行拦截:
ResultSetHandler
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 resultList = new ArrayList(); if(resultSet != null && resultSet.next()) { Account account = new Account(); // infos字段 String infos = resultSet.getString("infos"); // 判断是否为空 if(StringUtils.isNotBlank(infos)) { // fastjson泛型反序列化 Map infMap = JSON.parseObject(infos, new TypeReference(){}); 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配置文件中注册插件:
Account account = accountDao.get(1L); // {sex=男, region_city=深圳市, region_area=, nickName=Jan, region_country=中国, signature=只要不放弃就还有机会!, region_province=广东省} System.out.println(account.getInfos());
用Mybatis实现过如下需求
Mybatis
查询用户基本信息表,查询结果返回是List>的结果集,对其中的某个字段进行加密
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='用户基础信息表';
使用Mybatis对Executor.class的'qurey'方法进行拦截,并对返回结果集进行处理
Executor.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); } }
{ USR_ID=01000000000000000001, RES_FLD=null, CERT_NO=这个是加密结果, CERT_TYPE=0101, USR_REAL_NME=张三 }
用
MyBatis
结果集拦截器做过这样一个需求:数据表
数据:
JSONPlugin拦截器
使用
MyBatis
拦截器对ResultSetHandler
接口的handleResultSets
方法进行拦截:mybatis-config.xml
在mybatis配置文件中注册插件:
测试输出
用
Mybatis
实现过如下需求数据表
数据
使用
Mybatis
对Executor.class
的'qurey'
方法进行拦截,并对返回结果集进行处理mybatis-config.xml
在mybatis配置文件中注册插件:
测试输出