使用jsp+mybatis的分页插件实现查询加分页效果首页上一页下一页尾页
要实现查询加分页效果,可以使用jsp+mybatis的分页插件PageHelper。
首先,你需要在你的项目中引入PageHelper依赖。如果是Maven项目,可以在pom.xml文件中添加以下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>版本号</version>
</dependency>
然后,在你的Mapper接口中定义查询方法,使用PageHelper进行分页:
List<User> getUsers(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize);
在你的Mapper.xml文件中,编写对应的SQL语句:
<select id="getUsers" resultType="User">
SELECT * FROM users
</select>
接下来,在你的Controller中调用查询方法,并将结果传递给前端:
@RequestMapping("/users")
public String getUsers(Model model, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.getUsers(pageNum, pageSize);
PageInfo<User> pageInfo = new PageInfo<>(users);
model.addAttribute("users", users);
model.addAttribute("pageInfo", pageInfo);
return "users";
}
在你的JSP页面中,可以使用JSTL标签库来遍历用户列表,并显示分页导航:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="?pageNum=${pageInfo.prePage}&pageSize=${pageInfo.pageSize}">上一页</a>
</c:if>
<c:if test="${pageInfo.hasNextPage}">
<a href="?pageNum=${pageInfo.nextPage}&pageSize=${pageInfo.pageSize}">下一页</a>
</c:if>
</div>
这样,你就可以实现查询加分页效果,通过点击上一页和下一页链接来切换页面。首页和尾页的实现可以根据需要进行扩展
原文地址: https://www.cveoy.top/t/topic/ioNa 著作权归作者所有。请勿转载和采集!