java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot mybatis

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

作者:理舞

这篇文章主要介绍了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传入即可

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

您可能感兴趣的文章:
阅读全文