mybatisplus对list进行分页
时间: 2023-04-25 12:04:45 浏览: 309
MybatisPlus可以通过使用PageHelper插件来对List进行分页。具体步骤如下:
1. 在pom.xml文件中添加PageHelper依赖:
```
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
```
2. 在Mapper接口中添加方法,使用@Select注解查询数据:
```
@Select("SELECT * FROM table_name")
List<Table> selectAll();
```
3. 在Service层中调用Mapper接口方法,并使用PageHelper.startPage()方法设置分页参数:
```
List<Table> tableList = tableMapper.selectAll();
PageInfo<Table> pageInfo = new PageInfo<>(tableList);
```
其中,PageInfo是PageHelper提供的分页信息类,可以获取分页相关的信息,如总记录数、总页数、当前页码等。
4. 在Controller层中将PageInfo对象传递给前端页面,用于展示分页信息和数据列表:
```
model.addAttribute("pageInfo", pageInfo);
```
在前端页面中,可以使用PageInfo对象提供的方法获取分页信息和数据列表,如:
```
<ul>
<li>总记录数:${pageInfo.total}</li>
<li>总页数:${pageInfo.pages}</li>
<li>当前页码:${pageInfo.pageNum}</li>
</ul>
<table>
<thead>
<tr>
<th>列1</th>
<th>列2</th>
<th>列3</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pageInfo.list}" var="table">
<tr>
<td>${table.column1}</td>
<td>${table.column2}</td>
<td>${table.column3}</td>
</tr>
</c:forEach>
</tbody>
</table>
```
以上就是使用MybatisPlus对List进行分页的步骤。