mybatis在mysql中的分页扩展
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中的分页扩展的更多相关文章
- mybatis在oracle中的分页扩展
applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...
- [数据库]Oracle和mysql中的分页总结
Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...
- MySQL中的分页操作结合python
mysql中的分页操作结合python --分页: --方式1: ;-- 读取十行 , --从第十行读取 往后再读十行 --方式2: offset ; --从第二十行开始读取10行 -- 结合pyth ...
- MySQL中的空间扩展
目录 19.1. 前言 19.2. OpenGIS几何模型 19.2.1. Geometry类的层次 19.2.2. 类Geometry 19.2.3. 类Point 19.2.4. 类Curve 1 ...
- Mysql中的分页处理
先来说一下Mysql中limit的语法: --语法: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset --举例: selec ...
- MySQL中地理位置数据扩展geometry的使用心得
最近学习了些MySQL geometry数据存储和计算,在这里记录下. 1. 环境 geometry推荐在5.6版本以上使用,尽管大部分功能在5.5已经可用,除了距离计算函数st_distance等新 ...
- 优化 MySQL 中的分页
英文:Robert Eisele 译者:Giraffe 链接:http://yemengying.com/2016/05/28/optimized-pagiantion-mysql/ 一道面试的问题, ...
- PHP+MySQL中实现分页
你只需要在需要添加页的页面加入这几行代码 <?phpinclude 'form.class.php'; $p=new Page(100, 'Demo01.php');//这里需要传递两个参数,参 ...
- Mybatis对MySQL中BLOB字段的读取
1.在sqlMapConfig中,定义一个typeHandlers <typeHandlers> <typeHandler jdbcType="BLOB" jav ...
随机推荐
- 构建NetCore应用框架之实战篇(五):BitAdminCore框架1.0登录功能设计实现及源码
本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.设计原则 1.继承前面框架架构思维,设计以可读性作为首要目标. 2 ...
- Kubernetes性能测试实践
本文由 网易云 发布. 概述 随着容器技术的发展,容器服务已经成为行业主流,然而想要在生产环境中成功部署和操作容器,关键还是容器编排技术.市场上有各种各样的容器编排工具,如Docker原生的Swar ...
- Day 29 _模块二 -hashlib_configparse_logging
一.hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数 ...
- OpenStack 数据库操作 demo
#!/usr/bin/env python from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine f ...
- 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- VC API常用函数简单例子大全(1-89)
第一个:FindWindow根据窗口类名或窗口标题名来获得窗口的句柄,该函数返回窗口的句柄 函数的定义:HWND WINAPI FindWindow(LPCSTR lpClassName ,LPCST ...
- python django 更改模型字段出错时的一个解决办法
python/django 框架自带的 orm 无疑是django框架最拿得出手的一个亮点,orm无疑极大的方便了项目的开发,提高了开发的效率. 在实际的项目开发过程中,我们有时候需要修改模型的字段, ...
- jzoj5996
我們可以枚舉每一個串的最短回文後綴,這樣一定不會算重. 雖然一個字符串可能會有多個回文後綴,但是答案只會在最短的後綴被計算 記f[i]表示長度為i回文串中,沒有長度>1的回文後綴的個數,將總個數 ...
- elasticsearch Geo Bounding Box Query
Geo Bounding Box Query 一种查询,允许根据一个点位置过滤命中,使用一个边界框.假设以下索引文档: PUT /my_locations { "mappings" ...
- JSON.stringify和JSON.parse的使用
JSON.stringify 函数 (JavaScript)将 JavaScript 值转换为 JavaScript 对象表示法 (Json) 字符串.JSON.stringify(value [, ...