applicationContext.xml

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml" />
</bean>

mybatis-config.xml

<?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>
<!-- 设置成 true就可以启动缓存,一般的修改要1分钟才能生效(因为设置的flushInterval="60000" )
如果是在一个xxxxMapper.xml里面修改的数据库的数据,就直接清空本mapper的缓存 -->
<setting name="cacheEnabled" value="false"/>
<setting name="useGeneratedKeys" value="false"/>
</settings>
<plugins>
<plugin interceptor="com.system.util.DiclectStatementHandlerInterceptor" />
<plugin interceptor="com.system.util.DiclectResultSetHandlerInterceptor" />
</plugins>
</configuration>

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();
}
}

  

mybatis在mysql中的分页扩展的更多相关文章

  1. mybatis在oracle中的分页扩展

    applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...

  2. [数据库]Oracle和mysql中的分页总结

    Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...

  3. MySQL中的分页操作结合python

    mysql中的分页操作结合python --分页: --方式1: ;-- 读取十行 , --从第十行读取 往后再读十行 --方式2: offset ; --从第二十行开始读取10行 -- 结合pyth ...

  4. MySQL中的空间扩展

    目录 19.1. 前言 19.2. OpenGIS几何模型 19.2.1. Geometry类的层次 19.2.2. 类Geometry 19.2.3. 类Point 19.2.4. 类Curve 1 ...

  5. Mysql中的分页处理

    先来说一下Mysql中limit的语法: --语法: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset --举例: selec ...

  6. MySQL中地理位置数据扩展geometry的使用心得

    最近学习了些MySQL geometry数据存储和计算,在这里记录下. 1. 环境 geometry推荐在5.6版本以上使用,尽管大部分功能在5.5已经可用,除了距离计算函数st_distance等新 ...

  7. 优化 MySQL 中的分页

    英文:Robert Eisele 译者:Giraffe 链接:http://yemengying.com/2016/05/28/optimized-pagiantion-mysql/ 一道面试的问题, ...

  8. PHP+MySQL中实现分页

    你只需要在需要添加页的页面加入这几行代码 <?phpinclude 'form.class.php'; $p=new Page(100, 'Demo01.php');//这里需要传递两个参数,参 ...

  9. Mybatis对MySQL中BLOB字段的读取

    1.在sqlMapConfig中,定义一个typeHandlers <typeHandlers> <typeHandler jdbcType="BLOB" jav ...

随机推荐

  1. C# 对象引擎,以路径形式访问对象属性(data.Product[1].Name)

    对象引擎,以路径形式访问对象属性,例data.Product[1].Name. 在做excel模板引擎的时候,为了能方便的调用对象属性,找了一些模板引擎,不是太大就是不太适用于excel, 因为exc ...

  2. Regularjs是什么

    本文由作者郑海波授权网易云社区发布. 此文摘自regularjs的指南, 目前指南正在全面更新, 把老文档的[接口/语法部分]统一放到了独立的 Reference页面. Regularjs是基于动态模 ...

  3. Flask的WTforms

    一.简单介绍 WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 类似于Django中的modelform 安装: pip3 install wtforms 二.简 ...

  4. 输出图中顶点i到顶点j之间的所有简单路径

    简单路径(不包括环) DFS遍历以及回溯得到结果 void dfs(ALGraph graph, int v, int end, bool visit[], int path[], int cnt) ...

  5. PHP中操作mysql的函数

    (1)mysql_num_rows(),对应select操作,获取select结果中数据集的行数:(2)mysql_affected_rows(),对应update.delete.insert操作,影 ...

  6. 【北京】安全研究员/工程师-20-35K,人体工程学座椅坐等你来~

    招聘地点:北京市西城区 薪资:20-35K 人体工程学座椅,高大上环境坐等你来 [招聘]安全研究员+安全工程师-北京数字观星科技有限公司 北京数字观星科技有限公司成立于2016年9月29日,是一家致力 ...

  7. 智能卡操作系统COS概述

    随着IC卡从简单的同步卡发展到异步卡,从简单的EPROM卡发展到内带微处理器的智能卡(又称CPU卡),对IC卡的各种要求越来越高.而卡本身所需要的各种管理工作也越来越复杂,因此就迫切地需要有一种工具来 ...

  8. 从码农升为PM(节约成本)

    做为一个码农的潜规则,用户怎么要求怎么写,不论过程只论是否符合要求以及减少bug的存在,虽然bug随时会出现,这就是码农,一直以来都说码农分很多种但个人认为就是一种,原因是码农不懂的换位思考,不懂的在 ...

  9. 【NOIP2017】逛公园 最短路+DP

    诶,去年场上不会处理$0$的环,只拿了$60$有点可惜. 我们先不管边边权为$0$的边. 我们先跑一次最短路,令$dis[u]$表示从$1$至$u$的最短路的长度. 那么根据题目的要求,从起点走到$u ...

  10. WebDriverAPI(5)

    将当前浏览器截屏 测试网址 http://www.baidu.com Java语言版本实例 @Test public void captureScreenInCurrentWindows() { dr ...