mybatis在oracle中的分页扩展
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");
//PreparedStatementHandler handler = (PreparedStatementHandler) 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 );
if (offset > 0) {
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
}
else {
pagingSelect.append("select * from ( ");
}
pagingSelect.append(sql);
if (offset > 0) {
pagingSelect.append(" ) row_ ) where rownum_ <= " + limit + " and rownum_ > " + offset);
}
else {
pagingSelect.append(" ) where rownum <= " + limit);
}
if ( isForUpdate ) {
pagingSelect.append( " for update" );
} return pagingSelect.toString();
}
}
mybatis在oracle中的分页扩展的更多相关文章
- mybatis在mysql中的分页扩展
applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...
- Oracle中经典分页代码!
在Oracle中因为没有top关键字,所以在sqlserver中的分页代码并不适用于Oracle,那么在Oracle中如何来实现分页呢? --查询所有数据 STUNO STUNAME STUAGE S ...
- MyBatis在Oracle中插入数据并返回主键的问题解决
引言: 在MyBatis中,希望在Oracle中插入数据之时,同一时候返回主键值,而非插入的条数... 环境:MyBatis 3.2 , Oracle. Spring 3.2 SQL Snipp ...
- Mybatis调用Oracle中的存储过程和function
一.Mybatis调用存储过程 1 在数据库中创建以下的存储过程create or replace procedure pro_hello(p_user_name in varchar2,p_resu ...
- mybatis读取oracle中blob
controller: byte[] blob = commonService.getPersonImage(bean.getIdCard()); String base64 = new String ...
- 基于mybatis向oracle中插入数据的性能对比
数据库表结构: 逐条插入sql语句: <insert id="insert" parameterType="com.Structure"> INSE ...
- 对于Oracle中分页排序查询语句执行效率的比较分析
转自:http://bbs.csdn.net/topics/370033478 对于Oracle中分页排序查询语句执行效率的比较分析 作者:lzgame 在工作中我们经常遇到需要在Oracle中进行分 ...
- [数据库]Oracle和mysql中的分页总结
Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...
- 工作经验:mybatis 处理 oracle Long 类型
前言:mybatis 接收 oracle 中 LONG 类型的,报错:无效的列类型: getCLOB not implemented for class oracle.jdbc.driver.T4CL ...
随机推荐
- C#一些代码小结--文件对话框
C# 一些代码小结--文件对话框 查看文件完整路径 try { Config cfg = new Config(); var file = ""; if (saveFileDial ...
- CefSharp禁止弹出新窗体,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接
1.实现ILifeSpanHandler接口,代码如下: using CefSharp; using CefSharp.WinForms; using System; using System.Col ...
- UCS2-little endian转码(utf16)
public static void readFile(){ BufferedReader in = null; try { in = new BufferedReader(new InputStre ...
- 1000. Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- CodeChef TWOROADS(计算几何+拉格朗日乘数法)
题面 传送门 简要题意:给出\(n\)个点,请求出两条直线,并最小化每个点到离它最近的那条直线的距离的平方和,\(n\leq 100\) orz Shinbokuow 前置芝士 给出\(n\)个点,请 ...
- 点分治&&动态点分治学习笔记
突然发现网上关于点分和动态点分的教程好像很少……蒟蒻开篇blog记录一下吧……因为这是个大傻逼,可能有很多地方写错,欢迎在下面提出 参考文献:https://www.cnblogs.com/LadyL ...
- poj3624 Charm Bracelet
http://poj.org/problem?id=3624 题目大意:贝茜去了商场的珠宝店,发现了一个迷人的手镯.当然,她想装最好的魅力(N(1≤N≤3402)可用的魅力).每个魅力提供的列表中都有 ...
- mariadb审计日志通过 logstash导入 hive
我们使用的 mariadb, 用的这个审计工具 https://mariadb.com/kb/en/library/mariadb-audit-plugin/ 这个工具一点都不考虑后期对数据的处理, ...
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
如果你运行如下代码: 1 2 3 4 Integer a = 1000, b = 1000; System.out.println(a == b);//1 Integer c = 100, d = ...
- jQuery.extend 函数
jQuery.extend 函数详解 JQuery的extend扩展方法: Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解. 一.J ...