mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢(xún)的示例代碼
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。它提供了代碼生成器、條件構(gòu)造器、分頁(yè)插件等多種功能,其中分頁(yè)查詢(xún)是一個(gè)常用的功能。
以下是如何在 MyBatis-Plus 中實(shí)現(xiàn)分頁(yè)查詢(xún)的基本步驟:
1. 引入 MyBatis-Plus 分頁(yè)插件依賴(lài)
首先,確保你的項(xiàng)目中已經(jīng)添加了 MyBatis-Plus 的依賴(lài),并且包含了分頁(yè)插件。如果沒(méi)有,可以在 pom.xml 中添加如下依賴(lài):
<!-- MyBatis-Plus 分頁(yè)插件 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>你的MyBatis-Plus版本</version>
</dependency>2. 配置分頁(yè)插件
在你的 Spring Boot 配置類(lèi)中,添加分頁(yè)插件的配置:
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}3. 使用分頁(yè)查詢(xún)
在你的 Mapper 接口中,你可以使用 MyBatis-Plus 提供的 IPage<T> 類(lèi)型來(lái)接收分頁(yè)參數(shù),并返回分頁(yè)結(jié)果。
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import your.package.name.entity.YourEntity;
@Mapper
public interface YourEntityMapper extends BaseMapper<YourEntity> {
// 這里定義你的 CRUD 操作
}在你的服務(wù)層或控制器層,你可以這樣使用分頁(yè)查詢(xún):
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import your.package.name.mapper.YourEntityMapper;
import your.package.name.entity.YourEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class YourEntityService {
@Autowired
private YourEntityMapper yourEntityMapper;
public Page<YourEntity> selectPage(int current, int size) {
Page<YourEntity> page = new Page<>(current, size);
return yourEntityMapper.selectPage(page, null); // 第二個(gè)參數(shù)可以是查詢(xún)條件,這里為 null 表示查詢(xún)所有
}
}4. 控制器層調(diào)用
在你的控制器中,你可以接收前端傳遞的分頁(yè)參數(shù),并調(diào)用服務(wù)層的方法:
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 your.package.name.entity.YourEntity;
import your.package.name.service.YourEntityService;
import java.util.List;
@RestController
public class YourEntityController {
@Autowired
private YourEntityService yourEntityService;
@GetMapping("your-entity/list")
public Page<YourEntity> list(@RequestParam(defaultValue = "1") int current,
@RequestParam(defaultValue = "10") int size) {
return yourEntityService.selectPage(current, size);
}
}這樣,當(dāng)請(qǐng)求到達(dá)控制器的 list 方法時(shí),就會(huì)執(zhí)行分頁(yè)查詢(xún),并返回分頁(yè)結(jié)果。
請(qǐng)注意,這里的 your.package.name 需要替換為你的實(shí)際包名,YourEntity 和 YourEntityMapper 需要替換為你的實(shí)際實(shí)體類(lèi)和 Mapper 接口。
到此這篇關(guān)于mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢(xún)的示例代碼的文章就介紹到這了,更多相關(guān)mybatis-plus 分頁(yè)查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus分頁(yè)查詢(xún)的實(shí)現(xiàn)實(shí)例
- SpringBoot使用mybatis-plus分頁(yè)查詢(xún)無(wú)效的問(wèn)題解決
- SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢(xún)功能
- mybatis-plus多表分頁(yè)查詢(xún)最佳實(shí)現(xiàn)方法(非常簡(jiǎn)單)
- mybatis-plus分頁(yè)查詢(xún)?nèi)N方法小結(jié)
- Mybatis-plus分頁(yè)查詢(xún)不生效問(wèn)題排查全過(guò)程
- 如何使用mybatis-plus實(shí)現(xiàn)分頁(yè)查詢(xún)功能
- 一文搞懂Mybatis-plus的分頁(yè)查詢(xún)操作
- MyBatis-Plus?分頁(yè)查詢(xún)的實(shí)現(xiàn)示例
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢(xún)功能
- mybatis-plus分頁(yè)查詢(xún)的實(shí)現(xiàn)示例
相關(guān)文章
Zookeeper實(shí)現(xiàn)分布式鎖代碼實(shí)例
這篇文章主要介紹了Zookeeper實(shí)現(xiàn)分布式鎖代碼實(shí)例,Zookeeper?分布式鎖應(yīng)用了其?臨時(shí)順序節(jié)點(diǎn)?的特性,在Zookeeper中創(chuàng)建一個(gè)持久節(jié)點(diǎn)ParentLock,當(dāng)?shù)谝粋€(gè)客戶(hù)端要獲取鎖時(shí),在ParentLock節(jié)點(diǎn)下創(chuàng)建一個(gè)臨時(shí)順序節(jié)點(diǎn),需要的朋友可以參考下2023-12-12
springboot項(xiàng)目防止XSS攻擊和sql注入方式
這篇文章主要介紹了springboot項(xiàng)目防止XSS攻擊和sql注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
spring的同一定時(shí)任務(wù)上一次的任務(wù)未結(jié)束前不會(huì)啟動(dòng)這次任務(wù)問(wèn)題
這篇文章主要介紹了spring的同一定時(shí)任務(wù)上一次的任務(wù)未結(jié)束前不會(huì)啟動(dòng)這次任務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(條件隊(duì)列)
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02

