亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PageHelper在springboot+mybatis框架中的使用步驟及原理解析

 更新時(shí)間:2023年03月13日 10:50:06   作者:全國(guó)青少年熬夜大賽冠軍  
這篇文章主要介紹了PageHelper在springboot+mybatis框架中的使用步驟及原理解析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、思路

將分頁(yè)所需的內(nèi)容都放到一個(gè)實(shí)體類中

分頁(yè)數(shù)據(jù)所需要的實(shí)體類!內(nèi)包含頁(yè)碼,頁(yè)大小,總條數(shù),總頁(yè)數(shù),起始行

pagehelpr提供了這個(gè)類 pageInfo,不需要我們自己創(chuàng)建

 二、主要邏輯

select * from 表名 limit 起始行,展示幾條數(shù)據(jù)

#第n頁(yè) 每頁(yè)展示五條數(shù)據(jù)

select * from 表名 limit (n-1)*5,5

#每頁(yè)展示多少條 pageSize

3

#總共有多少條

total

select count(*) from 表名

#總頁(yè)數(shù)

pages

pages=total%pagesSize==0?total/pgeSize:total/pageSize+1;

#當(dāng)前頁(yè)

 pageNum

三、步驟

1.引入pagehelper依賴

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>
#pagehelper分頁(yè)插件配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

2.bean實(shí)體類

用戶實(shí)體:

package com.qianfeng.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
 
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Register {
    private Integer id;
 
    private String userName;
 
    private String passWord;
 
    private String rePassWord;
 
    private String idCard;
    private String gender;
 
}

返回前端的實(shí)體類:包括查到的數(shù)據(jù)和分頁(yè)數(shù)據(jù)

package com.qianfeng.bean;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
 
import java.util.List;
 
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class RegPage {
    private PageInfo pageInfo;
    private List<Register> registers;
}

2.mapper層:

package com.qianfeng.mapper;
 
import com.qianfeng.bean.Register;
import org.apache.ibatis.annotations.Mapper;
 
import java.util.List;
@Mapper
public interface PageDao {
    List<Register> getAll(Integer startRow,Integer pageSize);
    long getCount();
}

3.service層:

package com.qianfeng.service;
 
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.RegPage;
 
public interface RegService {
    RegPage getAll(PageInfo pageInfo);
}

4.serviceImpl:

package com.qianfeng.service.serviceImpl;
 
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.RegPage;
import com.qianfeng.bean.Register;
import com.qianfeng.mapper.PageDao;
import com.qianfeng.service.RegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class RegServiceImpl implements RegService {
    @Autowired
    private PageDao pageDao;
    @Override
    public RegPage getAll(PageInfo pageInfo) {
        List<Register> all = pageDao.getAll(pageInfo.getStartRow(), pageInfo.getPageSize());//分頁(yè)后的數(shù)據(jù)
        long count = pageDao.getCount();//總記錄條數(shù)
        pageInfo.setTotal(count);
        //總頁(yè)數(shù)
        int pages= (int) (pageInfo.getTotal()%pageInfo.getPageSize()==0?pageInfo.getTotal()/pageInfo.getPageSize():pageInfo.getTotal()/pageInfo.getPageSize()+1);
        pageInfo.setPages(pages);
 
        RegPage regPage = new RegPage();
        regPage.setPageInfo(pageInfo);
        regPage.setRegisters(all);
        return regPage;
 
    }
}

5.handler層:

package com.qianfeng.handler;
 
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.RegPage;
import com.qianfeng.service.RegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class RegHandler {
    @Autowired
    private RegService regService;
    @RequestMapping("/page/{pageNum}")
    public RegPage regPage(@PathVariable("pageNum") Integer pageNum){
        System.out.println(".........");
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(3);
        pageInfo.setStartRow((pageNum-1)*pageInfo.getPageSize());
        System.out.println("startRow" + pageInfo.getStartRow());
        return regService.getAll(pageInfo);
    }
}

6.mapper.xml

<?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.qianfeng.mapper.PageDao">
    <select id="getAll" resultType="com.qianfeng.bean.Register">
        select * from m_register limit #{startRow},#{pageSize}
    </select>
    <select id="getCount" resultType="java.lang.Long">
        select count(*) from m_register
    </select>
</mapper>

7.application.yaml

spring:
  datasource:
    url: jdbc:mysql:///map?serverTimezone=Asia/Shanghai&useSSL=false
    username: root
    password: 123
    driver-class-name: com.mysql.jdbc.Driver
 
    druid:
      aop-patterns: com.qianfeng.*  #監(jiān)控SpringBean
      filters: stat,wall     # 底層開(kāi)啟功能,stat(sql監(jiān)控),wall(防火墻)
 
      stat-view-servlet:   # 配置監(jiān)控頁(yè)功能
        enabled: true
        login-username: admin
        login-password: admin
        resetEnable: false
 
      web-stat-filter:  # 監(jiān)控web
        enabled: true
        urlPattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
      filter:
        stat:    # 對(duì)上面filters里面的stat的詳細(xì)配置
          slow-sql-millis: 1000
          logSlowSql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
