xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
package com.monkeylessey.framework.plugins;
 
import com.monkeylessey.framework.config.MybatisPlusConfig;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
 
/**
 * mybatis插件demo
 * 针对自己写的mqpper方法才会走这个插件,plus内置方法要想使用插件,只能用plus的插件
 * @see MybatisPlusPluginDemo plus的插件写法
 * @see MybatisPlusConfig  如何配置 line 46 & 56
 *
 * @author xp
 * @data 2023/11/14
 */
@Intercepts(
        @Signature(
                type = Executor.class,
                method = "query",
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )
)
public class MybatisPluginDemo implements Interceptor {
 
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 执行之前
        System.out.println("mybatis插件执行之前");
        // 执行sql
        Object result = invocation.proceed();
        // 执行之后
        System.out.println("mybatis插件执行之后");
        return result;
    }
}