PageHelper分页的学习(使用layui实现分页)

  通过layui实现分页,在做项目的时候,使用layui前端框架的数据表格实现分页,不知道如何实现的话,是会走很多的弯路的,现在我根据自己花了半天研究的时间研究和看各个视频学习,总结一下,具体不描述,只看过程咋实现。

 

  第一、进入layui的页面,拉取数据表格的初始化代码:(这里的数据接口和列表的显示名称我已经更换,具体使用根据自己的情况作出改变)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css" media="all">
</head>
<body>

<table id="demo" lay-filter="test"></table>

<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script>
    layui.use('table', function(){
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height: 315
            ,url: '/admin/splitByMap' //数据接口
            ,page: true //开启分页
            ,limit:6
            ,limits:[10,15,20,30,40,50,100]
            ,cols: [[ //表头
                {field: 'studentID', title: '学号', width:160,align:"center"}
                ,{field: 'studentName', title: '姓名', width:160,align:"center"}
                ,{field: 'studentSex', title: '性别', width:160,align:"center"}
                ,{field: 'studentPhone', title: '手机号', width:160,align:"center"}
                ,{field: 'studentAddress', title: '地址', width:160,align:"center"}
                ,{field: 'studentProfession', title: '专业', width:160,align:"center"}
                ,{field: 'studentGrade', title: '年级', width:160,align:"center"}
            ]]
        });
    });
</script>
</body>
</html>

  第二、做好第一步,那么前端的显示页面就实现ok了,下来就是实现后台了,对应后台接口。(后台还是很重要的,也是大家跟家关心的,在这里显示个人的具体代码)

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hui.pojo.StudentVo;
import com.hui.pojo.Students;
import com.hui.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin")
public class StudentController {

    @Autowired
    private StudentsService studentsService;

    @RequestMapping("/splitByMap")
    public String getAllStudentController(StudentVo studentVo) {

        PageHelper.startPage(studentVo.getPage(), studentVo.getLimit());

        System.out.println("controller:" + studentVo);

        List<Students> studentsList = studentsService.getAllStudentService(studentVo);

        PageInfo<Students> pageInfo = new PageInfo<Students>(studentsList);

        System.out.println("输出分页后的数据:" + pageInfo.getList());

        Map<String,Object> map = new HashMap<String, Object>();

        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return JSON.toJSONString(map);

    }

}

  看代码会发现有一个类的使用就是PageHelper类,这是一个分页插件,如果你是maven项目的话,需要在pom.xml中添加分页插件。

<!--    添加分页插件    -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

  同时,你还要会发现有JSON这是个啥?我想你应该知道,我就简单说一下,就是将数据类型进行转换为JSON类型,传递给前台解析使用。所有还是要添加JSON插件

<!-- 导入json支持包 -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

  再回发现StudentVo类是个啥?这是个对前台返回的page和limit参数进行封装,当前你不封装,就是用int page,int limit当然没有问题,这样只是会显得很清晰。代码如下:

import lombok.Data;

@Data
public class StudentVo extends Students{

    private int page;
    private int limit;

}

  最后给大家看看,前台返回的参数,给大家说参数,但是若是没有给大家具体看大家是不知道的

  前台的页面显示(最终的页面):

 

   这个是看前台的检查页面:

 

   注意:这个page和limit是我自己写的吗?显然不是,是人家框架就有的,他发送过来,那我就在后台也写一个同名的进行接收,以便处理。

  那么代码展示完了,插件也添加了,页面也说了,那我写的这个到底是啥意思?那就一步一步分析一下

public String getAllStudentController(StudentVo studentVo) {
    
        使用分页插件进行分页设置,在这个方法中国是需要两个参数的,第一个参数是当前的页码数,第二个参数是每一页都要显示几条数据。
        PageHelper.startPage(studentVo.getPage(), studentVo.getLimit());
这个代码的含义,相信大家都是知道的,就是调用service层与其中的mapper层的方法实现数据的返回,这里的数据返回可不是全部数据,而是根据你的页码数与页面显示数,进行判断的,那这是咋做的呢?你会发现这里的方法调用中有一个参数,就是studentVo类对象,里面封装了来自前台请求的参数page和limit List
<Students> studentsList = studentsService.getAllStudentService(studentVo);
使用PageInfo类实现对返回数据的封装,为啥要封装?因为里面有很多的方法,有总数据量,返回的数据,当前页码数,前一个页码,后一个页码等等很方便。 PageInfo
<Students> pageInfo = new PageInfo<Students>(studentsList);
下来就是使用map进行设置参数。 那这个是啥?首先先说一下,layui有自己特定的接收参数的格式,上面给大家看了,你不按照他的来,你的数据就无法实现前台接收,也就无法显示。 Map
<String,Object> map = new HashMap<String, Object>(); map.put("code",0); map.put("msg",""); map.put("count",pageInfo.getTotal()); map.put("data",pageInfo.getList());
最后,将封装好的需要显示的数据和数据的总量的map对象以JSON的类型返回,因为前台就需要JSON类型数据,他会自己进行解析,不用我们操心。 return JSON.toJSONString(map); }

  第三、controller完成了,那就看service层(也没啥就是一步步的调用方法)

import com.hui.mapper.StudentsMapper;
import com.hui.pojo.StudentVo;
import com.hui.pojo.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentsServiceImpl implements StudentsService{

    @Autowired
    private StudentsMapper studentsMapper;


    public List<Students> getAllStudentService(StudentVo studentVo) {
        return studentsMapper.getAllStudentMapper(studentVo);
    }
}

  第四、mapper层

import java.util.List;

public interface StudentsMapper {

    List<Students> getAllStudentMapper(StudentVo studentVo);
}
<?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.hui.mapper.StudentsMapper" >

    <select id="getAllStudentMapper" resultType="com.hui.pojo.Students" parameterType="com.hui.pojo.StudentVo">
注意一下,这里是不需要自己写limit的,pagehelper会帮你自动写的,但是你不要在后面添加;分号,后则就会报错,出错的SQL语句就是:
      select * from splitpage.students;limit 1,5

        select * from splitpage.students
    </select>
</mapper>

  ok,到这里就源码结束,对于对基本的分页操作就结束了。

  回顾一下,需要了啥:

  JSON插件、PageHelper插件实现数据的转换传递和分页的核心处理。

  StudentVo类实现了对page和limit的封装。

  Map类实现了对数据接收格式的设置(也就是数据的返回),最后用JSON.toJSONString(map)方法返回。

   有的时候,做项目就很奇怪,你不会就是不会,咋搞都搞不好,多看看,多思考,网上的也良莠不齐。

 

  最后,对于layui的更多使用我会继续更新。

posted @ 2021-11-08 23:40  心向未来  Views(425)  Comments(0)    收藏  举报