如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能
今天就跟大家聊聊有關(guān)使用mybatis-plus如何實(shí)現(xiàn)分頁查詢功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
引入依賴:
<!-- 引入mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- 引入mysql驅(qū)動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- 引入Druid依賴,阿里巴巴所提供的數(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
在啟動類上面添加@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類
@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接口:里面聲明了很強(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);
}
分頁查詢
這點(diǎn)官方文檔講的也很詳細(xì):https://mp.baomidou.com/guide/page.html
新建一個config包,在里面建一個MybatisPlus配置類 返回一個分頁攔截器
package com.qiao.demo02.config;
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
這樣就能使用mybatis的分頁功能了
Junit測試
@Resource
private UserMapper userMapper;
@Test
public void queryUserForPage(){
IPage<User> userPage = new Page<>(2, 2);//參數(shù)一是當(dāng)前頁,參數(shù)二是每頁個數(shù)
userPage = userMapper.selectPage(userPage, null);
List<User> list = userPage.getRecords();
for(User user : list){
System.out.println(user);
}
}
Controller返回json串
先定義一個包裝類UserVo,用來保存分頁所需要的數(shù)據(jù)
package com.qiao.demo02.vo;
@Data
public class UserVo {
private Integer current;
private Integer size;
private Long total;
private List<User> userList;
}
然后在控制器編寫代碼,這里省略了service層,實(shí)際開發(fā)業(yè)務(wù)代碼寫在service層,Controller只負(fù)責(zé):接受參數(shù)、調(diào)用service層方法處理業(yè)務(wù)邏輯,返回結(jié)果
Controller類貼上了@RestController注解
@GetMapping("queryUser")
public UserVo queryList(Integer current, Integer size) {
/**
* 這些代碼應(yīng)該寫在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ù)即可

看完上述內(nèi)容,你們對使用mybatis-plus如何實(shí)現(xiàn)分頁查詢功能有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注小編,感謝大家的支持。
總結(jié)
到此這篇關(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的分頁查詢操作
- MyBatis-Plus?分頁查詢的實(shí)現(xiàn)示例
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能
- mybatis-plus分頁查詢的實(shí)現(xiàn)示例
- mybatis-plus 實(shí)現(xiàn)分頁查詢的示例代碼
相關(guān)文章
javaWeb連接數(shù)據(jù)庫實(shí)現(xiàn)簡單登陸注冊功能的全過程
初學(xué)javaWeb,老師留下一小作業(yè),用JAVA實(shí)現(xiàn)與服務(wù)器端交互,實(shí)現(xiàn)登錄和注冊功能,下面這篇文章主要給大家介紹了關(guān)于javaWeb連接數(shù)據(jù)庫實(shí)現(xiàn)簡單登陸注冊功能的相關(guān)資料,需要的朋友可以參考下2022-06-06
lambda表達(dá)式與傳統(tǒng)接口函數(shù)實(shí)現(xiàn)方式對比詳解
這篇文章主要為大家介紹了lambda表達(dá)式與傳統(tǒng)接口函數(shù)實(shí)現(xiàn)方式對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家度偶多進(jìn)步早日升職加薪2022-03-03
JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn)
本文主要介紹了JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫的配置方法
這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫的配置方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03

