久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

瀏覽:113日期:2023-10-22 19:53:47

在Mybatis Plus 中,雖然IService 接口幫我們定義了很多常用的方法,但這些都是 T 對(duì)象有用,如果涉及到 多表的查詢(xún),還是需要自定義Vo 對(duì)象和自己編寫(xiě)sql 語(yǔ)句,Mybatis Plus提供了一個(gè)Page 對(duì)象,查詢(xún)是需要設(shè)置其中的 size 字段 和 current 字段的值

一、分頁(yè)配置

可以直接使用selectPage這樣的分頁(yè),但返回的數(shù)據(jù)確實(shí)是分頁(yè)后的數(shù)據(jù),但在控制臺(tái)打印的SQL語(yǔ)句其實(shí)并沒(méi)有真正的物理分頁(yè),而是通過(guò)緩存來(lái)獲得全部數(shù)據(jù)中再進(jìn)行的分頁(yè),這樣對(duì)于大數(shù)據(jù)量操作時(shí)是不可取的,那么接下來(lái)就敘述一下,真正實(shí)現(xiàn)物理分頁(yè)的方法。官方在分頁(yè)插件上如是描述:自定義查詢(xún)語(yǔ)句分頁(yè)(自己寫(xiě)sql/mapper),也就是針對(duì)自己在Mapper中寫(xiě)的方法,但經(jīng)過(guò)測(cè)試,如果不配置分頁(yè)插件,其默認(rèn)采用的分頁(yè)為RowBounds的分頁(yè)即邏輯分頁(yè),也就是先把數(shù)據(jù)記錄全部查詢(xún)出來(lái),然在再根據(jù)offset和limit截?cái)嘤涗浄祷兀〝?shù)據(jù)量大的時(shí)候會(huì)造成內(nèi)存溢出),故而不可取,而通過(guò)分頁(yè)插件的配置即可達(dá)到物理分頁(yè)效果。

新建一個(gè)MybatisPlusConfig配置類(lèi)文件,代碼如下所示:

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; @Configuration@EnableTransactionManagement(proxyTargetClass = true)public class MybatisPlusConfig { /** * mybatis-plus分頁(yè)插件<br> */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; }}

二、使用分頁(yè)進(jìn)行單表的查詢(xún)

對(duì)于單表的分頁(yè)查詢(xún),ServiceImpl 類(lèi)已經(jīng)為我們提供了對(duì)應(yīng)的方法 selectPage(),并將結(jié)果封裝到Page 對(duì)象中:

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

在項(xiàng)目開(kāi)發(fā)當(dāng)中,都會(huì)將分頁(yè)的一些參數(shù)封裝成一個(gè)類(lèi) PageReq(不要在意這個(gè)Req 為什么不是全大寫(xiě))->import java.io.Serializable;

public class PageReq implements Serializable { /** * 每頁(yè)顯示大小 */ private long size; /** * 當(dāng)前頁(yè)碼 */ private long current; /** * 最大頁(yè)數(shù) */ private long maxCurrent; /** * 數(shù)據(jù)總條數(shù) */ private long total; public long getSize() { return size; } public void setSize(long size) { this.size = size; } public long getCurrent() { return current; } public void setCurrent(long current) { this.current = current; } public long getMaxCurrent() { return maxCurrent; } public void setMaxCurrent(long maxCurrent) { this.maxCurrent = maxCurrent; } public long getTotal() { return total; } public void setTotal(long total) { if(size != 0){ if(total % size != 0){maxCurrent = total / size + 1; }else {maxCurrent = total / size; } } } public PageReq() { } public PageReq(long size, long current, long total) { this.size = size; this.current = current; this.total = total; setTotal(total); }}

功能編寫(xiě):

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

執(zhí)行完之后,會(huì)將查詢(xún)的接口封裝到我們 Page的 對(duì)象中:

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

三、多表關(guān)聯(lián)分頁(yè)查詢(xún)

對(duì)于多表關(guān)聯(lián)的查詢(xún)時(shí),還是需要編寫(xiě) VO 類(lèi)和 手動(dòng)的在Mapper.xml 中編寫(xiě)sql,雖然是可以不用創(chuàng)建VO,用Map 的方式接受返回的結(jié)果,但這樣只會(huì)更麻煩,甚至VO 是很有可能在其他地方使用的先準(zhǔn)備個(gè)VO類(lèi):

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

編寫(xiě)Mapper接口,添加一個(gè)分頁(yè)查詢(xún)的方法package com.eiot.e_view.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.eiot.e_view.model.req.RoomPageReq;import com.eiot.e_view.model.vo.RoomVO;import org.apache.ibatis.annotations.Param;import java.util.List;public interface RoomMapper extends BaseMapper<Room> { List<RoomVO> getRoomPageList(Page page, @Param('roomPageReq')RoomPageReq roomPageReq);}

編寫(xiě)sql,和我們使用Mybatis 沒(méi)有區(qū)別:

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

編寫(xiě)Server :

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

執(zhí)行結(jié)果:

MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼

總結(jié)

到此這篇關(guān)于MyBatis Plus 實(shí)現(xiàn)多表分頁(yè)查詢(xún)功能的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis Plus 多表分頁(yè)查詢(xún)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

相關(guān)文章:
主站蜘蛛池模板: 天峻县| 肇东市| 旬阳县| 孟州市| 扶绥县| 奎屯市| 龙口市| 抚州市| 维西| 南阳市| 福州市| 汝城县| 古蔺县| 方城县| 五大连池市| 清苑县| 大石桥市| 寻甸| 普宁市| 大丰市| 色达县| 井研县| 林州市| 沙田区| 蓝山县| 宜宾市| 汾阳市| 洛宁县| 河池市| 保亭| 集贤县| 江门市| 临桂县| 余干县| 凭祥市| 桦南县| 黑龙江省| 林甸县| 姚安县| 察雅县| 新野县|