xuekang
2024-05-13 15a0280ae9e7db96fdf0744c722d214d2cb5a0e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.nuvole.four.config.mybatisInt;//package com.nuvole.shop.config.mybatisInt;
//
//import com.nuvole.util.AESUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.binding.MapperMethod;
//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 org.springframework.stereotype.Component;
//
//import java.util.Properties;
//
///**
// * @ClassName MySqlInterceptor
// * @Author cy
// * @Date 2022/12/13
// * @Description
// * @Version 1.0
// **/
//@Slf4j
//@Intercepts({@Signature(type = Executor.class, method = "query",
//        args = {MappedStatement.class,
//                Object.class,
//                RowBounds.class,
//                ResultHandler.class})})
//@Component
//public class MySqlInterceptor implements Interceptor {
////    @Autowired
////    private ActivityDetailHeadBandCoupDetailService activityDetailHeadBandCoupDetailService;
//
//    @Override
//    /**
//     * intercept 方法用来对拦截的 sql 进行具体的操作
//     * 本拦截方法只对 mapper 接口中的 searchByQuery 方法进行拦截,实际每个方法都拦截了,
//     * 只是只有 searchByQuery 方法时,才真正执行 拦截的相关操作
//     * @param invocation 拦截器执行器
//     * @return
//     * @throws Throwable 异常信息
//     */
//    public Object intercept(Invocation invocation) throws Throwable {
//        //  logger.info("执行intercept方法:{}", invocation.toString());
//        //  获取 invocation 传递的参数
//        Object[] args = invocation.getArgs();
//
//        MappedStatement ms = (MappedStatement) args[0];
//        // 获取执行的该拦截器的全路径方法 比如你的 UserInfoMapper 接口的 getById 方法, com.xx.UserInfoMapper.getById
//        String id = ms.getId();
//        // 如果不是 searchByQuery 方法,就不进行相关的拦截操作
//        Object proceed = invocation.proceed();
//        if (!filterMethodById(id)) {
//            return proceed;
//        }
//
//        // 该参数类型 org.apache.ibatis.binding.MapperMethod$ParamMap
//        Object parameterObject = args[1];
//        // 获取传递的参数, 主要包含三个参数 1. 查询语句 searchQuery, 2. 查询偏移 pageOffset, 3. 查询每页的数据条数
//        MapperMethod.ParamMap paramMap = (MapperMethod.ParamMap) parameterObject;
//
//        Long memberId = null;
//        String cardNo = null;
//        if (paramMap.containsKey("memberId")) {
//            memberId = (Long) paramMap.get("memberId");
//        }
//        if (paramMap.containsKey("cardNo")) {
//            cardNo = AESUtil.decode((String) paramMap.get("cardNo"));
//        }
////        activityDetailHeadBandCoupDetailService.doGetGiveCoupon(memberId, cardNo);
//        log.info("拦截了 " + id + " 相关执行。");
//        return proceed;
//    }
//
//    @Override
//    /***
//     * 定义拦截的类 Executor、ParameterHandler、StatementHandler、ResultSetHandler当中的一个
//     * @param target 需要拦截的类
//     * @return
//     */
//    public Object plugin(Object target) {
//        if (target instanceof Executor) {
//            return Plugin.wrap(target, this);
//        }
//        return target;
//    }
//
//    @Override
//    /**
//     * 属性相关操作
//     * 设置和自定义属性值
//     * @param properties 属性值
//     */
//    public void setProperties(Properties properties) {
//        // 获取属性
//        // String value1 = properties.getProperty("prop1");
//    }
//
//
//    /**
//     * 根据获取到执行 id 找到对应的方法,只在 insertSelective 方法上执行拦截
//     *
//     * @param id 根据 MappedStatement 获取到的 id 属性
//     * @return 是否是 insertSelective 方法
//     */
//    private boolean filterMethodById(String id) {
//        System.out.println("id: " + id);
//        return id.contains("com.nuvole.shop.mapper.extend.StoreMemberBankcardMapper.insertSelective");
//    }
//
//    private void headBandGiveCoup(Long memberId) {
//
//    }
//
//}