手写mybatis插件之分页查询

yml文件

server:
  port: 8081


mybatis:
  mapper-locations: classpath:mapper/*.xml
  config-location: classpath:mybatis-config.xml


spring:
  datasource:
    password: 1234
    username: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&serverTimezone=UTC

mybatis配置文件



<?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>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <plugins>
        <plugin interceptor="com.dls.config.PageInterceptor"></plugin>
    </plugins>




</configuration>

分页类

package com.dls.config;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Page {
    Integer page;
    Integer size;
}


分页工具类(保存分页数据)

package com.dls.config;

public class PageUtil {

    private static ThreadLocal<Page> PAGE_THREADLOCAL = new ThreadLocal<Page>();

    public static void setPage(Integer page,Integer size) {
        Page page1 = new Page(page, size);
        PAGE_THREADLOCAL.set(page1);
    }

    public static Page getPage() {
        return PAGE_THREADLOCAL.get();
    }

    public static void remove() {
        PAGE_THREADLOCAL.remove();
    }
}

分页插件类

package com.dls.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;

@Intercepts(
        @Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class,Integer.class})
)
@Slf4j
public class PageInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Page page = PageUtil.getPage();
        if (page == null) {
            return invocation.proceed();
        }
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        BoundSql boundSql = statementHandler.getBoundSql();
        String sql = boundSql.getSql();
        String limit = String.format(" limit %s,%s",(page.getPage() - 1) * page.getSize(),page.getSize());
        sql += limit;

        Field field = boundSql.getClass().getDeclaredField("sql");
        field.setAccessible(true);
        field.set(boundSql,sql);
        field.setAccessible(false);

        Object proceed = invocation.proceed();
        PageUtil.remove();
        return proceed;
    }

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

    @Override
    public void setProperties(Properties properties) {

    }
}

设置分页参数
在这里插入图片描述

控制台sql信息发现分页插件已经生效

==>  Preparing: select * from student limit 0,3
==> Parameters: 
<==    Columns: id, password, username
<==        Row: 1, 660779, dls
<==        Row: 2, 2, 9
<==        Row: 3, 666, 777
<==      Total: 3
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值