SpringMVC分页显示

1、结构

  • entity javabean类
  • controller 控制层的包
  • service 接口
  • serviceImpl 服务层的实现类
  • dao dao层接口
  • mapper 实现dao层的方法与数据库打交道
  • util 工具类
  • 要使用到jstl 附上maven官网 https://mvnrepository.com/下载jstl依赖包,或者直接引用下面的代码
        <!-- jstl jsp标签库包 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

2、具体实现

首先在util包中新建一个Page类,用于封装分页查询所用到的数据,并进行计算

package com.practise.util;

import java.io.Serializable;

/**
 * 用于存放分页的信息
 * @author lenovo
 *
 */
public class Page{
    
    
	
	/**
	 * 1、起始位置(id)
	 * 2、每页显示的个数
	 * 3、最后一页的开始位置(id)
	 */
	
	int start = 0;
	int count = 5;
	int last = 0;
	public int getStart() {
    
    
		return start;
	}
	public void setStart(int start) {
    
    
		this.start = start;
	}
	public int getCount() {
    
    
		return count;
	}
	public void setCount(int count) {
    
    
		this.count = count;
	}
	public int getLast() {
    
    
		return last;
	}
	public void setLast(int last) {
    
    
		this.last = last;
	}
	//计算,需要一个总数(总记录数)
	public void calculate(int total) {
    
    
		//假设总数是50,是能够被5整除的,那么最后一页的开始就是45
		if(total % count == 0) {
    
    
			last = total - count;
		}else {
    
    
			//假设总数是51,不能够被5整除,那么最后一页开始就是50
			last = total - total % count;
		}
		
	}

}

接下来编写mapper
判断起始位置和所查询的数量是否为空
因为是分页查询我们要使用到总的记录数total

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.practise.dao.UserDao">
   <!-- 查询所有 -->
   <select id="list"  resultType="com.practise.entity.User">
      select id,email,password from user_info
      <if test="start!=null and count!=null">
         limit #{start},#{count}
      </if>
   </select>
   <!--用户总数 -->
   <select id="total" resultType="int">
       select count(*) from user_info
   </select>
</mapper>

dao层


public interface UserDao {
    
    
	List<User> list();
	//查询用户
	List<User> list(Page page);
	//查询用户的总数
	int total();
}

Service层,以及service实现类

public interface UserService {
    
    

	List<User> list();
	List<User> list(Page page);
	int total();
}

serviceImpl,调用dao层方法并返回

@Service
public class UserServiceImpl implements UserService {
    
    
	
	@Autowired
	UserDao userDao;

	public List<User> list() {
    
    
		return userDao.list();
	}

	public List<User> list(Page page) {
    
    
		return userDao.list(page);
	}

	public int total() {
    
    
		return userDao.total();
	}
}

controller层,将查询出来的用户保存到ModelAndView中
查询总记录数,计算最后一页起始查询位置。
接下来把userList.jsp放入到通过方法setViewName放入到ModelAndView中并返回。

@Controller
@RequestMapping("")
public class UserController {
    
    
	
	@Autowired
	UserService userService;
	
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Page page) {
    
    
	
		 ModelAndView mav = new ModelAndView(); 
		 List<User> list = userService.list(page);
		 int total = userService.total();
	     page.calculate(total);
		 
		 mav.addObject("list",list); 
		 //userlist是jsp页面的名字
		 mav.setViewName("userlist");
		 return mav;
	}
	

jsp页面,点击查询所有,显示查询的结果

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>所有用户</title>
</head>

<body>
<div style="width:500px;margin:0px auto;text-align:center">
  <p><a href="${pageContext.request.contextPath}/list.do">查询所有</a></p>
</div>
 <div style="width:500px;margin:0px auto;text-align:center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>email</td>
            <td>password</td>
        </tr>
        
       <c:forEach items="${list}" var="c" varStatus="st">
            <tr>
                <td>${c.email}</td>
                <td>${c.password}</td>
            </tr>
        </c:forEach>
    </table>
    <div style="text-align:center">
        <a href="?start=0">首  页</a>

        <!--当起始位置减去查询的个数大于等于0的时候  显示上一页 -->
        <c:if test="${page.start-page.count >= 0 }">
           <a href="?start=${page.start-page.count}">上一页</a>
        </c:if>
        <!--当起始位置加上查询的个数小于等于最后一页的起始查询位置  显示下一页 -->
        <c:if test="${page.start+page.count <= page.last }">
          <a href="?start=${page.start+page.count}">下一页</a>
        </c:if>
       
        <a href="?start=${page.last}">末  页</a>
    </div>
 </div>
</body>
</html>

最终效果

在这里插入图片描述

点击查询所有

在这里插入图片描述

点击下一页

在这里插入图片描述

点击末页

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ifyouwjk/article/details/106064388