mybatis-plus 實(shí)現(xiàn)分頁查詢的示例代碼
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。它提供了代碼生成器、條件構(gòu)造器、分頁插件等多種功能,其中分頁查詢是一個(gè)常用的功能。
以下是如何在 MyBatis-Plus 中實(shí)現(xiàn)分頁查詢的基本步驟:
1. 引入 MyBatis-Plus 分頁插件依賴
首先,確保你的項(xiàng)目中已經(jīng)添加了 MyBatis-Plus 的依賴,并且包含了分頁插件。如果沒有,可以在 pom.xml 中添加如下依賴:
<!-- MyBatis-Plus 分頁插件 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>你的MyBatis-Plus版本</version>
</dependency>2. 配置分頁插件
在你的 Spring Boot 配置類中,添加分頁插件的配置:
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. 使用分頁查詢
在你的 Mapper 接口中,你可以使用 MyBatis-Plus 提供的 IPage<T> 類型來接收分頁參數(shù),并返回分頁結(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ù)層或控制器層,你可以這樣使用分頁查詢:
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ù)可以是查詢條件,這里為 null 表示查詢所有
}
}4. 控制器層調(diào)用
在你的控制器中,你可以接收前端傳遞的分頁參數(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)請求到達(dá)控制器的 list 方法時(shí),就會(huì)執(zhí)行分頁查詢,并返回分頁結(jié)果。
請注意,這里的 your.package.name 需要替換為你的實(shí)際包名,YourEntity 和 YourEntityMapper 需要替換為你的實(shí)際實(shí)體類和 Mapper 接口。
到此這篇關(guān)于mybatis-plus 實(shí)現(xiàn)分頁查詢的示例代碼的文章就介紹到這了,更多相關(guān)mybatis-plus 分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus分頁查詢的實(shí)現(xiàn)實(shí)例
- SpringBoot使用mybatis-plus分頁查詢無效的問題解決
- SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁查詢功能
- mybatis-plus多表分頁查詢最佳實(shí)現(xiàn)方法(非常簡單)
- mybatis-plus分頁查詢?nèi)N方法小結(jié)
- Mybatis-plus分頁查詢不生效問題排查全過程
- 如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能
- 一文搞懂Mybatis-plus的分頁查詢操作
- MyBatis-Plus?分頁查詢的實(shí)現(xiàn)示例
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能
- mybatis-plus分頁查詢的實(shí)現(xiàn)示例
相關(guān)文章
Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡及Eureka與Ribbon的應(yīng)用小結(jié)
這篇文章主要介紹了Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡:Eureka與Ribbon的應(yīng)用,通過使用Eureka和Ribbon,我們可以在Java項(xiàng)目中實(shí)現(xiàn)高效的服務(wù)發(fā)現(xiàn)和負(fù)載均衡,需要的朋友可以參考下2024-08-08
springboot集成websocket的四種方式小結(jié)
本文主要介紹了springboot集成websocket的四種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Java實(shí)現(xiàn)限定時(shí)間CountDownLatch并行場景
本文將結(jié)合實(shí)例代碼,介紹Java實(shí)現(xiàn)限定時(shí)間CountDownLatch并行場景,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
Vue結(jié)合Springboot實(shí)現(xiàn)用戶列表單頁面(前后端分離)
本文主要介紹了Vue結(jié)合Springboot實(shí)現(xiàn)用戶列表單頁面,可以實(shí)現(xiàn)簡單的查詢,刪除,修改,和添加用戶信息功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
淺談Mybatis中resultType為hashmap的情況
這篇文章主要介紹了淺談Mybatis中resultType為hashmap的情況,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

