From 0a37c042f1f5c1e0cbe4472e87dd48288eeeec7b Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Wed, 6 Dec 2023 13:54:02 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=95=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xit/foundation/data/paging/Pageable.java | 39 ++++++++++++++ .../xit/foundation/data/paging/PagingAid.java | 51 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/cokr/xit/foundation/data/paging/PagingAid.java diff --git a/src/main/java/cokr/xit/foundation/data/paging/Pageable.java b/src/main/java/cokr/xit/foundation/data/paging/Pageable.java index f5c4cb0..d8d396a 100644 --- a/src/main/java/cokr/xit/foundation/data/paging/Pageable.java +++ b/src/main/java/cokr/xit/foundation/data/paging/Pageable.java @@ -2,6 +2,7 @@ package cokr.xit.foundation.data.paging; import java.util.Map; +import cokr.xit.foundation.component.QueryRequest; import cokr.xit.foundation.data.Convert; /**조회 결과를 페이징 처리하여 받고자 할 경우, @@ -68,4 +69,42 @@ public interface Pageable { * @param totalSize 조회 결과의 전체 갯수 */ default void setTotalSize(long totalSize) {} + + public static class Info { + static Info create(Object obj) { + if (obj instanceof Map) { + @SuppressWarnings("unchecked") + Map params = (Map)obj; + return new Pageable.Info( + Pageable.getFetchSize(params), + Pageable.getPageNum(params) + ); + } else if (obj instanceof QueryRequest) { + QueryRequest req = (QueryRequest)obj; + return new Pageable.Info( + req.getFetchSize(), + req.getPageNum() + ); + } else if (obj instanceof Pageable) { + Pageable pageable = (Pageable)obj; + return new Pageable.Info( + pageable.getFetchSize(), + pageable.getPageNum() + ); + } else + return null; + } + + int pageNum; + int fetchSize; + + Info(int pageNum, int fetchSize) { + this.pageNum = pageNum; + this.fetchSize = fetchSize; + } + + boolean isPaging() { + return pageNum > 0 && fetchSize > 0; + } + } } \ No newline at end of file diff --git a/src/main/java/cokr/xit/foundation/data/paging/PagingAid.java b/src/main/java/cokr/xit/foundation/data/paging/PagingAid.java new file mode 100644 index 0000000..d123964 --- /dev/null +++ b/src/main/java/cokr/xit/foundation/data/paging/PagingAid.java @@ -0,0 +1,51 @@ +package cokr.xit.foundation.data.paging; + +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; + +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.executor.resultset.ResultSetHandler; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +@Intercepts({ + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), + @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) +}) +public class PagingAid extends MybatisPlugin { + private static final ThreadLocal pagingInfo = new ThreadLocal<>(); + + @Override + protected Object query(Executor executor, MappedStatement mappedStatement, Object obj, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { + Pageable.Info paging = Pageable.Info.create(obj); + if (paging != null && paging.isPaging()) + pagingInfo.set(paging); + + return super.query(executor, mappedStatement, obj, rowBounds, resultHandler); + } + + @Override + protected Object handle(ResultSetHandler resultSetHandler, Statement statement) throws SQLException { + Object obj = super.handle(resultSetHandler, statement); + + Pageable.Info paging = pagingInfo.get(); + if (paging != null && (obj instanceof List)) { + pagingInfo.remove(); + + List list = (List)obj; + BoundedList boundedList = new BoundedList<>(); + boundedList.setFetchSize(paging.fetchSize); + boundedList.addAll(list); + if (!boundedList.isEmpty()) { + boundedList.setStart((paging.pageNum - 1) * paging.fetchSize); + } + obj = boundedList; + } + + return obj; + } +} \ No newline at end of file