Mybatis-Plus 多表聯(lián)查分頁(yè)的實(shí)現(xiàn)代碼
上一節(jié),簡(jiǎn)單講述了 Mybatis-Plus 搭建與使用入門,這一節(jié),簡(jiǎn)單講一下如何使用 MP 實(shí)現(xiàn)多表分頁(yè)。
分析
使用的工程,依舊是 spring-boot,關(guān)于分頁(yè),官網(wǎng)給出了一個(gè)單表的demo,其實(shí)多表分頁(yè)實(shí)現(xiàn)原理相同,都是通過(guò) mybatis 的攔截器
(攔截器做了什么?他會(huì)在你的 sql 執(zhí)行之前,為你做一些事情,例如分頁(yè),我們使用了 MP 不用關(guān)心 limit,攔截器為我們拼接。我們也不用關(guān)心總條數(shù),攔截器獲取到我們 sql 后,拼接 select count(*)
為我們查詢總條數(shù),添加到參數(shù)對(duì)象中)。
實(shí)現(xiàn)
1. 配置攔截器
@EnableTransactionManagement @Configuration @MapperScan("com.web.member.mapper") public class MybatisPlusConfig { /** * mybatis-plus SQL執(zhí)行效率插件【生產(chǎn)環(huán)境可以關(guān)閉】 */ @Bean public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); } /* * 分頁(yè)插件,自動(dòng)識(shí)別數(shù)據(jù)庫(kù)類型 多租戶,請(qǐng)參考官網(wǎng)【插件擴(kuò)展】 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
2. mapper 接口以及 xml
/** * <p> * 用戶表 Mapper 接口 * </p> * * @author 殷天文 * @since 2018-06-01 */ public interface UserMapper extends BaseMapper<User> { List<UserListModel> selectUserListPage(Pagination page ,@Param("user") UserListBean user); }
這里要注意的是,這個(gè) Pagination page 是必須要有的,否則 MP 無(wú)法為你實(shí)現(xiàn)分頁(yè)。
<select id="selectUserListPage" resultType="com.web.member.model.UserListModel"> SELECT * FROM ftms_user u LEFT JOIN ftms_user_level l ON u.level_id = l.id WHERE 1=1 <if test="user.nickname != null"> and u.nickname like "%"#{user.nickname}"%" </if> </select>
3. service 實(shí)現(xiàn)
import com.web.member.beans.admin.UserListBean; import com.web.member.entity.User; import com.web.member.mapper.UserMapper; import com.web.member.model.UserListModel; import com.web.member.service.UserService; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * <p> * 用戶表 服務(wù)實(shí)現(xiàn)類 * </p> * * @author 殷天文 * @since 2018-06-01 */ @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Transactional(readOnly=true) @Override public Page<UserListModel> selectUserListPage(UserListBean user) { Page<UserListModel> page = new Page<>(user.getCurr(), user.getNums());// 當(dāng)前頁(yè),總條數(shù) 構(gòu)造 page 對(duì)象 return page.setRecords(this.baseMapper.selectUserListPage(page, user)); } }
最后將結(jié)果集 set 到 page 對(duì)象中,page 對(duì)象的 json 結(jié)構(gòu)如下
{ "total": 48,//總記錄 "size": 10,//每頁(yè)顯示多少條 "current": 1,//當(dāng)前頁(yè) "records": [//結(jié)果集 數(shù)組 {...}, {...}, {...}, ... ], "pages": 5 // 總頁(yè)數(shù) }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用mybatis-plus分頁(yè)查詢無(wú)效的問題解決
- MyBatis-Plus 分頁(yè)查詢以及自定義sql分頁(yè)的實(shí)現(xiàn)
- MyBatis-Plus分頁(yè)插件不生效的解決方法
- 解決mybatis plus 一對(duì)多分頁(yè)查詢問題
- MyBatis-Plus實(shí)現(xiàn)分頁(yè)的方法使用詳解
- MyBatis-Plus實(shí)現(xiàn)2種分頁(yè)方法(QueryWrapper查詢分頁(yè)和SQL查詢分頁(yè))
- MyBatis-Plus分頁(yè)時(shí)排序的實(shí)現(xiàn)方法
- Mybatis-Plus如何使用分頁(yè)實(shí)例詳解
- Mybatis-plus原生pages分頁(yè)未生效的解決方案
- mybatis-plus分頁(yè)無(wú)效問題解決
相關(guān)文章
?Java數(shù)據(jù)結(jié)構(gòu)的十大排序
這篇文章主要介紹了?Java數(shù)據(jù)結(jié)構(gòu)的十大排序,排序算法分為比較類排序和非比較類排序,具體的內(nèi)容,需要的朋友參考下面思維導(dǎo)圖及文章介紹,希望對(duì)你有所幫助2022-01-01Hibernate 與 Mybatis 的共存問題,打破你的認(rèn)知!(兩個(gè)ORM框架)
這篇文章主要介紹了Hibernate 與 Mybatis 如何共存?本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08java8新特性之stream的collect實(shí)戰(zhàn)教程
這篇文章主要介紹了java8新特性之stream的collect實(shí)戰(zhàn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java BufferedImage轉(zhuǎn)換為MultipartFile方式
這篇文章主要介紹了Java BufferedImage轉(zhuǎn)換為MultipartFile方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java實(shí)現(xiàn)兩個(gè)線程交替打印的實(shí)例代碼
在本篇文章里小編給大家整理的是一篇關(guān)于java實(shí)現(xiàn)兩個(gè)線程交替打印的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。2019-12-12