mybatis中MySQL的分页_mybatis在mysql中的分页扩展

applicationContext.xml

mybatis-config.xml

DiclectResultSetHandlerInterceptor.java

package com.system.util;

import java.sql.Statement;

import java.util.Properties;

import org.apache.ibatis.executor.resultset.FastResultSetHandler;

import org.apache.ibatis.executor.resultset.NestedResultSetHandler;

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

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.session.RowBounds;

@Intercepts({ @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = { Statement.class }) })

public class DiclectResultSetHandlerInterceptor implements Interceptor {

public Object intercept(Invocation invocation) throws Throwable {

FastResultSetHandler resultSet = (FastResultSetHandler) invocation.getTarget();

if(!(resultSet instanceof NestedResultSetHandler)) {

RowBounds rowBounds = (RowBounds) ReflectUtil.getClassField(resultSet, "rowBounds");

if (rowBounds.getLimit() > 0 && rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT) {

ReflectUtil.setClassField(resultSet, "rowBounds", new RowBounds());

}

}

return invocation.proceed();

}

public Object plugin(Object target) {

return Plugin.wrap(target, this);

}

public void setProperties(Properties properties) {}

}

DiclectStatementHandlerInterceptor.java

package com.system.util;

import java.sql.Connection;

import java.util.Properties;

import org.apache.ibatis.executor.statement.PreparedStatementHandler;

import org.apache.ibatis.executor.statement.RoutingStatementHandler;

import org.apache.ibatis.executor.statement.StatementHandler;

import org.apache.ibatis.mapping.BoundSql;

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.session.RowBounds;

@Intercepts( { @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })

public class DiclectStatementHandlerInterceptor implements Interceptor {

public Object intercept(Invocation invocation) throws Throwable {

RoutingStatementHandler statement = (RoutingStatementHandler) invocation.getTarget();

StatementHandler handler = (StatementHandler) ReflectUtil.getClassField(statement, "delegate");

if (handler instanceof PreparedStatementHandler){

RowBounds rowBounds = (RowBounds) ReflectUtil.getSuperClassField(handler, "rowBounds");

if (rowBounds.getLimit() > 0 && rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT) {

BoundSql boundSql = statement.getBoundSql();

String sql = boundSql.getSql();

sql = getLimitString(sql, rowBounds.getOffset(), rowBounds.getLimit());

ReflectUtil.setClassField(boundSql, "sql", sql);

}

}

return invocation.proceed();

}

public Object plugin(Object target) {

return Plugin.wrap(target, this);

}

public void setProperties(Properties properties) {

}

public String getLimitString(String sql, int offset, int limit) {

limit = offset+limit;

sql = sql.trim();

boolean isForUpdate = false;

if ( sql.toLowerCase().endsWith(" for update") ) {

sql = sql.substring( 0, sql.length()-11 );

isForUpdate = true;

}

StringBuffer pagingSelect = new StringBuffer( sql.length()+100 );

pagingSelect.append(sql);

pagingSelect.append(" limit "+offset+","+limit);

if ( isForUpdate ) {

pagingSelect.append( " for update" );

}

return pagingSelect.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值