如何使用mybatis-plus實(shí)現(xiàn)分頁(yè)查詢(xún)功能
今天就跟大家聊聊有關(guān)使用mybatis-plus如何實(shí)現(xiàn)分頁(yè)查詢(xún)功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
引入依賴(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接口:里面聲明了很強(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ù)即可
看完上述內(nèi)容,你們對(duì)使用mybatis-plus如何實(shí)現(xiàn)分頁(yè)查詢(xún)功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注小編,感謝大家的支持。
總結(jié)
到此這篇關(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的分頁(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)示例
- mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢(xún)的示例代碼
相關(guān)文章
javaWeb連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)簡(jiǎn)單登陸注冊(cè)功能的全過(guò)程
初學(xué)javaWeb,老師留下一小作業(yè),用JAVA實(shí)現(xiàn)與服務(wù)器端交互,實(shí)現(xiàn)登錄和注冊(cè)功能,下面這篇文章主要給大家介紹了關(guān)于javaWeb連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)簡(jiǎn)單登陸注冊(cè)功能的相關(guān)資料,需要的朋友可以參考下2022-06-06lambda表達(dá)式與傳統(tǒng)接口函數(shù)實(shí)現(xiàn)方式對(duì)比詳解
這篇文章主要為大家介紹了lambda表達(dá)式與傳統(tǒng)接口函數(shù)實(shí)現(xiàn)方式對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家度偶多進(jìn)步早日升職加薪2022-03-03詳解Java中的數(shù)組與字符串相關(guān)知識(shí)
這篇文章主要介紹了詳解Java中的數(shù)組與字符串相關(guān)知識(shí),包括操作字符串的一些基本方法列舉,需要的朋友可以參考下2015-09-09JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn)
本文主要介紹了JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09全面了解java byte數(shù)組與文件讀寫(xiě)
下面小編就為大家?guī)?lái)一篇全面了解java byte數(shù)組與文件讀寫(xiě)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法
這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03