用mybatis PageHelper的分页插件加jsp页面写出员工表的分页编写代码
以下是使用MyBatis PageHelper插件配合JSP页面实现员工表的分页的代码示例:
- 引入PageHelper依赖和JSTL标签库: 在pom.xml中添加以下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
在JSP页面中引入JSTL标签库:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- 在MyBatis配置文件中配置PageHelper插件:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
<property name="reasonable" value="true"/>
</plugin>
</plugins>
- 在Controller中获取员工列表并进行分页:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class EmployeeController {
@Resource
private EmployeeService employeeService;
@GetMapping("/employees")
public String getEmployees(Model model, Integer pageNum, Integer pageSize) {
// 设置分页参数
if (pageNum != null && pageSize != null) {
PageHelper.startPage(pageNum, pageSize);
}
// 获取员工列表
List<Employee> employees = employeeService.getEmployees();
// 封装分页结果
PageInfo<Employee> pageInfo = new PageInfo<>(employees);
// 将分页结果传递给JSP页面
model.addAttribute("pageInfo", pageInfo);
return "employee-list";
}
}
- 在JSP页面中显示员工列表和分页信息:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>员工列表</title>
</head>
<body>
<h1>员工列表</h1>
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>职位</th>
</tr>
<c:forEach items="${pageInfo.list}" var="employee">
<tr>
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.position}</td>
</tr>
</c:forEach>
</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>
</body>
</html>
以上代码示例展示了如何使用MyBatis PageHelper插件配合JSP页面实现员工表的分页功能。其中,Controller中的getEmployees方法接收pageNum和pageSize参数来进行分页,然后将分页结果传递给JSP页面进行展示。JSP页面使用JSTL标签库来遍历员工列表和生成分页导航链接
原文地址: http://www.cveoy.top/t/topic/ioXn 著作权归作者所有。请勿转载和采集!