Mybatis基于结果集的拦截

mybatis基于结果集的拦截处理实际使用场景较少,因为拦截了结果集再进行修改替换属于比较单一的场景

package com.dlh.sundoctor.intercepter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dlh.sundoctor.model.entity.User;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author 
 * @date 2022/3/4 11:18
 */
@Intercepts({ 
        @Signature(type = Executor.class, method = "query", 
            args = { MappedStatement.class, 
                    Object.class,
                    RowBounds.class, 
                    ResultHandler.class 
            }) 
})
public class MybatisInterceptor implements Interceptor {
    
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //执行请求方法,并将所得结果保存到result中
        Object result = invocation.proceed(); 
        if (result instanceof ArrayList) {
            ArrayList resultList = (ArrayList) result;
            for (int i = 0; i < resultList.size(); i++) {
                if (resultList.get(i) instanceof User) {
                    User user = (User) resultList.get(i);
                    JSONObject jsonObject = JSON.parseObject(user.getUserJSONInfo());
                    List<Map<String,Object>> maps = (List<Map<String,Object>>) jsonObject.get("listInfo");
                    for (Map<String, Object> map : maps) {
                        String link = (String) map.get("link");
                        map.put("link",link+"?sign=153100");
                    }
                    jsonObject.put("listInfo",maps);
                    ipcw.setUserJSONInfo(JSON.toJSONString(jsonObject));
                }
            }
        }
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }
}
package com.dlh.sundoctor.config;

import com.dlh.sundoctor.intercepter.MybatisInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 
 * @date 2022/3/4 14:12
 */
@Configuration
public class MybatisInterceptorConfig {

    @Bean
    public String myInterceptor(SqlSessionFactory sqlSessionFactory) {
        sqlSessionFactory.getConfiguration().addInterceptor(new MybatisInterceptor());
        return "interceptor";
    }

}
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.dlh.sundoctor.intercepter.MybatisInterceptor" />
    </plugins>
</configuration>

最后在application.properties配置文件里配置 mybatis的配置文件即可

mybatis.configLocation = classpath:mybatis/mybatis-config.xml

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值