Mybatis-分頁攔截器
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u010741376/article/details/47722429
package com.mgear.samering.util;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
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.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSONObject;
/**
* Mybatis - 分頁攔截器
*
* @author c.c.
*/
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }),
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = { Statement.class }) })
public class PageInterceptor implements Interceptor {
private static final Logger logger = Logger.getLogger(PageInterceptor.class);
public static final ThreadLocal<Page> localPage = new ThreadLocal<Page>();
private static Page mypage;
/**
* 開始分頁
* @param pageNum
* @param pageSize
*/
public static void startPage(int pageNum, int pageSize) {
localPage.set(new Page(pageNum, pageSize));
}
/**
* 結束分頁並返回結果,該方法必須被呼叫,否則localPage會一直儲存下去,直到下一次startPage
* @return
*/
public static Page endPage() {
// Page page = localPage.get();
// localPage.remove();
return mypage;
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
if (localPage.get() == null) {
return invocation.proceed();
}
if (invocation.getTarget() instanceof StatementHandler) {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
// 分離代理物件鏈(由於目標類可能被多個攔截器攔截,從而形成多次代理,通過下面的兩次迴圈
// 可以分離出最原始的的目標類)
while (metaStatementHandler.hasGetter("h")) {
Object object = metaStatementHandler.getValue("h");
metaStatementHandler = SystemMetaObject.forObject(object);
}
// 分離最後一個代理物件的目標類
while (metaStatementHandler.hasGetter("target")) {
Object object = metaStatementHandler.getValue("target");
metaStatementHandler = SystemMetaObject.forObject(object);
}
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
//分頁資訊if (localPage.get() != null) {
Page page = localPage.get();
BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
// 分頁引數作為引數物件parameterObject的一個屬性
String sql = boundSql.getSql();
// 重寫sql
String pageSql = buildPageSql(sql, page);
//重寫分頁sql
metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
Connection connection = (Connection) invocation.getArgs()[0];
// 重設分頁引數裡的總頁數等
setPageParameter(sql, connection, mappedStatement, boundSql, page);
// 將執行權交給下一個攔截器
return invocation.proceed();
} else if (invocation.getTarget() instanceof ResultSetHandler) {
Object result = invocation.proceed();
Page page = localPage.get();
page.setResult((List) result);
mypage=page;
localPage.remove();
return result;
}
return null;
}
/**
* 只攔截這兩種型別的
* <br>StatementHandler
* <br>ResultSetHandler
* @param target
* @return
*/
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler || target instanceof ResultSetHandler) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
@Override
public void setProperties(Properties properties) {
}
/**
* 修改原SQL為分頁SQL
* @param sql
* @param page
* @return
*/
private String buildPageSql(String sql, Page page) {
StringBuilder pageSql = new StringBuilder(200);
// pageSql.append("select rn.* from ( select temp.*, rownum XH from ( ");
pageSql.append(sql);
pageSql.append(" limit ").append(page.getStartRow()).append(" , ").append(page.getPageRecord());
// pageSql.append(" ) temp where rownum <= ").append(page.getEndRow());
// pageSql.append(") rn where XH > ").append(page.getStartRow());
return pageSql.toString();
// return sql;
}
/**
* 獲取總記錄數
* @param sql
* @param connection
* @param mappedStatement
* @param boundSql
* @param page
*/
private void setPageParameter(String sql, Connection connection, MappedStatement mappedStatement,
BoundSql boundSql, Page page) {
// 記錄總記錄數
String countSql = "select count(0) from (" + sql + ") t";
// System.out.println(sql);
PreparedStatement countStmt = null;
ResultSet rs = null;
try {
countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
boundSql.getParameterMappings(), boundSql.getParameterObject());
//使用反射注入引數
Field metaParamsField = ReflectUtil.getFieldByFieldName(boundSql, "metaParameters");
if (metaParamsField != null) {
MetaObject mo = (MetaObject) ReflectUtil.getValueByFieldName(boundSql, "metaParameters");
ReflectUtil.setValueByFieldName(countBS, "metaParameters", mo);
}
setParameters(countStmt, mappedStatement, countBS, boundSql.getParameterObject());
rs = countStmt.executeQuery();
int totalCount = 0;
if (rs.next()) {
totalCount = rs.getInt(1);
}
page.setRecordCount(totalCount);
int totalPage = totalCount==0?0:(totalCount-1)/page.getPageRecord()+1;
page.setPageCount(totalPage);
} catch (Exception e) {
} finally {
try {
rs.close();
} catch (SQLException e) {
}
try {
countStmt.close();
} catch (SQLException e) {
}
}
}
/**
* 代入引數值
* @param ps
* @param mappedStatement
* @param boundSql
* @param parameterObject
* @throws SQLException
*/
private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql,
Object parameterObject) throws SQLException {
ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler.setParameters(ps);
}
/**
* Description: 分頁
* Author: liuzh
* Update: liuzh(2014-04-16 10:56)
*/
public static class Page<E> {
private int CurrentPage;
private int PageRecord;
private int startRow;
private int endRow;
private long RecordCount;
private int PageCount;
private List<E> result;
public Page(int pageNum, int pageSize) {
this.CurrentPage = pageNum;
this.PageRecord = pageSize;
this.startRow = pageNum > 0 ? (pageNum - 1) * pageSize : 0;
this.endRow = pageNum * pageSize;
}
public String toString() {
JSONObject count = new JSONObject();
count.put("RecordCount", RecordCount);
count.put("PageCount", PageCount);
count.put("CurrentPage",CurrentPage);
count.put("PageRecord", PageRecord);
return count.toString();
}
public int getCurrentPage() {
return CurrentPage;
}
public void setCurrentPage(int currentPage) {
CurrentPage = currentPage;
}
public int getPageRecord() {
return PageRecord;
}
public void setPageRecord(int pageRecord) {
PageRecord = pageRecord;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getEndRow() {
return endRow;
}
public void setEndRow(int endRow) {
this.endRow = endRow;
}
public long getRecordCount() {
return RecordCount;
}
public void setRecordCount(long recordCount) {
RecordCount = recordCount;
}
public int getPageCount() {
return PageCount;
}
public void setPageCount(int pageCount) {
PageCount = pageCount;
}
public List<E> getResult() {
return result;
}
public void setResult(List<E> result) {
this.result = result;
}
}
}
相關文章
- Mybatis 分頁:Pagehelper + 攔截器實現MyBatis
- mybatis-攔截器實際應用-替換表名-2022新專案MyBatis
- Spring MVC 中的攔截器的使用“攔截器基本配置” 和 “攔截器高階配置”SpringMVC
- SpringMVC攔截器SpringMVC
- axios 攔截器iOS
- spring攔截器Spring
- axios攔截器iOS
- sql攔截器SQL
- Mybatis 攔截器MyBatis
- MyBatis攔截器MyBatis
- SpringMVC攔截器,設定不攔截的URLSpringMVC
- IIS 攔截主頁和SwaggerSwagger
- Mybatis Interceptor 攔截器MyBatis
- spring boot 攔截器Spring Boot
- SpringMVC-攔截器SpringMVC
- gRPC(3):攔截器RPC
- 【SpringMVC】 4.3 攔截器SpringMVC
- SpringBoot攔截器中獲取註解、攔截器中注入ServiceSpring Boot
- Flume內建攔截器與自定義攔截器(程式碼實戰)
- Spring Boot新增攔截器Spring Boot
- SpringBoot 手寫攔截器Spring Boot
- HandlerInterceptor - 自定義攔截器
- 攔截過濾器模式過濾器模式
- mybatis註冊攔截器MyBatis
- 16、threadlocal+攔截器thread
- SpringMVC中的攔截器SpringMVC
- grpc中的攔截器RPC
- Spring 過濾器和攔截器Spring過濾器
- spring mvc攔截器,spring攔截器以及AOP切面的區別和原始碼SpringMVC原始碼
- 網站域名被QQ攔截提示:當前網頁非官方頁面攔截的解決辦法網站網頁
- axios原始碼分析——攔截器iOS原始碼
- C# 12 攔截器 InterceptorsC#
- spring mvc 攔截器的使用SpringMVC
- webwork的攔截器真是好用Web
- Java實現的攔截器Java
- 攔截器的使用問題
- 在springboot中使用攔截器Spring Boot
- 攔截器(Interceptor)與過濾器(Filter)過濾器Filter
- SpringBoot 攔截器、過濾器、監聽器Spring Boot過濾器