您好,登錄后才能下訂單哦!
在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;
@Controller
public 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>
訪問如下圖所示表示成功
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。