Skip to content

Commit 6150b4f

Browse files
committed
ApiBoot Mybatis Pageable 示例
1 parent 2f58136 commit 6150b4f

File tree

1 file changed

+124
-0
lines changed
  • api-boot-samples/api-boot-sample-mybatis-pageable

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
## ApiBoot Mybatis Pageable
2+
3+
`MyBatis-Pageable`是一款自动化分页的插件,基于`MyBatis`内部的插件`Interceptor`拦截器编写完成,拦截`Executor.query`的两个重载方法计算出分页的信息以及根据配置的数据库`Dialect`自动执行不同的查询语句完成总数量的统计。
4+
5+
### 添加依赖
6+
7+
```xml
8+
<dependencies>
9+
<!--ApiBoot Mybatis Pageable-->
10+
<dependency>
11+
<groupId>org.minbox.framework</groupId>
12+
<artifactId>api-boot-starter-mybatis-pageable</artifactId>
13+
</dependency>
14+
<!--省略其他依赖-->
15+
</dependencies>
16+
<dependencyManagement>
17+
<dependencies>
18+
<!--springboot dependencies-->
19+
<dependency>
20+
<groupId>org.minbox.framework</groupId>
21+
<artifactId>api-boot-dependencies</artifactId>
22+
<version>2.0.6.RELEASE</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
```
29+
30+
31+
32+
### 支持的数据库
33+
`MyBatis-Pageable`目前支持的主流数据库:
34+
- DB2
35+
- Derby
36+
- DM、
37+
- H2、
38+
- HSQL、
39+
- InforMix、
40+
- Mariadb、
41+
- MySQL、
42+
- Oracle、
43+
- Postgres、
44+
- SqlLite、
45+
- SqlServer2000以上版本
46+
47+
### 怎么使用?
48+
目前`MyBatis-Pageable`使用比较简单,有一个分页请求类`PageableRequest`来完成自动分页操作,我们来看个简单的示例:
49+
```java
50+
Page<UserEntity> page = PageableRequest.of(1, 5).request(() -> userMapper.selectAll());
51+
```
52+
53+
- `of` 配置分页的`当前页码`以及`每页的限制条数`
54+
- `request` 该方法需要传递一个业务逻辑方法,也就是你需要执行分页的方法
55+
56+
#### Page对象详解
57+
在上面简单的一行代码就可以完成自动分页以及读取出分页相关的信息,分页执行后我们通过`Page`对象都可以获取到什么内容呢?
58+
- `data` 分页后的数据列表,具体的返回值可以使用`Page<T>`泛型接收
59+
- `totalPages` 总页数
60+
- `totalElements` 总条数
61+
- `pageIndex` 当前页码
62+
- `pageSize` 每页限制条数
63+
- `offset` 分页开始位置
64+
- `endRow` 分页结束位置
65+
- `hasNext` 是否存在下一页,`true`:存在,`false`:不存在
66+
- `hasPrevious` 是否存在上一页,`true`:存在,`false`:不存在
67+
- `isFirst` 是否为首页,`true`:首页,`false`:非首页
68+
- `isLast` 是否为末页,`true`:末页,`false`:非末页
69+
70+
#### 翻页查询
71+
实际开发过程中存在这种情况,虽然传递的分页页码为`1`,但是种种判断过后我需要查询`上一页`或者`下一页``首页`的数据,这时候你就可以`PageableRequest`对象的`next()``previous()``first()`方法来处理这种事情的发生,如下示例:
72+
```
73+
Pageable pageable = PageableRequest.of(2, 10);
74+
if (xx = xx) {
75+
pageable.next();
76+
}
77+
Page<UserEntity> page = pageable.request(()->userMapper.selectAll());
78+
```
79+
上面是`翻页到下一页`的查询示例,当然这个功能是为了尽可能的方便分页的使用,同样的`previous()``first()`方法都可以这么使用。
80+
81+
### 注解使用
82+
83+
`Mybatis Pageable`内置了两个分页相关的注解:
84+
85+
- `PageSize`:如果实体类内的字段添加了该注解,会自动作为分页每页限制数量作为查询条件
86+
- `PageIndex`:如果实体类内的字段添加了该注解,会自动作为分页当前页码作为查询条件
87+
88+
使用示例:
89+
90+
```java
91+
@Test
92+
public void test() {
93+
// 构建查询参数类
94+
QueryUserPageParam pageParam = new QueryUserPageParam();
95+
pageParam.setPageSize(10);
96+
pageParam.setPageIndex(1);
97+
pageParam.setKeyWord("少年");
98+
99+
PageableRequest.of(pageParam).request(() -> {
100+
// 模糊查询用户列表(无需添加分页限制)
101+
});
102+
}
103+
104+
/**
105+
* 示例:用户分页查询
106+
* 根据关键字条件
107+
*/
108+
@Data
109+
class QueryUserPageParam extends PageParam {
110+
private String keyWord;
111+
}
112+
113+
/**
114+
* 示例:@PageIndex、@PageSize注解使用
115+
*/
116+
@Data
117+
class PageParam {
118+
@PageIndex
119+
private int pageIndex;
120+
@PageSize
121+
private int pageSize;
122+
}
123+
```
124+

0 commit comments

Comments
 (0)