MyBatis--拦截器实现分页

本文介绍了MyBatis的四大对象和插件原理,重点讲解了如何开发MyBatis插件,以实现分页功能。通过定义MyFirstInterceptor并配置插件,展示了分页插件PageHelper的使用步骤,包括在pom.xml添加依赖,mybatis-config.xml的配置,以及代码中的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要内容:


MyBatis四大对象及插件原理:


MyBatis插件开发:

测试:定义一个插件类:

MyFirstInterceptor :一定继承自Interceptor类,重写里面的方法

package com.mooc.mybatis.interceptor;

import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.sql.Statement;
import java.util.Properties;

/**
 * 插件签名,告诉mybatis单钱插件用来拦截那个对象的哪个方法
 */
@Intercepts({
        @Signature(type = ResultSetHandler.class,method ="handleResultSets",args = Statement.class)

})
public class MyFirstInterceptor implements Interceptor {

    //拦截目标对象的目标方法
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("first plugin Intercept :"+invocation.getTarget());
        System.out.println(invocation.getMethod());
        System.out.println(invocation.getArgs());

        Object object=invocation.proceed();
        return object;
    }

    //包装目标对象 为目标对象创建代理对象的
    public Object plugin(Object o) {
        System.out.println("first plugin :"+o);
        return Plugin.wrap(o,this);
    }

    public void setProperties(Properties properties) {
        System.out.println("first plugin :"+properties);

    }
}

在配置文件对新创建的插件类进行配置:

	<plugins>
		<!--<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>-->

		<plugin interceptor="com.mooc.mybatis.interceptor.MyFirstInterceptor">
			<property name="hello" value="world"></property>
		</plugin>

		<plugin interceptor="com.mooc.mybatis.interceptor.MySecondInterceptor">
		</plugin>

	</plugins>

插件签名:

/**
 * 插件签名,告诉mybatis单钱插件用来拦截那个对象的哪个方法
 */
@Intercepts({
        @Signature(type = ResultSetHandler.class,method ="handleResultSets",args = Statement.class)

})

多插件开发详解:


分页原理:


PageHelper:

github地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/en/HowToUse.md

使用PageHelper:在maven里

步骤1、pom.xml添加依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>x.x.x</version>
</dependency>

步骤2、修改mybatis-config.xml配置文件

<!-- 
    In the configuration file, 
    plugins location must meet the requirements as the following order:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- config params as the following -->
        <property name="param1" value="value1"/>
	</plugin>
</plugins>

步骤3、在代码中使用:有多种方式,建议使用这种

//2. use static method startPage
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectIf(1);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值