前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis快速入门——第六章、MyBatis拦截器接口

MyBatis快速入门——第六章、MyBatis拦截器接口

作者头像
红目香薰
发布2022-11-30 15:56:39
2280
发布2022-11-30 15:56:39
举报
文章被收录于专栏:CSDNToQQCode

MyBatis快速入门——第六章、MyBatis拦截器接口

承接上文的程序

1、添加配置

代码语言:javascript
复制
<!-- 配置拦截器插件,一定要在别名的下面 -->
<plugins>
    <plugin interceptor="com.item.interceptor.MyBatisInterceptor">
        <property name="prop0" value="BigDateGood"/>
        <property name="prop1" value="BigDateSuper"/>
    </plugin>
</plugins>

2、配置拦截器

创建【com.item.interceptor】包

创建【MybatisInterceptor】类,并继承【Interceptor】接口

代码语言:javascript
复制
package com.item.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;

import java.lang.reflect.Method;
import java.util.Properties;

@Intercepts({
        @Signature(
                type = Executor.class,//指定要拦截的接口
                method = "update",//设置拦截接口中的方法名
                args = {MappedStatement.class,Object.class}//设置拦截方法的参数类型数组
        )
})
public class MyBatisInterceptor implements Interceptor {
    /**
     * 获取被拦截器拦截对象的相关信息
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //拦截目标
        Object target = invocation.getTarget();
        System.out.println("目标:"+target);
        //获取被拦截对象执行的方法
        Method method = invocation.getMethod();
        System.out.println("方法:"+method.getName());
        //获取执行目标的中参数
        Object[] args = invocation.getArgs();
        for (Object o : args) {
            System.out.println("参数:"+o);
        }
        Object proceed = invocation.proceed();
        System.out.println("继续:"+proceed);
        return proceed;
    }

    /**
     * 获取当前拦截对象
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        System.out.println("当前拦截对象"+target);
        return Plugin.wrap(target,this);
    }

    /**
     * 获取拦截器插件中的参数值
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        System.out.println("参数0:"+properties.get("prop0"));
        System.out.println("参数1:"+properties.get("prop1"));
    }
}

修改访问测试:【http://localhost:8080/UpdateById

查询访问测试:【http://localhost:8080/GetInfo

可以看到两种访问都被拦截到了。

代码语言:javascript
复制
当前拦截对象org.apache.ibatis.executor.CachingExecutor@62279478
当前拦截对象org.apache.ibatis.scripting.defaults.DefaultParameterHandler@4139cac7
当前拦截对象org.apache.ibatis.executor.resultset.DefaultResultSetHandler@5d31611c
当前拦截对象org.apache.ibatis.executor.statement.RoutingStatementHandler@6a49ef9a

ParameterHandler :处理 SQL 的参数对象。

ResultSetHandler :处理 SQL 的返回结果集。

StatementHandler :数据库的处理对象,用于执行 SQL 语句 。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MyBatis快速入门——第六章、MyBatis拦截器接口
  • 1、添加配置
  • 2、配置拦截器
  • 修改访问测试:【http://localhost:8080/UpdateById】
  • 查询访问测试:【http://localhost:8080/GetInfo】
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档