mybatis-plus多表分頁查詢最佳實現(xiàn)方法(非常簡單)
1.簡介
在Mybatis Plus 中,雖然IService 接口幫我們定義了很多常用的方法,但這些都是 T 對象有用,如果涉及到 多表的查詢,還是需要自定義Vo 對象和自己編寫sql 語句,Mybatis Plus提供了一個Page 對象,查詢是需要設(shè)置其中的 size 字段 和 current 字段的值。
mybatis-plus的單表分頁就不必多說了,那多表聯(lián)查的分頁該如何實現(xiàn)呢?其實也很簡單,你只需要自己寫好關(guān)聯(lián)查詢的sql再結(jié)合mybatis-plus提供的分頁對象,就可以實現(xiàn)了。但是如何才能優(yōu)雅的將分頁參數(shù)和查詢條件提供給mybatis-plus呢?我選擇使用
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;這個對象來實現(xiàn)。直接繼承baomidou提供的類可以省去每次手動再new對象,因為在獲取參數(shù)時就已經(jīng)自動構(gòu)建了
2.實現(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分頁插件
@MapperScan(“。。。”)這些基本配置我就不多說了
@Configuration
public class MPConfig {
/**
* 分頁插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//樂觀鎖
//interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//分頁鎖
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}2.3 分頁參數(shù) 繼承 自 Page
直接繼承baomidou提供的類可以省去每次手動再new對象,因為在獲取參數(shù)時就已經(jī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;
/**
* 每頁顯示條數(shù),默認(rèn) 10
*/
@ApiParam(value = "每頁大小,默認(rèn)10",required = false, defaultValue = "10")
private long size = 10;
/**
* 當(dāng)前頁
*/
@ApiParam(value = "當(dāng)前頁,默認(rèn)1",required = false,defaultValue = "1")
private long current = 1;
/**
* 是否進行 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 實現(xià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("角色列表分頁查詢")
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> {
/**
* 分頁查詢角色
*/
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多表分頁查詢最佳實現(xiàn)方法的文章就介紹到這了,更多相關(guān)mybatis-plus多表分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring?java?動態(tài)獲取consul?K/V的方法
這篇文章主要介紹了spring?java?動態(tài)獲取consul?K/V的相關(guān)資料,主要包括springConsul配置kv路徑以及自動注入consulKV到服務(wù)中,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
SpringSecurity rememberme功能實現(xiàn)過程解析
這篇文章主要介紹了SpringSecurity rememberme功能實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

