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

举报
红目香薰 发表于 2022/05/27 15:29:57 2022/05/27
【摘要】 ​ ​编辑MyBatis快速入门——第六章、MyBatis拦截器接口(二)目录MyBatis快速入门——第六章、MyBatis拦截器接口(二)1、修改返回值2、创建【CamelHumpInterceptor】 文件1、修改返回值<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD...

 编辑

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

目录

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

1、修改返回值

2、创建【CamelHumpInterceptor】 文件



1、修改返回值

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.item.mapper.ProductMapper">
    <select id="GetInfo" resultType="java.util.Map">
        select * from product
        <if test="productName!=null or productType!=null or productColor!=null ">
            where 1=1
        </if>
        <!-- 模糊查询 -->
        <if test="productName!=null">
            and productName like "%${productName}%"
        </if>
        <!-- 类型筛选 -->
        <if test="productType!=null">
            and productType="${productType}"
        </if>
        <!-- 颜色筛选 -->
        <if test="productColor!=null">
            and productColor="${productColor}"
        </if>
    </select>
    <update id="UpdateById">
        update product set productTitle = "${productTitle}" where id=#{id}
    </update>
</mapper>

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 {
    /**
     * 获取被拦截器拦截对象的相关信息
     * @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"));
    }
}

执行返回效果:

编辑


【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。