SSM框架集成分页插件

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;

@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>

访问如下图所示表示成功

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值