mybatis-plus分頁(yè)查詢(xún)的實(shí)現(xiàn)示例
按照官方文檔進(jìn)行的配置:快速開(kāi)始|mybatis-plus
引入依賴(lài):
<!-- 引入mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- 引入mysql驅(qū)動(dòng)包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- 引入Druid依賴(lài),阿里巴巴所提供的數(shù)據(jù)源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency>
在application.yml配置
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456
在啟動(dòng)類(lèi)上面添加@MapperScan注解,掃描mapper包
@SpringBootApplication
@MapperScan("com.qiao.demo02.mapper")
public class SpringbootDemo02Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo02Application.class, args);
}
}
新建User和UserMapper

user類(lèi)
@Data
public class User {
@TableId
private Integer userId;
private String userName;
private Integer userAge;
private String userEmail;
}
UserMapper接口
public interface UserMapper extends BaseMapper<User> {
}
最重要的是繼承BaseMapper<E>接口:里面聲明了很強(qiáng)大的CRUD方法
public interface BaseMapper<T> extends Mapper<T> {
int insert(T entity);
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> wrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
}
分頁(yè)查詢(xún)
這點(diǎn)官方文檔講的也很詳細(xì):https://mp.baomidou.com/guide/page.html
新建一個(gè)config包,在里面建一個(gè)MybatisPlus配置類(lèi) 返回一個(gè)分頁(yè)攔截器
package com.qiao.demo02.config;
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
這樣就能使用mybatis的分頁(yè)功能了
Junit測(cè)試
@Resource
private UserMapper userMapper;
@Test
public void queryUserForPage(){
IPage<User> userPage = new Page<>(2, 2);//參數(shù)一是當(dāng)前頁(yè),參數(shù)二是每頁(yè)個(gè)數(shù)
userPage = userMapper.selectPage(userPage, null);
List<User> list = userPage.getRecords();
for(User user : list){
System.out.println(user);
}
}
Controller返回json串
先定義一個(gè)包裝類(lèi)UserVo,用來(lái)保存分頁(yè)所需要的數(shù)據(jù)
package com.qiao.demo02.vo;
@Data
public class UserVo {
private Integer current;
private Integer size;
private Long total;
private List<User> userList;
}
然后在控制器編寫(xiě)代碼,這里省略了service層,實(shí)際開(kāi)發(fā)業(yè)務(wù)代碼寫(xiě)在service層,Controller只負(fù)責(zé):接受參數(shù)、調(diào)用service層方法處理業(yè)務(wù)邏輯,返回結(jié)果
Controller類(lèi)貼上了@RestController注解
@GetMapping("queryUser")
public UserVo queryList(Integer current, Integer size) {
/**
* 這些代碼應(yīng)該寫(xiě)在service層
*/
UserVo userVo = new UserVo();
IPage<User> page = new Page<>(current, size);
userMapper.selectPage(page, null);
userVo.setCurrent(current);
userVo.setSize(size);
userVo.setTotal(page.getTotal());
userVo.setUserList(page.getRecords());
return userVo;
}
附上結(jié)果,前端直接處理json數(shù)據(jù)即可

到此這篇關(guān)于mybatis-plus分頁(yè)查詢(xún)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)mybatis-plus 分頁(yè)查詢(xún) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深度剖析java中JDK動(dòng)態(tài)代理機(jī)制
本篇文章主要介紹了深度剖析java中JDK動(dòng)態(tài)代理機(jī)制 ,動(dòng)態(tài)代理避免了開(kāi)發(fā)人員編寫(xiě)各個(gè)繁鎖的靜態(tài)代理類(lèi),只需簡(jiǎn)單地指定一組接口及目標(biāo)類(lèi)對(duì)象就能動(dòng)態(tài)的獲得代理對(duì)象。2017-04-04
Java數(shù)據(jù)結(jié)構(gòu)之順序表詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之順序表詳解,線(xiàn)性表在邏輯上是線(xiàn)性結(jié)構(gòu),也就說(shuō)是連續(xù)的一條直線(xiàn)。但是在物理結(jié)構(gòu)上并不一定是連續(xù)的,線(xiàn)性表在物理上存儲(chǔ)時(shí),通常以數(shù)組和鏈?zhǔn)浇Y(jié)構(gòu)的形式存儲(chǔ),需要的朋友可以參考下2023-07-07
SpringBoot Mybatis Plus公共字段自動(dòng)填充功能
這篇文章主要介紹了SpringBoot Mybatis Plus公共字段自動(dòng)填充功能的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java?FTP協(xié)議實(shí)現(xiàn)文件下載功能
FTP(File?Transfer?Protocol)就是文件傳輸協(xié)議。通過(guò)FTP客戶(hù)端從遠(yuǎn)程FTP服務(wù)器上拷貝文件到本地計(jì)算機(jī)稱(chēng)為下載,將本地計(jì)算機(jī)上的文件復(fù)制到遠(yuǎn)程FTP服務(wù)器上稱(chēng)為上傳,上傳和下載是FTP最常用的兩個(gè)功能2022-11-11
Spring boot定時(shí)任務(wù)的原理及動(dòng)態(tài)創(chuàng)建詳解
這篇文章主要給大家介紹了關(guān)于Spring boot定時(shí)任務(wù)的原理及動(dòng)態(tài)創(chuàng)建的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java靜態(tài)方法不能調(diào)用非靜態(tài)成員的原因分析
在Java中,靜態(tài)方法是屬于類(lèi)的方法,而不是屬于對(duì)象的方法,它可以通過(guò)類(lèi)名直接調(diào)用,無(wú)需創(chuàng)建對(duì)象實(shí)例,非靜態(tài)成員指的是類(lèi)的實(shí)例變量和實(shí)例方法,它們需要通過(guò)對(duì)象實(shí)例才能訪(fǎng)問(wèn)和調(diào)用,本文小編將和大家一起探討Java靜態(tài)方法為什么不能調(diào)用非靜態(tài)成員2023-10-10
關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問(wèn)題
這篇文章主要介紹了關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
@Accessors(chain = true)注解報(bào)錯(cuò)的解決方案
這篇文章主要介紹了@Accessors(chain = true)注解報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java將字節(jié)轉(zhuǎn)換為十六進(jìn)制代碼分享
我們知道,在java中,一個(gè)byte 就是一個(gè)字節(jié),也就是八個(gè)二進(jìn)制位;而4個(gè)二進(jìn)制位就可以表示一個(gè)十六進(jìn)制位,所以一個(gè)byte可以轉(zhuǎn)化為2個(gè)十六進(jìn)制位。下面我們就來(lái)詳細(xì)看下具體方法吧。2016-01-01

