SSM框架集成分页插件
在SSM框架搭建之maven方式(二)基础上进一步做以下修改
pom.xml添加如下代码
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version></dependency>
spring-mybatis.xml的id标签为sqlSessionFactory节点中添加如下内容
<property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> <!-- 你使用的数据库类型 --> helperDialect=mysql reasonable=true autoRuntimeDialect=true </value> </property> </bean> </array></property>
将UserController.java中的内容改写为如下代码
package com.lymn.it.controller;import java.util.List;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.lymn.it.model.User;import com.lymn.it.service.UserService;@Controllerpublic class UserController { @Autowired UserService userService; Logger logger=Logger.getLogger(UserController.class); @RequestMapping(value="/user") public String user(@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo,Model model) { logger.info("查询所有用户数据"); PageHelper.startPage(pageNo, 5); List<User> userList = userService.getAllUsers(); PageInfo<User> pageInfo=new PageInfo<User>(userList); model.addAttribute("userList", userList); model.addAttribute("pageInfo", pageInfo); logger.info("查询完毕,返回页面"); return "user"; }}
将user.jsp中的内容改写为如下代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html><head><meta charset="UTF-8"><title>User</title></head><body> <center> <table width="200" border="1"> <tr> <th scope="col">userid</th> <th scope="col">username</th> <th scope="col">password</th> <th scope="col">email</th> </tr> <c:forEach items="${userList}" var="user"> <tr> <td>${user.userid}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.email}</td> </tr> </c:forEach> </table> <p>当前 ${pageInfo.pageNum }页,总${pageInfo.pages } 页,总 ${pageInfo.total } 条记录</div></p> <a href="user?pageNo=${pageInfo.firstPage}">第一页</a> <c:if test="${pageInfo.hasPreviousPage }"> <a href="user?pageNo=${pageInfo.pageNum-1}">上一页</a> </c:if> <c:if test="${pageInfo.hasNextPage }"> <a href="user?pageNo=${pageInfo.pageNum+1}">下一页</a> </c:if> <a href="user?pageNo=${pageInfo.lastPage}">最后页</a> </center></body>
访问如下图所示表示成功
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。