Springboot如何使用mybatis实现拦截SQL分页

新建一个类MyPageInterceptor.java(注意在springboot中要添加注解@Component)

package com.grand.p1upgrade.mapper.test;

import java.sql.Connection;
import java.util.Map;
import java.util.Properties;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
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.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

@Component
@Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class,Integer.class})})
public class MyPageInterceptor implements Interceptor {
  
  private int page;
  private int size;
  @SuppressWarnings("unused")
  private String dbType;
 
  @SuppressWarnings("unchecked")
  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    System.out.println("plugin is running...");
    StatementHandler statementHandler = (StatementHandler)invocation.getTarget();
    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    while(metaObject.hasGetter("h")){
      Object object = metaObject.getValue("h");
      metaObject = SystemMetaObject.forObject(object);
    }
    while(metaObject.hasGetter("target")){
      Object object = metaObject.getValue("target");
      metaObject = SystemMetaObject.forObject(object);
    }
    MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement");
    String mapId = mappedStatement.getId();
    if(mapId.matches(".+ByPager$")){
      ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler");
      Map<String, Object> params = (Map<String, Object>)parameterHandler.getParameterObject();
      page = (int)params.get("page");
      size = (int)params.get("size");
      String sql = (String) metaObject.getValue("delegate.boundSql.sql");
      sql += " limit "+(page-1)*size +","+size;
      metaObject.setValue("delegate.boundSql.sql", sql);
    }
    return invocation.proceed();
  }
 
  @Override
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
 
  @Override
  public void setProperties(Properties properties) {
    String limit = properties.getProperty("limit","10");
    this.page = Integer.parseInt(limit);
    this.dbType = properties.getProperty("dbType", "mysql");
  }
 
}

添加测试TestMapper.java

package com.grand.p1upgrade.mapper.test;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestMapper {
  public List<Map<String,Object>> findByPager(Map<String, Object> params);
  public long count();
}

TestMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.grand.p1upgrade.mapper.test.TestMapper">

  <select id="findByPager" resultType="java.util.HashMap">
    select * from p1project.sys_user
  </select>
  <select id="count" resultType="long">
    select count(1) from p1project.sys_user
  </select>
</mapper>

在调用TestMapper.findByPager传递参数的时候将page和size传入即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。