JAVA后端分頁三種方式思想


第一種  :springboot+mybatis+pageHelper

第二種: 利用 listMap.subList(firstIndex, lastIndex) ,listMap是數組,firstIndex,是開始元素,lastIndex是結束元素但不包括lastIndex。 (本人目前用的比較多)

第三種:利用數據庫自身的limit關鍵字  limit 

 select * from table limit (pageNum-1)*pageSize,pageSize

第一種

1.概念:PageHelper 是一款好用的開源免費的 Mybatis 第三方物理分頁插件,可以支持多個數據庫,應用mybatis

2.引入依賴:

<!-- 這個pagehelper好像適用於spring,對springboot不適用,給個提醒-->
<!--<dependency>-->
<!--<groupId>com.github.pagehelper</groupId>-->
<!--<artifactId>pagehelper</artifactId>-->
<!--<version>5.0.3</version>-->
<!--</dependency>-->

<!--分頁插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>

3.application.yml

server:
port: 8088

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
username: root
password: zhang

mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.yiyezhiqiu.jwt.jwt.domain
configuration:
map-underscore-to-camel-case: true

logging:
level:
com:
yiyezhiqiu:
jwt:
jwt:
dao: debug

4.數據庫表:

 

5.domain層

  5.1.Goods實體類

package com.yiyezhiqiu.jwt.jwt.domain;

import lombok.Data;

import java.math.BigDecimal;

@Data
public class Goods {

private int id;

private String goodsType;

private BigDecimal goodsPrice;

private Double goodsWeight;

private String goodsShape;
}

  5.2 分頁傳入參數實體類
  
package com.yiyezhiqiu.jwt.jwt.domain;

import lombok.Data;

/**
* 分頁實體類
*/

@Data
public class PageHelpParam {

/**
* 當前頁
*/
private String currentPageParam ;

/**
* 當前頁條數
*/
private String pageSizeParam;

/**
* 模糊查詢參數fuzzy
*/
private String fuzzy;

/**
* 模糊查詢參數 nextFuzzy
*/

private String nextFuzzy;
}




6.service層
  6.1 接口:IGoodsService
  
package com.yiyezhiqiu.jwt.jwt.service;


import com.yiyezhiqiu.jwt.jwt.domain.PageHelpParam;

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

public interface IGoodsService {

public Map findPageHelp(PageHelpParam pageHelpParam);

}

  6.2.實現類:GoodsServiceImpl

package com.yiyezhiqiu.jwt.jwt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yiyezhiqiu.jwt.jwt.dao.GoodsDao;
import com.yiyezhiqiu.jwt.jwt.domain.Goods;
import com.yiyezhiqiu.jwt.jwt.domain.PageHelpParam;
import com.yiyezhiqiu.jwt.jwt.service.IGoodsService;
import com.yiyezhiqiu.jwt.jwt.service.IUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
* 實現分頁
*/
@Service
@Slf4j
public class GoodsServiceImpl implements IGoodsService {


@Autowired
IGoodsService goodsService;
@Autowired
GoodsDao goodsDao;
@Override
public Map findPageHelp(PageHelpParam pageHelpParam) {
String currentPageParam = pageHelpParam.getCurrentPageParam();
String pageSizeParam = pageHelpParam.getPageSizeParam();
log.info("currentPageParam:"+currentPageParam);
log.info("pageSizeParam:"+pageSizeParam);
int currenPage = 1;
int pageSize = 10;
if(StringUtils.isEmpty(currentPageParam)){
currenPage = 1;
}else{
currenPage = Integer.parseInt(currentPageParam);
}
if(StringUtils.isEmpty(pageSizeParam)){
pageSize = 10;
}else{
pageSize = Integer.parseInt(pageSizeParam);

}

//分頁條件查詢參數
PageHelper.startPage(currenPage,pageSize);//頁數和每頁大小

/**
* 獲得要分頁的集合,
* 1.這里獲得集合,如果你要給參數到數據庫,比如模糊查詢,就可以在PageHelpParam中添加相關字段(在實現了分頁后,做了下模糊查詢)
* 2.這里是返回某個表的實體類,當然你也可以用一個vo類,也就是不同表字段返回在一個vo類中使用
*/
String fuzzy = pageHelpParam.getFuzzy();
String nextFuzzy = pageHelpParam.getNextFuzzy();
Map<String,Object> mapParam = new HashMap<>();
if(null != fuzzy && fuzzy != ""){
mapParam.put("fuzzy",fuzzy);
}

if(null != nextFuzzy && fuzzy != ""){
mapParam.put("nextFuzzy",nextFuzzy);
}

List<Goods> goodsList = goodsDao.findAll(mapParam);


//這里可以看下PageInfo為啥會和PageHelper有關系,因為startPage會返回page,PageInfo中使用page,這就是聯系,可以去看源碼
PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);

List<Goods> goods = new ArrayList<>();
if(!CollectionUtils.isEmpty(goodsList)){
for(Goods good:pageInfo.getList()){
goods.add(good);
}
}

Map<String,Object> map = new HashMap<>();
map.put("total",pageInfo.getTotal());
map.put("currentPage",pageInfo.getPageNum());
map.put("pageSize", pageInfo.getPageSize());

Map<String ,Object> maps = new HashMap<>();
maps.put("pageInfo",map);
maps.put("goods",goods);

return maps;
}
}

 7.controller

package com.yiyezhiqiu.jwt.jwt.controller;

import com.github.pagehelper.PageHelper;
import com.yiyezhiqiu.jwt.jwt.annotation.OtherPermit;
import com.yiyezhiqiu.jwt.jwt.domain.PageHelpParam;
import com.yiyezhiqiu.jwt.jwt.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

@Autowired
IGoodsService goodsService;

@RequestMapping(value = "/findGoods",method = RequestMethod.GET)
public Map findGoodsPageHelper(PageHelpParam pageHelpParam){

Map<String,Object> map = goodsService.findPageHelp(pageHelpParam);

return map;
}

}


8.GoodsMapping.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.yiyezhiqiu.jwt.jwt.dao.GoodsDao">

<!--這里
IFNULL(A,B) 意思是如果A不為空,則為A,否則為B
CONCAT(多個IFNULL)實現一個框就可以分別對多個字段進行模糊查詢
-->
<select id="findAll" resultType="com.yiyezhiqiu.jwt.jwt.domain.Goods">
select * from goods
<where>
<if test= "fuzzy != null">
IFNULL(goods_type ," ") like "%"#{fuzzy}"%"
</if>
<if test="nextFuzzy != null">
and CONCAT(IFNULL(goods_price," "),IFNULL(goods_shape," ")) like "%"#{nextFuzzy}"%"
</if>

</where>
</select>

</mapper>
9.postman測試:
9.1:
    當只為分頁時,不模糊篩選

 

 
        

     9.2:

        當為輸入goods_type單個參數篩選時

 

 

     9.3 :

      當可以是goods_weight 或者goods_shape篩選時

 

 

 

 

 

github地址:https://github.com/yiyeluowuchen/springboot-jwt-token.git

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM