Java后端实现分页效果

前言:在我们的Java项目中,分页是必不可少的数据展示页面所以这篇博客主要讲分页的实现

在我们的开发当中 ,前后端之间数据交互是必不可少的,如果数据比较多,就需要进行数据封装,拿到前端进行页面的展示

第一步,准备工具类,用作数据封装,拿到前端

- PageUtils.java

public class PageUtils<T> {
     

    private long pageIndex;  //当前页码
    private long pageSize;  //页面大小
    private long totalCount; //总条数
    private long pageCount;  //总页数

    private List<T> records; //每页的数据集合

    private long numberStart=1;//开始的页码序号
    private long numberEnd;//结束序号

    public PageUtils(long pageIndex, long pageSize, long totalCount, List<T> records) {
     
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.records = records;
        this.pageCount=(totalCount%pageSize==0)?(totalCount/pageSize):(totalCount/pageSize+1);

        this.numberStart=1;
        this.numberEnd=pageCount;

        //数学算法
        // -----------给页码序号赋值------------------
        // 一共显示10个页面 动态伸缩
        if (this.pageCount <= 10) {
     
            this.numberStart = 1;
            this.numberEnd = pageCount;
        } else {
     
            this.numberStart = pageIndex - 4;
            this.numberEnd = pageIndex + 5;
            if (numberStart < 1) {
     
                this.numberStart = 1;
                this.numberEnd = 10;
            } else if (numberEnd > pageCount) {
     
                this.numberEnd = pageCount;
                this.numberStart = pageCount - 9;
            }
        }
        // -----------偷偷的给页码序号赋值------------------
    }

    public PageUtils() {
     
        this.pageCount=(totalCount%pageSize==0)?(totalCount/pageSize):(totalCount/pageSize+1);
    }

    public long getPageIndex() {
     
        return pageIndex;
    }

    public void setPageIndex(long pageIndex) {
     
        this.pageIndex = pageIndex;
    }

    public long getPageSize() {
     
        return pageSize;
    }

    public void setPageSize(long pageSize) {
     
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
     
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
     
        this.totalCount = totalCount;
    }

    public long getPageCount() {
     
        return pageCount;
    }

    public void setPageCount(long pageCount) {
     
        this.pageCount = pageCount;
    }

    public List<T> getRecords() {
     
        return records;
    }

    public void setRecords(List<T> records) {
     
        this.records = records;
    }

    public long getNumberStart() {
     
        return numberStart;
    }

    public void setNumberStart(long numberStart) {
     
        this.numberStart = numberStart;
    }

    public long getNumberEnd() {
     
        return numberEnd;
    }

    public void setNumberEnd(long numberEnd) {
     
        this.numberEnd = numberEnd;
    }

}

第二步:需要准备好数据库的来查询语句,实体类更具数据表定义

1. public int getTotalCount(); //一共多少
2. public List getUsers(@Param(“pageStart”) long pageStart, @Param(“pageSize”) long pageSize);//查询每页数据

UserMapper.java持久层

public interface UserMapper{
     
	public int getTotalCount(); //一共多少
	public List<User> getUsers(@Param("pageStart") long pageStart, @Param("pageSize") long pageSize);//查询每页数据
}

UserService.java

public interface UserService{
     
	public int getTotalCount(); //一共多少
	public List<User> getUsers(@Param("pageStart") long pageStart, @Param("pageSize") long pageSize);//查询每页数据
}

UserServiceImpl实现类

@Service
public class UserServiceImpl implements UserService{
     
	@Autowired
	UserMapper userMapper ;
	 //一共多少
	public int getTotalCount(){
     
		return userMapper.getTotalCount();
	}
	//查询每页数据
	public List<User> getUsers(@Param("pageStart") long pageStart, @Param("pageSize") long pageSize){
     
		return userMapper.getUsers(pageStart,pageSize);
	}
}

第三步:控制层中编写代码

//@RequestMapping("/user_list/{pageIndex}/{pageSize}")
//解释:/user_list/第一页/一共显示5条数据

@Controller
public class UserController {
     
	@Autowired
	UserService userSerivce;
		
	@RequestMapping("/user_list/{pageIndex}/{pageSize}")
	    public String user_list(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") long pageSize, Model model){
     
	        int totalCount = userService.getTotalCount(); //总条数
	        List<User> users = userService.getUsers((pageIndex - 1) * pageSize, pageSize);//每页数据
	        //控制台打印出数据
	       for(User user:users){
     
	            System.out.println("user= " + user);
	        }
	        //封装一个分页的工具类
	        PageUtils pageUtils=new PageUtils(pageIndex,pageSize,totalCount,users);
	        //通过Model拿到前端
	        model.addAttribute("pageUtils",pageUtils);
	        return "userlist";//返回页面,经过SpringMVC前后缀配置生成路径
	    }
}

第四步:JSP页面

  • 加入文件头
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
  • 使用JSTL的 标签循环拿出数据添加
 <%--拿到数据集合--%>
<c:forEach items="${pageUtils.records}" var="user">
        <tr>
            <td>${
     user.id}</td>
            <td>${
     user.name}</td>
            <td>${
     user.pass}</td>
        </tr>
</c:forEach>

第五步:配置tomcat,运行项目,当然如果使用SpringBoot会简单很多

Java后端实现分页效果_第1张图片

第五步:启动成功,页面展示出来,说明分页就做好了

Java后端实现分页效果_第2张图片




原创文章,未经允许禁止盗用




你可能感兴趣的:(Java,spring,jsp,web,java)