實(shí)現(xiàn)分頁(yè)有很多種方式借助數(shù)組進(jìn)行分頁(yè)
原理:進(jìn)行數(shù)據(jù)庫(kù)查詢操作時(shí),獲取到數(shù)據(jù)庫(kù)中所有滿足條件的記錄數(shù)據(jù)庫(kù)映射文件系統(tǒng) , 保存在應(yīng)用的臨時(shí)數(shù)組中,再通過(guò)List的方法 , 獲取到滿足條件的所有記錄 。
借助Sql語(yǔ)句進(jìn)行分頁(yè)
實(shí)現(xiàn):通過(guò)sql語(yǔ)句實(shí)現(xiàn)分頁(yè)也是非常簡(jiǎn)單的,只是需要改變我們查詢的語(yǔ)句就能實(shí)現(xiàn)了數(shù)據(jù)庫(kù)映射文件系統(tǒng),即在sql語(yǔ)句后面添加limit分頁(yè)語(yǔ)句 。
攔截器分頁(yè)
自定義攔截器實(shí)現(xiàn)了攔截所有以結(jié)尾的查詢語(yǔ)句,并且利用獲取到的分頁(yè)相關(guān)參數(shù)統(tǒng)一在sql語(yǔ)句后面加上limit分頁(yè)的相關(guān)語(yǔ)句,一勞永逸 。
實(shí)現(xiàn)分頁(yè)
原理:通過(guò)實(shí)現(xiàn)分頁(yè)和通過(guò)數(shù)組方式分頁(yè)原理差不多,都是一次獲取所有符合條件的數(shù)據(jù),然后在內(nèi)存中對(duì)大數(shù)據(jù)進(jìn)行操作,實(shí)現(xiàn)分頁(yè)效果 。只是數(shù)組分頁(yè)需要我們自己去實(shí)現(xiàn)分頁(yè)邏輯,這里更加簡(jiǎn)化而已 。
第三方分頁(yè)插件
如常用的-plus、 。
今天要講的是自定義分頁(yè)
自定義分頁(yè)
首先還是在接口中添加sql語(yǔ)句查詢的方法,如下:
/*** 自定義分頁(yè)查詢* @param data* @return*/List queryStudentsBySql(Map data);
然后在.xml文件中編寫sql語(yǔ)句通過(guò)limiy關(guān)鍵字進(jìn)行分頁(yè):
select * from t_student limit #{currIndex} , #{pageSize}
接下來(lái)還是在中對(duì)sql分頁(yè)實(shí)現(xiàn) 。
public List queryStudentsBySql(int currPage, int pageSize) {Map data = https://www.jianzixun.com/new HashMap();data.put("currIndex", (currPage-1)*pageSize);data.put("pageSize", pageSize);return studentMapper.queryStudentsBySql(data);}
中調(diào)用方法
@GetMapping("getByPage/{currPage}/{pageSize}")public List getByPage(@PathVariable int currPage,@PathVariable int pageSize) {return studentService.queryStudentsBySql(currPage,pageSize);}
sql分頁(yè)語(yǔ)句如下: * from table limit index, ;
所以在中計(jì)算出:要開始查詢的第一條記錄的索引 。
全部代碼如下:
mysql
CREATE TABLE `t_student` (`id` int NOT NULL AUTO_INCREMENT,`city_no` varchar(30) DEFAULT NULL,`city_name` varchar(30) DEFAULT NULL,`city_type` varchar(30) DEFAULT NULL,`p_city_no` varchar(30) DEFAULT NULL,`stu_no` varchar(30) DEFAULT NULL,`stu_name` varchar(30) DEFAULT NULL,`stu_grade_no` varchar(30) DEFAULT NULL,`stu_grade_name` varchar(30) DEFAULT NULL,`stu_class_no` varchar(30) DEFAULT NULL,`stu_class_name` varchar(30) DEFAULT NULL,`exam_time` varchar(30) DEFAULT NULL,`course` varchar(255) DEFAULT NULL,`score` int DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
【springboot集成mybatis自定義分頁(yè)】代碼架構(gòu)
pom.xml
4.0.0org.springframework.bootspring-boot-starter-parent2.6.4com.fishmybatis0.0.1-SNAPSHOTmybatisSpring Boot集成mybatis1.8org.mybatis.spring.bootmybatis-spring-boot-starter2.2.2org.springframework.bootspring-boot-starter-testtestmysqlmysql-connector-javaorg.projectlomboklombok1.18.20org.springframework.bootspring-boot-starter-webRELEASEcompileorg.springframework.bootspring-boot-maven-plugin
.yml
spring:profiles:active: dev
-dev.yml
server:port: 8080spring:datasource:username: usernamepassword: passwordurl: jdbc:mysql://192.168.80.128:3301/test_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8driver-class-name: com.mysql.cj.jdbc.Drivermybatis:# 配置mapper的掃描,找到所有的mapper.xml映射文件# 搜索指定包別名typeAliasesPackage: com.fish.**.mappermapperLocations: classpath*:mapper/*Mapper.xml
.java實(shí)體類
package com.fish.mybatis.entity;import lombok.Data;/** * @author fish */@Datapublic class Student {private Integer id;private String cityNo;private String cityName;private String cityType;private String pCityNo;private String stuNo;private String stuName;private String stuGradeNo;private String stuGradeName;private String stuClassNo;private String stuClassName;private String examTime;private String course;private Integer score;}
.java
package com.fish.mybatis.mapper;import com.fish.mybatis.entity.Student;import com.fish.mybatis.entity.User;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;import java.util.List;import java.util.Map;/** * @author fish */@Repositorypublic interface StudentMapper {/*** 根據(jù)主鍵查詢* @param id* @return*/Student getById(int id);/*** 根據(jù)全部學(xué)生* @return*/List getByAll();/*** 自定義分頁(yè)查詢* @param data* @return*/List queryStudentsBySql(Map data);}
.xml
select * from t_student where id = #{id}SELECTu.*FROMt_student uselect * from t_student limit #{currIndex} , #{pageSize}
.java
package com.fish.mybatis.service;import com.fish.mybatis.entity.Student;import com.fish.mybatis.mapper.StudentMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author fish */@Servicepublic class StudentService {@AutowiredStudentMapper studentMapper;public Student getById(int id) {return studentMapper.getById(id);}public List getByAll() {List studentList=studentMapper.getByAll();return studentList;}public List queryStudentsBySql(int currPage, int pageSize) {Map data = https://www.jianzixun.com/new HashMap();data.put("currIndex", (currPage-1)*pageSize);data.put("pageSize", pageSize);return studentMapper.queryStudentsBySql(data);}}
.java
package com.fish.mybatis.controller;import com.fish.mybatis.entity.Student;import com.fish.mybatis.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * @author fish */@RestController@RequestMapping("/student")public class StudentController {@Autowiredprivate StudentService studentService;@RequestMapping("getById/{id}")public Student getById(@PathVariable int id) {return studentService.getById(id);}@RequestMapping("getByAll")public List getByAll() {return studentService.getByAll();}@GetMapping("getByPage/{currPage}/{pageSize}")public List getByPage(@PathVariable int currPage,@PathVariable int pageSize) {return studentService.queryStudentsBySql(currPage,pageSize);}}
.java
package com.fish.mybatis;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author fish */@MapperScan("com.fish.mybatis.mapper")@SpringBootApplicationpublic class MybatisApplication {public static void main(String[] args) {SpringApplication.run(MybatisApplication.class, args);}}
測(cè)試:
在瀏覽器輸入:8080///3/3獲取第一頁(yè)的數(shù)據(jù),每頁(yè)顯示兩條數(shù)據(jù) 。
結(jié)果:
從輸出結(jié)果可以看出和數(shù)組分頁(yè)的結(jié)果是一致的,因此sql語(yǔ)句的分頁(yè)也是沒(méi)問(wèn)題的 。
在實(shí)際工作中有些很復(fù)雜的SQL使用第三方分頁(yè)插件不靠譜還是需要自己實(shí)現(xiàn)分頁(yè)的 。
本文到此結(jié)束,希望對(duì)大家有所幫助 。
