MyBatis-Plus實現(xiàn)對查詢結(jié)果進行分頁的基本步驟
MyBatis-Plus 是一個 MyBatis 的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。MyBatis-Plus 支持多種數(shù)據(jù)庫的分頁查詢,其分頁功能是通過 Page
類實現(xiàn)的。
以下是使用 MyBatis-Plus 實現(xiàn)分頁查詢的基本步驟:
添加依賴:首先確保你的項目中已經(jīng)添加了 MyBatis-Plus 的依賴。
配置 Mapper 接口:創(chuàng)建一個 Mapper 接口,該接口繼承自
BaseMapper<T>
,其中T
是你的實體類。創(chuàng)建 Service:在 Service 層中,你可以注入 Mapper 接口,并調(diào)用其分頁查詢的方法。
使用 Page 類:創(chuàng)建一個
Page
對象,設(shè)置當前頁碼和每頁顯示的記錄數(shù)。調(diào)用分頁查詢:在 Mapper 接口中定義一個分頁查詢的方法,使用
@Select
注解或者 XML 映射文件來指定查詢語句。然后在 Service 層調(diào)用這個方法,傳入Page
對象。處理結(jié)果:分頁查詢的結(jié)果會返回一個
PageInfo<T>
對象,其中包含了當前頁的數(shù)據(jù)和分頁信息。
好的,用一個案例來具體看一下,如何使用 MyBatis-Plus 進行分頁查詢。假設(shè)我們有一個需求是,在用戶管理系統(tǒng),需要對用戶列表進行分頁顯示。
- 實體類(User.java):
public class User { private Long id; private String name; private Integer age; private String email; // 省略其他字段和getter/setter方法 }
- Mapper 接口(UserMapper.java):
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public interface UserMapper extends BaseMapper<User> { // 這里可以添加自定義的查詢方法,例如按狀態(tài)查詢 Page<User> selectByState(Page<User> page, @Param("state") Integer state); }
- Mapper XML 文件(UserMapper.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.example.mapper.UserMapper"> <!-- 按狀態(tài)查詢用戶的分頁結(jié)果 --> <select id="selectByState" resultType="com.example.entity.User"> SELECT * FROM user WHERE state = #{state} </select> </mapper>
- Service 接口(IUserService.java):
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.core.metadata.IPage; import com.example.entity.User; public interface IUserService extends IService<User> { IPage<User> getUserListByState(Integer state, int current, int size); }
- Service 實現(xiàn)(UserServiceImpl.java):
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.mapper.UserMapper; import com.example.entity.User; import com.baomidou.mybatisplus.core.metadata.IPage; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { @Override public IPage<User> getUserListByState(Integer state, int current, int size) { IPage<User> page = this.page(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), null); if (state != null) { return this.baseMapper.selectByState(page, state); } return page; } }
- Controller(UserController.java):
import com.example.service.IUserService; import com.baomidou.mybatisplus.core.metadata.IPage; import com.example.entity.User; 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; @RestController public class UserController { @Autowired private IUserService userService; @GetMapping("/users") public IPage<User> getUsers(@RequestParam(defaultValue = "0") Integer state, @RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) { return userService.getUserListByState(state, current, size); } }
- 啟動類(Application.java):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
- application.properties:
# 配置數(shù)據(jù)庫連接信息 spring.datasource.url=jdbc:mysql://localhost:3306/userdb?useSSL=false&serverTimezone=UTC spring.datasource.username=weige spring.datasource.password=wg123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 配置 MyBatis-Plus mybatis-plus.mapper-locations=classpath:/mapper/*.xml mybatis-plus.type-aliases-package=com.vin.entity
以上就是MyBatis-Plus實現(xiàn)對查詢結(jié)果進行分頁的基本步驟的詳細內(nèi)容,更多關(guān)于MyBatis-Plus查詢結(jié)果分頁的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java調(diào)用shell腳本解決傳參和權(quán)限問題的方法
今天小編就為大家分享一篇關(guān)于Java調(diào)用shell腳本解決傳參和權(quán)限問題的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Java中Set集合轉(zhuǎn)為List集合常見的兩種方式
List是Java中比較常用的集合類,指一系列存儲數(shù)據(jù)的接口和類,可以解決復(fù)雜的數(shù)據(jù)存儲問題,這篇文章主要給大家介紹了關(guān)于Java中Set集合轉(zhuǎn)為List集合常見的兩種方式,需要的朋友可以參考下2023-12-12SpringBoot返回統(tǒng)一的JSON標準格式實現(xiàn)步驟
這篇文章主要介紹了SpringBoot返回統(tǒng)一的JSON標準格式,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Java數(shù)據(jù)結(jié)構(gòu)及算法實例:三角數(shù)字
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:三角數(shù)字,本文直接給出實現(xiàn)代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06idea運行tomcat報錯找不到catalina.bat,系統(tǒng)找不到指定的文件問題
這篇文章主要介紹了idea運行tomcat報錯找不到catalina.bat,系統(tǒng)找不到指定的文件問題,具有很好的參考價值,希望對大家有所幫助,2023-11-11SpringBoot+Response如何統(tǒng)一返回result結(jié)果集
這篇文章主要介紹了SpringBoot+Response如何統(tǒng)一返回result結(jié)果集,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05