拦截器介绍

mybatis提供了@Intercepts注解允许开发者对mybatis的执行器Executor进行拦截。

Executor接口方法主要有update、query、commit、rollback等等。

主要思路为:

  1. 进入拦截器方法中
  2. 获取拦截器方法参数
  3. 获取解析参数及MappedStatement
  4. 从MappedStatement声明类中获取resultMap
  5. 获取resultMappings并且进行自定义
  6. 重新组装新的ResultMap
  7. 利用反射将新的ResultMap设置进入MappedStatement中

拦截器代码

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.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
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.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils; import java.lang.reflect.Field;
import java.util.*; @Component
@Intercepts({@org.apache.ibatis.plugin.Signature(type = Executor.class, method = "query",
args = {
MappedStatement.class,
Object.class,
RowBounds.class,
ResultHandler.class,
CacheKey.class,
BoundSql.class})})
public class MybatisInterceptorConfig implements Interceptor { /*重置ResultMapping*/
private List<ResultMapping> resetResultMap(List<ResultMapping> resultMaps){
//ResultMaps.add
return resultMaps;
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
resetResultMap(invocation);
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
return Plugin.wrap(o, this);
}
@Override
public void setProperties(Properties properties) { }
private void resetResultMap(Invocation invocation) {
final Object[] args = invocation.getArgs();
MappedStatement mappedStatement = (MappedStatement) args[5];
ResultMap resultMap = mappedStatement.getResultMaps().iterator().next();
if (resultMap != null) {
List<ResultMapping> resultMappings = resultMap.getResultMappings();
if (resultMappings != null && !(resultMappings instanceof AdjustedList)) {
List<ResultMapping> newResultMappings = this.resetResultMap(new AdjustedList(resultMappings));
ResultMap newResultMap = new ResultMap.Builder(mappedStatement.getConfiguration(), resultMap.getId(), resultMap.getType(), newResultMappings).build();
Field field = ReflectionUtils.findField(MappedStatement.class, "resultMaps");
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, mappedStatement, Collections.singletonList(newResultMap));
}
}
}
private class AdjustedList<E> extends ArrayList<E> {
public AdjustedList(Collection<? extends E> c) {
super(c);
}
}
}

mybatis - 基于拦截器修改执行语句中的ResultMap映射关系的更多相关文章

  1. mybatis - 基于拦截器修改执行中的SQL语句

    拦截器介绍 mybatis提供了@Intercepts注解允许开发者对mybatis的执行器Executor进行拦截. Executor接口方法主要有update.query.commit.rollb ...

  2. Struts2拦截器的执行过程浅析

    在学习Struts2的过程中对拦截器和动作类的执行过程一度陷入误区,特别读了一下Struts2的源码,将自己的收获分享给正在困惑的童鞋... 开始先上图: 从Struts2的图可以看出当浏览器发出请求 ...

  3. Mybatis自定义拦截器与插件开发

    在Spring中我们经常会使用到拦截器,在登录验证.日志记录.性能监控等场景中,通过使用拦截器允许我们在不改动业务代码的情况下,执行拦截器的方法来增强现有的逻辑.在mybatis中,同样也有这样的业务 ...

  4. mybatis Interceptor拦截器代码详解

    mybatis官方定义:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  5. Mybatis之拦截器原理(jdk动态代理优化版本)

    在介绍Mybatis拦截器代码之前,我们先研究下jdk自带的动态代理及优化 其实动态代理也是一种设计模式...优于静态代理,同时动态代理我知道的有两种,一种是面向接口的jdk的代理,第二种是基于第三方 ...

  6. MyBatis实现拦截器分页功能

    1.原理 在mybatis使用拦截器(interceptor),截获所执行方法的sql语句与参数. (1)修改sql的查询结果:将原sql改为查询count(*) 也就是条数 (2)将语句sql进行拼 ...

  7. Mybatis Interceptor 拦截器原理 源码分析

    Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的),由于插件会深入到Mybatis的核心,因此在编写自己的插件前最 ...

  8. Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】

    什么是拦截器 拦截器Interceptor-..拦截器是Struts的概念,它与过滤器是类似的-可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Struts为 ...

  9. mybatis定义拦截器

    applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...

随机推荐

  1. 自己用C语言写RH850 F1L serial bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 由于有了RH850 F ...

  2. vue路由--使用router.push进行路由跳转

    手机赚钱怎么赚,给大家推荐一个手机赚钱APP汇总平台:手指乐(http://www.szhile.com/),辛苦搬砖之余用闲余时间动动手指,就可以日赚数百元 route-link是在html中静态定 ...

  3. 《Java 8 in Action》Chapter 12:新的日期和时间API

    在Java 1.0中,对日期和时间的支持只能依赖java.util.Date类.同时这个类还有两个很大的缺点:年份的起始选择是1900年,月份的起始从0开始. 在Java 1.1中,Date类中的很多 ...

  4. AOP in .NET

    AOP in .NET AOP是所有现代OOP语言开发框架中的基础功能,随着Spring框架的普及,对于AOP的使用已经像喝水一样普通.可是知其然还要其所以然.本文将基于.NET环境探讨实现AOP的底 ...

  5. 《C# 6.0 本质论》 - 学习笔记

    <C# 6.0 本质论> ========== ========== ==========[作者] (美) Mark Michaelis (美) Eric Lippert[译者] (中) ...

  6. 使用vscode对threejs的本地调试

    始终坚信阅读和调试结合的方式,才是学习开源的高效方法 一.老版本调试 遥想当年的threejs(使用版本为r75)还没有CommonJS,对于我这个小白可以拿起大刀大杀四方......(咳.咳. 就是 ...

  7. MySQL基础(7) | 触发器

    MySQL基础(7) | 触发器 基本语法 创建 CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EA ...

  8. Linux学习Day1:开班第一天

    其实这篇博客应该昨天就要写完的,算是补作业吧. 昨天(2020年2月14日)是参加Linux线上培训的第一天,当天培训结束后,老师要求学员每天写一篇博客来记录自己学到的知识,于是就有了这篇博客的诞生. ...

  9. Ubuntu禁用root账号,开启Ubuntu密钥登录

    新建普通用户 ## 新建普通用户 $ adduser ubuntu $ apt-get install sudo ## 将用户加入sudo组 $ usermod -a -G sudo ubuntu 为 ...

  10. opencv —— saturate_cast 溢出保护

    src.at<uchar>(i,j)[0] = saturate_cast<uchar>(data); if (data < 0) data = 0; else if ( ...