페이징 수정
parent
d8f9cd21c4
commit
0a37c042f1
@ -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…
Reference in New Issue