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

网友投稿 355 2022-12-04


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")YKeOiWaFnj;

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 params = (Map)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;

impohttp://rt java.util.Map;

import org.apache.ibatis.annotations.Mapper;

@Mapper

public interface TestMapper {

public List> findByPager(Map params);

public long count();

}

TestMapper.xml

select * from p1project.sys_user

select count(1) from p1projehttp://ct.sys_user

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用Maven打包异常
下一篇:浅谈对Java双冒号::的理解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~