Skip to content

Commit e680751

Browse files
committed
🚀 支持跳过分页
1 parent ad3272f commit e680751

File tree

5 files changed

+90
-19
lines changed

5 files changed

+90
-19
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.minbox.framework.mybatis.pageable.common;
2+
3+
/**
4+
* 分页状态
5+
*
6+
* @author 恒宇少年
7+
*/
8+
public enum PagingStatus {
9+
/**
10+
* 已开启分页
11+
*/
12+
STARTED,
13+
/**
14+
* 跳过本次分页
15+
*/
16+
SKIPPING
17+
}

src/main/java/org/minbox/framework/mybatis/pageable/common/exception/ExceptionCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
public enum ExceptionCode {
1212
PAGE_INDEX_FAILED("分页参数:当前页码参数传递错误,请传递大于0的正整数."),
1313
PAGE_SIZE_FAILED("分页参数:每页条数参数传递错误,请传递大于0的正整数."),
14-
DIALECT_NOT_FOUND("数据库方言暂未支持.");
14+
DIALECT_NOT_FOUND("数据库方言暂未支持."),
15+
PAGE_LOCAL_NOT_FOUND("线程副本中不存在分页对象.");
1516
private String message;
1617

1718
ExceptionCode(String message) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.minbox.framework.mybatis.pageable.request;
2+
3+
import lombok.AccessLevel;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import org.minbox.framework.mybatis.pageable.Page;
8+
import org.minbox.framework.mybatis.pageable.common.PagingStatus;
9+
10+
/**
11+
* 本地分页封装对象
12+
*
13+
* @author 恒宇少年
14+
*/
15+
@Getter
16+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
17+
public class PageLocal {
18+
@Setter
19+
private PagingStatus pagingStatus;
20+
private Page<?> page;
21+
22+
private PageLocal(PagingStatus pagingStatus, Page<?> page) {
23+
this.pagingStatus = pagingStatus;
24+
this.page = page;
25+
}
26+
27+
public static PageLocal instance(Page<?> page) {
28+
return new PageLocal(PagingStatus.STARTED, page);
29+
}
30+
}
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.minbox.framework.mybatis.pageable.request;
22

33
import org.minbox.framework.mybatis.pageable.Page;
4+
import org.minbox.framework.mybatis.pageable.common.PagingStatus;
5+
import org.minbox.framework.mybatis.pageable.common.exception.ExceptionCode;
6+
import org.minbox.framework.mybatis.pageable.common.exception.PageableException;
47

58
/**
69
* {@link ThreadLocal}上下文类
@@ -10,21 +13,45 @@
1013
* @author 恒宇少年
1114
*/
1215
public class PageLocalContext {
13-
private static final ThreadLocal<Page<?>> PAGE_THREAD_LOCAL = new ThreadLocal<>();
16+
private static final ThreadLocal<PageLocal> PAGE_THREAD_LOCAL = new ThreadLocal<>();
1417

1518
public static Page<?> getPageInstance() {
16-
return PAGE_THREAD_LOCAL.get();
19+
PageLocal pageLocal = PAGE_THREAD_LOCAL.get();
20+
if (pageLocal == null) {
21+
throw new PageableException(ExceptionCode.PAGE_LOCAL_NOT_FOUND);
22+
}
23+
return pageLocal.getPage();
1724
}
1825

26+
/**
27+
* 判定是否启动了分页
28+
*
29+
* @return 线程副本中存在 {@link PageLocal}对象并且是{@link PagingStatus#STARTED}状态时返回"true"
30+
*/
1931
public static boolean isStarting() {
20-
return PAGE_THREAD_LOCAL.get() != null;
32+
PageLocal pageLocal = PAGE_THREAD_LOCAL.get();
33+
return pageLocal != null && PagingStatus.STARTED == pageLocal.getPagingStatus();
2134
}
2235

23-
public static void startPaging(Page<?> page) {
24-
PAGE_THREAD_LOCAL.set(page);
36+
public static void startPaging() {
37+
PageLocal pageLocal = PAGE_THREAD_LOCAL.get();
38+
if (!isStarting()) {
39+
pageLocal.setPagingStatus(PagingStatus.STARTED);
40+
}
2541
}
2642

27-
public static void stopPaging() {
43+
static void startPaging(Page<?> page) {
44+
if (!isStarting()) {
45+
PAGE_THREAD_LOCAL.set(PageLocal.instance(page));
46+
}
47+
}
48+
49+
public static void skipPaging() {
50+
PageLocal pageLocal = PAGE_THREAD_LOCAL.get();
51+
pageLocal.setPagingStatus(PagingStatus.SKIPPING);
52+
}
53+
54+
static void stopPaging() {
2855
PAGE_THREAD_LOCAL.remove();
2956
}
3057
}

src/main/java/org/minbox/framework/mybatis/pageable/request/PageableRequest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,15 @@ public static Pageable of(Object pageableObject) {
3838

3939
@Override
4040
public <T> Page<T> request(LogicFunction function) {
41-
if (PageLocalContext.isStarting()) {
42-
try {
43-
function.invoke();
44-
return (Page<T>) PageLocalContext.getPageInstance();
45-
} catch (Exception e) {
46-
log.error(e.getMessage(), e);
47-
} finally {
48-
if (PageLocalContext.isStarting()) {
49-
PageLocalContext.stopPaging();
50-
}
41+
try {
42+
function.invoke();
43+
return (Page<T>) PageLocalContext.getPageInstance();
44+
} catch (Exception e) {
45+
log.error(e.getMessage(), e);
46+
} finally {
47+
if (PageLocalContext.isStarting()) {
48+
PageLocalContext.stopPaging();
5149
}
52-
} else {
53-
log.warn("There is no page replica instance in PageLocalContext.");
5450
}
5551
return DefaultPage.instance(this);
5652
}

0 commit comments

Comments
 (0)