Skip to content

8.插件支持

FuriousPws002 edited this page Apr 19, 2024 · 1 revision

代码分支:08-plugin

Mybatis插件通过动态代理实现,Invocation用于被代理的方法封装,PointcutDefinition用于切入点的定义,PointcutRegistry用于注册切入点,原生Mybatis采用Signature注解实现,自定义插件需要实现Interceptor接口,InterceptorChain则用于Mybatis中所有的插件拦截器的管理,通过调用pluginAll方法生成代理类,Plugin为InvocationHandler的实现

单元测试:

public class SqlSessionTest {
    @Test
    public void queryPointcut() {
        Configuration configuration = new Configuration();
        configuration.addInterceptor(new PageInterceptor());
        configuration.setDataSource(DataSourceBuilderTest.build());
        configuration.addMapper(UserMapper.class);
        SqlSession sqlSession = new DefaultSqlSession(configuration);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Long> idList = new ArrayList<>();
        idList.add(1L);
        idList.add(2L);
        List<UserDO> list = userMapper.listForeach(idList);
        Assert.assertNotNull(list);
        Assert.assertEquals(list.size(), 1);
    }
}
Clone this wiki locally