mybatis-plus多表分頁(yè)查詢最佳實(shí)現(xiàn)方法(非常簡(jiǎn)單)
1.簡(jiǎn)介
在Mybatis Plus 中,雖然IService 接口幫我們定義了很多常用的方法,但這些都是 T 對(duì)象有用,如果涉及到 多表的查詢,還是需要自定義Vo 對(duì)象和自己編寫sql 語(yǔ)句,Mybatis Plus提供了一個(gè)Page 對(duì)象,查詢是需要設(shè)置其中的 size 字段 和 current 字段的值。
mybatis-plus的單表分頁(yè)就不必多說(shuō)了,那多表聯(lián)查的分頁(yè)該如何實(shí)現(xiàn)呢?其實(shí)也很簡(jiǎn)單,你只需要自己寫好關(guān)聯(lián)查詢的sql再結(jié)合mybatis-plus提供的分頁(yè)對(duì)象,就可以實(shí)現(xiàn)了。但是如何才能優(yōu)雅的將分頁(yè)參數(shù)和查詢條件提供給mybatis-plus呢?我選擇使用
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;這個(gè)對(duì)象來(lái)實(shí)現(xiàn)。直接繼承baomidou提供的類可以省去每次手動(dòng)再new對(duì)象,因?yàn)樵讷@取參數(shù)時(shí)就已經(jīng)自動(dòng)構(gòu)建了
2.實(shí)現(xiàn)
2.1版本
<mybatis-plus.version>3.5.1</mybatis-plus.version>
<connector.version>8.0.18</connector.version> <!-- ================mybatis-plus================== -->
<!--mybatis-plus 持久層-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- mybatis plus 代碼生成器依賴 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- ================mybatis-plus================== -->
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${connector.version}</version>
</dependency>2.2分頁(yè)插件
@MapperScan(“。。。”)這些基本配置我就不多說(shuō)了
@Configuration
public class MPConfig {
/**
* 分頁(yè)插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//樂(lè)觀鎖
//interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//分頁(yè)鎖
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}2.3 分頁(yè)參數(shù) 繼承 自 Page
直接繼承baomidou提供的類可以省去每次手動(dòng)再new對(duì)象,因?yàn)樵讷@取參數(shù)時(shí)就已經(jīng)自動(dòng)構(gòu)建了
package com.dxf.common.utils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
public class PageParam<T> extends Page<T> {
/**
* 查詢數(shù)據(jù)列表
*/
@ApiParam(hidden = true)
private List<T> records;
/**
* 總數(shù)
*/
@ApiParam(hidden = true)
private long total = 0;
/**
* 每頁(yè)顯示條數(shù),默認(rèn) 10
*/
@ApiParam(value = "每頁(yè)大小,默認(rèn)10",required = false, defaultValue = "10")
private long size = 10;
/**
* 當(dāng)前頁(yè)
*/
@ApiParam(value = "當(dāng)前頁(yè),默認(rèn)1",required = false,defaultValue = "1")
private long current = 1;
/**
* 是否進(jìn)行 count 查詢
*/
@ApiParam(hidden = true)
private boolean isSearchCount = true;
@Override
@ApiParam(hidden = true)
public List<T> getRecords() {
return this.records;
}
@Override
public Page<T> setRecords(List<T> records) {
this.records = records;
return this;
}
@Override
public long getTotal() {
return this.total;
}
@Override
public Page<T> setTotal(long total) {
this.total = total;
return this;
}
@ApiParam(hidden = true)
public boolean getSearchCount() {
if (total < 0) {
return false;
}
return isSearchCount;
}
@Override
@ApiParam(hidden = true)
public boolean isSearchCount() {
if (total < 0) {
return false;
}
return isSearchCount;
}
@Override
public Page<T> setSearchCount(boolean isSearchCount) {
this.isSearchCount = isSearchCount;
return this;
}
@Override
public long getSize() {
return this.size;
}
@Override
public Page<T> setSize(long size) {
this.size = size;
return this;
}
@Override
public long getCurrent() {
return this.current;
}
@Override
public Page<T> setCurrent(long current) {
this.current = current;
return this;
}
}2.4 實(shí)現(xiàn)的重點(diǎn)mapper
import org.apache.ibatis.annotations.Param;
public interface SysRoleMapper extends BaseMapper<SysRole> {
IPage<SysRole> searchPage(PageParam<SysRole> pageParam, @Param("name") String name);
}關(guān)聯(lián)的是角色表和用戶表
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dxf.data.mapper.SysRoleMapper">
<select id="searchPage" resultType="com.dxf.data.entity.SysRole">
SELECT r.id,r.name,r.status,r.role_sort, a.name create_by ,r.create_time,r.update_time,r.remark
FROM sys_role r left join sys_user a on r.create_by=a.id
<where>
<if test="name != null and name != ''">
r.name like concat(#{name},'%')
</if>
</where>
order by r.role_sort
</select>
</mapper>2.5 其他層就是傳下參數(shù)
@RestController
@RequestMapping("/admin/role")
@Api(tags = "SysRoleControllr|角色控制器")
public class SysRoleController {
@Autowired
SysRoleService roleService;
@GetMapping("/page")
@ApiOperation("角色列表分頁(yè)查詢")
public ResultJson page(PageParam<SysRole> pageParam,String likeKey) {
IPage<SysRole> iPage = roleService.searchPage(pageParam,likeKey);
Map<String, Object> map = new HashMap<>();
map.put("rows",iPage.getRecords());
map.put("total",iPage.getTotal());
return ResultJson.ok().data(map);
}
public interface SysRole
}
Service extends IService<SysRole> {
/**
* 分頁(yè)查詢角色
*/
IPage<SysRole> searchPage(PageParam<SysRole> pageParam,String name);
}
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
@Override
public IPage<SysRole> searchPage(PageParam<SysRole> pageParam,String name) {
return baseMapper.searchPage(pageParam,name);
}
}總結(jié)
到此這篇關(guān)于mybatis-plus多表分頁(yè)查詢最佳實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)mybatis-plus多表分頁(yè)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot詳解RocketMQ實(shí)現(xiàn)廣播消息流程
RocketMQ作為一款純java、分布式、隊(duì)列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時(shí)消息、消息回溯等,本篇我們了解如何實(shí)現(xiàn)廣播消息2022-06-06
使用Java實(shí)現(xiàn)查看線程的運(yùn)行狀態(tài)(附源碼)
在現(xiàn)代 Java 應(yīng)用中,線程的運(yùn)行狀態(tài)對(duì)于排查問(wèn)題和優(yōu)化性能具有至關(guān)重要的作用,本文將使用Java編寫一個(gè)查看線程運(yùn)行狀態(tài)的工具,有需要的可以了解下2025-03-03
Java中FutureTask 和 CompletableFuture的區(qū)別
本文主要介紹了Java中FutureTask 和 CompletableFuture的區(qū)別嗎,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2026-02-02
關(guān)于Java中finalize析構(gòu)方法的作用詳解
構(gòu)造方法用于創(chuàng)建和初始化類對(duì)象,也就是說(shuō),構(gòu)造方法負(fù)責(zé)”生出“一個(gè)類對(duì)象,并可以在對(duì)象出生時(shí)進(jìn)行必要的操作,在這篇文章中會(huì)給大家簡(jiǎn)單介紹一下析構(gòu)方法,需要的朋友可以參考下2023-05-05
json序列化時(shí)忽略值為null的字段2種方式實(shí)例
這篇文章主要給大家介紹了關(guān)于json序列化時(shí)忽略值為null的字段的2種方式,當(dāng)對(duì)象中某個(gè)字段為null時(shí),我們希望將對(duì)象轉(zhuǎn)換成json時(shí)為null的字段不會(huì)被轉(zhuǎn)換到j(luò)son字符串,里面需要的朋友可以參考下2023-10-10
springboot啟動(dòng)mongoDB報(bào)錯(cuò)之禁用mongoDB自動(dòng)配置問(wèn)題
這篇文章主要介紹了springboot啟動(dòng)mongoDB報(bào)錯(cuò)之禁用mongoDB自動(dòng)配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