# mybatis的配置規(guī)則
mybatis:
  #config-location: classpath:mapper/mybatis-config.xml
  mapper-locations: classpath:mapper/*
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#      static-locations: [classpath:/haha/]  # 靜態(tài)資源路徑自定義

8.application,properties

spring.main.allow-circular-references=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

關(guān)于PageInfo這個(gè)類,源碼如下: 

public class PageInfo implements Serializable {
private static final long serialVersionUID = 1L;
//當(dāng)前頁(yè)
private int pageNum;
//每頁(yè)的數(shù)量
private int pageSize;
//當(dāng)前頁(yè)的數(shù)量
private int size;
//由于startRow 和endRow 不常用,這里說(shuō)個(gè)具體的用法
//可以在頁(yè)面中"顯示startRow 到endRow 共size 條數(shù)據(jù)"
//當(dāng)前頁(yè)面第一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
private int startRow;
//當(dāng)前頁(yè)面最后一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
private int endRow;
//總記錄數(shù)
private long total;
//總頁(yè)數(shù)
private int pages;
//結(jié)果集
private List list;
//前一頁(yè)
private int prePage;
//下一頁(yè)
private int nextPage;
//是否為第一頁(yè)
private boolean isFirstPage = false;
//是否為最后一頁(yè)
private boolean isLastPage = false;
//是否有前一頁(yè)
private boolean hasPreviousPage = false;
//是否有下一頁(yè)
private boolean hasNextPage = false;
//導(dǎo)航頁(yè)碼數(shù)
private int navigatePages;
//所有導(dǎo)航頁(yè)號(hào)
private int[] navigatepageNums;
//導(dǎo)航條上的第一頁(yè)
private int navigateFirstPage;
//導(dǎo)航條上的最后一頁(yè)
private int navigateLastPage;
}

目錄結(jié)構(gòu):

到此這篇關(guān)于PageHelper在springboot+mybatis框架中的使用步驟及原理的文章就介紹到這了,更多相關(guān)PageHelper在springboot+mybatis框架使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java list去重操作實(shí)現(xiàn)方式

    java list去重操作實(shí)現(xiàn)方式

    本文主要介紹了java list 去重的方法,其中有帶類型寫(xiě)法和不帶類型寫(xiě)法,并舉例測(cè)試,具有一定參考借鑒價(jià)值,希望能對(duì)有需要的小伙伴有所幫助
    2016-07-07
  • Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序介紹

    Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序介紹

    這篇文章主要介紹了Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 使用Zookeeper實(shí)現(xiàn)分布式鎖

    使用Zookeeper實(shí)現(xiàn)分布式鎖

    這篇文章主要介紹了使用Zookeeper實(shí)現(xiàn)分布式鎖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • java?stream實(shí)現(xiàn)分組BigDecimal求和以及自定義分組求和

    java?stream實(shí)現(xiàn)分組BigDecimal求和以及自定義分組求和

    這篇文章主要給大家介紹了關(guān)于java?stream實(shí)現(xiàn)分組BigDecimal求和以及自定義分組求和的相關(guān)資料,Stream是Java8的一大亮點(diǎn),是對(duì)容器對(duì)象功能的增強(qiáng),它專注于對(duì)容器對(duì)象進(jìn)行各種非常便利、高效的聚合操作或者大批量數(shù)據(jù)操作,需要的朋友可以參考下
    2023-12-12
  • 利用Spring JPA中的@Version注解實(shí)現(xiàn)樂(lè)觀鎖

    利用Spring JPA中的@Version注解實(shí)現(xiàn)樂(lè)觀鎖

    樂(lè)觀鎖是數(shù)據(jù)庫(kù)和應(yīng)用程序中使用的一種并發(fā)控制策略,用于在多個(gè)事務(wù)嘗試更新單個(gè)記錄時(shí)確保數(shù)據(jù)完整性,Java Persistence API (JPA) 提供了一種借助@Version注解在 Java 應(yīng)用程序中實(shí)現(xiàn)樂(lè)觀鎖的機(jī)制,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2023-11-11
  • 從Myeclipse 導(dǎo)入到eclipse中無(wú)法識(shí)別為 web項(xiàng)目 問(wèn)題的解決步驟

    從Myeclipse 導(dǎo)入到eclipse中無(wú)法識(shí)別為 web項(xiàng)目 問(wèn)題的解決步驟

    這篇文章主要介紹了從Myeclipse 導(dǎo)入到eclipse中無(wú)法識(shí)別為 web項(xiàng)目 問(wèn)題的解決步驟,需要的朋友可以參考下
    2018-05-05
  • 詳解批處理框架之Spring Batch

    詳解批處理框架之Spring Batch

    Spring Batch是一個(gè)輕量級(jí)的、完善的批處理框架,作為Spring體系中的一員,它擁有靈活、方便、生產(chǎn)可用的特點(diǎn)。在應(yīng)對(duì)高效處理大量信息、定時(shí)處理大量數(shù)據(jù)等場(chǎng)景十分簡(jiǎn)便。結(jié)合調(diào)度框架能更大地發(fā)揮Spring Batch的作用
    2021-06-06
  • 最新評(píng)論