springBoot学习笔记(2.4)—— 整合PageHelper分页组件

更多文章

更多系列文章在个人网站

springBoot学习系列笔记文章

springBoot学习笔记(1)—— 搭建springBoot项目


文章目录

  • 更多文章
  • springBoot学习系列笔记文章
  • 一、构建项目步骤
    • 1.引入pageHelper的jar包
    • 2.完整pom.xml内容
    • 3.application.yml的内容
    • 4.controller中查询分页数据
    • 5.service层中调用dao接口
    • 6.dao中的接口
    • 7.mapper文件内容
    • 8.运行截图
  • 总结


提示:以下是本篇文章正文内容,下面案例可供参考

一、构建项目步骤

1.引入pageHelper的jar包

   <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.2.3version>
        dependency>

2.完整pom.xml内容


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.2version>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>springBoot-pageHelpname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jdbcartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.2.1version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.2.3version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>


    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

3.application.yml的内容

spring:
  datasource:
    #   数据源基本配置
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
  
  main:
    allow-circular-references: true #解决升级 Spring Boot 2.6后,因循环引用导致启动时报错的问题


mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml




server:
  port: 8083 # 配置项目启动端口


pagehelper:
  helper-dialect: mysql #设置pageHelper的语言
  reasonable: true

4.controller中查询分页数据

  /****
     * description: 查询分页数据
     * version: 1.0 ->
     * date: 2022/1/7 18:09
     * author: xiaYZ
     * iteration: 迭代说明
     * @param commodityName
     * @param pageSize 每页数据大小
     * @param pageNum 页码
     * @return java.lang.Object
     */
    @GetMapping("findAllCommodity")
    public Object findAllCommodity(String commodityName, int pageSize,int pageNum){
        
        List<Commodity> allCommodity;
        try{
       		 PageHelper.startPage(pageNum, pageSize);
            allCommodity = commodityService.findAllCommodity(commodityName);
        }catch (Exception e){
            e.printStackTrace();
            return "查询分页数据错误";
        }
        PageInfo<Commodity> pageInfo = new PageInfo<>(allCommodity);
        return pageInfo;
    }

5.service层中调用dao接口

 public List<Commodity> findAllCommodity(String  commodityName){
        return commodityDao.findAllCommodity(commodityName);
    }

6.dao中的接口

/**
     * description: 查询所有商品数据
     * version: 1.0
     * date: 2022/1/7 18:01
     * author: xiaYZ
     * iteration: 迭代说明
     * @param commodityName 商品类
     * @return
     */
    List<Commodity> findAllCommodity(String commodityName);

7.mapper文件内容

    <select id="findAllCommodity" resultType="com.example.demo.entity.Commodity">
        select id, commodity_name commodityName, create_time createTime
        from commodity
        <where>
            <if test="commodityName != null and commodityName != ''">
                commodity_name = #{commodityName}
            if>
        where>
    select>

pageHelper生效后查询语句后面会自动添加limit语句限制

8.运行截图

在这里插入图片描述
springBoot学习笔记(2.4)—— 整合PageHelper分页组件_第1张图片

总结

  1. 引入pagehelper的jar包

  2. 设置pageHelper的语言种类

    pagehelper:
      helper-dialect: mysql #设置pageHelper的语言
      reasonable: true
    
  3. java语言中使用PageHelper

     PageHelper.startPage(pageNum, pageSize);
    

    注意 PageHelper.startPage(pageNum, pageSize);这句要紧跟查询数据的语句,否则会失效

项目源码

你可能感兴趣的:(springboot,mybatis,spring,boot,java,spring,mybatis,mysql)