페이징 수정

master
mjkhan21 12 months ago
parent d8f9cd21c4
commit 0a37c042f1

@ -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<String, ?> params = (Map<String, ?>)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;
}
}
}

@ -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<Pageable.Info> 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<Object> boundedList = new BoundedList<>();
boundedList.setFetchSize(paging.fetchSize);
boundedList.addAll(list);
if (!boundedList.isEmpty()) {
boundedList.setStart((paging.pageNum - 1) * paging.fetchSize);
}
obj = boundedList;
}
return obj;
}
}
Loading…
Cancel
Save