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)文章
spring?java?動(dòng)態(tài)獲取consul?K/V的方法
這篇文章主要介紹了spring?java?動(dòng)態(tài)獲取consul?K/V的相關(guān)資料,主要包括springConsul配置kv路徑以及自動(dòng)注入consulKV到服務(wù)中,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
WebSocket無(wú)法注入屬性的問(wèn)題及解決方案
這篇文章主要介紹了WebSocket無(wú)法注入屬性的問(wèn)題及解決方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
JavaEE實(shí)現(xiàn)前后臺(tái)交互的文件上傳與下載
這篇文章主要介紹了JavaEE實(shí)現(xiàn)前后臺(tái)交互的文件上傳與下載,分享相關(guān)技術(shù),實(shí)現(xiàn)文件上傳下載功能,需要的朋友可以參考下2015-11-11
SpringBoot上傳文件大小受限問(wèn)題的解決辦法
最近有一次由于項(xiàng)目升級(jí)發(fā)現(xiàn)了一個(gè)上傳方面的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于SpringBoot上傳文件大小受限問(wèn)題的解決辦法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Spring-Boot 訪問(wèn)外部接口的方案總結(jié)
在Spring-Boot項(xiàng)目開發(fā)中,存在著本模塊的代碼需要訪問(wèn)外面模塊接口,或外部url鏈接的需求,針對(duì)這一需求目前存在著三種解決方案,下面將對(duì)這三種方案進(jìn)行整理和說(shuō)明,對(duì)Spring-Boot 訪問(wèn)外部接口方案感興趣的朋友跟隨小編一起看看吧2022-12-12
java時(shí)間戳轉(zhuǎn)換為日期格式的多種方式
本文介紹了五種將Java時(shí)間戳轉(zhuǎn)換為日期格式的方法,包括使用Date類、LocalDateTime類、Instant類、DateUtils類以及自定義時(shí)區(qū),每種方法都有其適用場(chǎng)景,可以根據(jù)具體需求選擇合適的方法,感興趣的朋友跟隨小編一起看看吧2025-01-01
Java MAVEN 工程pom配置報(bào)錯(cuò)解決方案
這篇文章主要介紹了Java MAVEN 工程pom配置報(bào)錯(cuò)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

