PageHelper(分页助手)使用教程

前言:基于SSM(或者SpringBoot)使用的,可以快速完成分页的需求。

导入对应的包(pagehelper),下面是对应的pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入mybatis插件-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- 分页处理引入包 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!--MySQL依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

其他层的配置情况

controller层

package com.example.demo.controller;

import com.example.demo.domain.Hospital;
import com.example.demo.service.TestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {
    @Autowired
    TestService testService;


    @GetMapping("/list")  //等价于@RequestMapping(method = RequestMethod.GET)
    //@RequestParam是将请求参数绑定到控制器的方法参数上
    public Object listPages(@RequestParam int pageNum, @RequestParam int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Hospital> hospital = testService.selectAll();
        return new PageInfo<>(hospital);
    }
}

Service层

package com.example.demo.service;

import com.example.demo.domain.Hospital;
import com.example.demo.mapper.TestMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TestService {
    @Autowired
    TestMapper testMapper;

    public List<Hospital> selectAll() {
        return testMapper.selectAll();
    }
}

mapper层

package com.example.demo.mapper;

import com.example.demo.domain.Hospital;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface TestMapper {
    List<Hospital> selectAll();
}

实体层

package com.example.demo.domain;


public class Hospital {

  private long id;
  private String hospitalName;
  private String hospitalArea;
  private String hospitalImg;
  private String hospitalDeanName;
  private String hospitalYear;
  private String hospitalNature;
  private String hospitalGrade;
  private long hospitalOfficesNum;
  private long medicalInsuranceNum;
  private long hospitalBedNum;
  private long outpatientNum;
  private String isMedicalInsurance;
  private String hospitalEquipment;
  private String hospitalAbout;
  private String hospitalHonor;
  private String hospitalUrl;
  private String hospitalPhone;
  private String hospitalAddress;
  private String hospitalPostCode;
  private String hospitalBusRoute;
  private long isOpen;


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public String getHospitalName() {
    return hospitalName;
  }

  public void setHospitalName(String hospitalName) {
    this.hospitalName = hospitalName;
  }


  public String getHospitalArea() {
    return hospitalArea;
  }

  public void setHospitalArea(String hospitalArea) {
    this.hospitalArea = hospitalArea;
  }


  public String getHospitalImg() {
    return hospitalImg;
  }

  public void setHospitalImg(String hospitalImg) {
    this.hospitalImg = hospitalImg;
  }


  public String getHospitalDeanName() {
    return hospitalDeanName;
  }

  public void setHospitalDeanName(String hospitalDeanName) {
    this.hospitalDeanName = hospitalDeanName;
  }


  public String getHospitalYear() {
    return hospitalYear;
  }

  public void setHospitalYear(String hospitalYear) {
    this.hospitalYear = hospitalYear;
  }


  public String getHospitalNature() {
    return hospitalNature;
  }

  public void setHospitalNature(String hospitalNature) {
    this.hospitalNature = hospitalNature;
  }


  public String getHospitalGrade() {
    return hospitalGrade;
  }

  public void setHospitalGrade(String hospitalGrade) {
    this.hospitalGrade = hospitalGrade;
  }


  public long getHospitalOfficesNum() {
    return hospitalOfficesNum;
  }

  public void setHospitalOfficesNum(long hospitalOfficesNum) {
    this.hospitalOfficesNum = hospitalOfficesNum;
  }


  public long getMedicalInsuranceNum() {
    return medicalInsuranceNum;
  }

  public void setMedicalInsuranceNum(long medicalInsuranceNum) {
    this.medicalInsuranceNum = medicalInsuranceNum;
  }


  public long getHospitalBedNum() {
    return hospitalBedNum;
  }

  public void setHospitalBedNum(long hospitalBedNum) {
    this.hospitalBedNum = hospitalBedNum;
  }


  public long getOutpatientNum() {
    return outpatientNum;
  }

  public void setOutpatientNum(long outpatientNum) {
    this.outpatientNum = outpatientNum;
  }


  public String getIsMedicalInsurance() {
    return isMedicalInsurance;
  }

  public void setIsMedicalInsurance(String isMedicalInsurance) {
    this.isMedicalInsurance = isMedicalInsurance;
  }


  public String getHospitalEquipment() {
    return hospitalEquipment;
  }

  public void setHospitalEquipment(String hospitalEquipment) {
    this.hospitalEquipment = hospitalEquipment;
  }


  public String getHospitalAbout() {
    return hospitalAbout;
  }

  public void setHospitalAbout(String hospitalAbout) {
    this.hospitalAbout = hospitalAbout;
  }


  public String getHospitalHonor() {
    return hospitalHonor;
  }

  public void setHospitalHonor(String hospitalHonor) {
    this.hospitalHonor = hospitalHonor;
  }


  public String getHospitalUrl() {
    return hospitalUrl;
  }

  public void setHospitalUrl(String hospitalUrl) {
    this.hospitalUrl = hospitalUrl;
  }


  public String getHospitalPhone() {
    return hospitalPhone;
  }

  public void setHospitalPhone(String hospitalPhone) {
    this.hospitalPhone = hospitalPhone;
  }


  public String getHospitalAddress() {
    return hospitalAddress;
  }

  public void setHospitalAddress(String hospitalAddress) {
    this.hospitalAddress = hospitalAddress;
  }


  public String getHospitalPostCode() {
    return hospitalPostCode;
  }

  public void setHospitalPostCode(String hospitalPostCode) {
    this.hospitalPostCode = hospitalPostCode;
  }


  public String getHospitalBusRoute() {
    return hospitalBusRoute;
  }

  public void setHospitalBusRoute(String hospitalBusRoute) {
    this.hospitalBusRoute = hospitalBusRoute;
  }


  public long getIsOpen() {
    return isOpen;
  }

  public void setIsOpen(long isOpen) {
    this.isOpen = isOpen;
  }

}

mybatis层

<?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.example.demo.mapper.TestMapper">
    <select id="selectAll" resultType="com.example.demo.domain.Hospital">
        select *  from hospital
    </select>
</mapper>

配置层

server:
  port: 8080
  address: 127.0.0.1

#配置pagehelper的内容
pagehelper:
    helper-dialect: mysql

#配置spring的连接
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/hosdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC


mybatis:
  mapper-locations: classpath:mabatis/*.xml
  configuration:
    #开启驼峰功能
    map-underscore-to-camel-case: true

测试结果

PageHelper(分页助手)使用教程

http://localhost:8080/list?pageNum=1&pageSize=5
上一篇:创建maven项目配置spring boot


下一篇:Java https ssl证书导入删除