栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

目录

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

1、修改返回值

2、创建【CamelHumpInterceptor】 文件


1、修改返回值



    
        select * from product
        
            where 1=1
        
        
        
            and productName like "%${productName}%"
        
        
        
            and productType="${productType}"
        
        
        
            and productColor="${productColor}"
        
    
    
        update product set productTitle = "${productTitle}" where id=#{id}
    

2、创建【CamelHumpInterceptor】 文件
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 {
    
    @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;
    }

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

    
    @Override
    public void setProperties(Properties properties) {
        System.out.println("参数0:"+properties.get("prop0"));
        System.out.println("参数1:"+properties.get("prop1"));
    }
}

执行返回效果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/888397.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号