MyBatis Plus 实现多表分页查询功能的示例代码

网友投稿 730 2022-11-24


MyBatis Plus 实现多表分页查询功能的示例代码

在Mybatis Plus 中,虽然IService 接口帮我们定义了很多常用的方法,但这些都是 T 对象有用,如果涉及到 多表的查询,还是需要自定义Vo 对象和自己编写sql 语句,Mybatis Plus提供了一个Page 对象,查询是需要设置其中的 size 字段 和 current 字段的值

一、分页配置

可以直接使用selectPage这样的分页,但返回的数据确实是分页后的数据,但在控制台打印的SQL语句其实并没有真正的物理分页,而是通过缓存来获得全部数据中再进行的分页,这样对于大数据量操作时是不可取的,那么接下来就叙述一下,真正实现物理分页的方法。

官方在分页插件上如是描述:自定义查询语句分页(自己写sql/mapper),也就是针对自己在Mapper中写的方法,但经过测试,如果不配置分页插件,其默认采用的分页为RowBounds的分页即逻辑分页,也就是先把数据记录全部查询出来,然在再根据offset和limit截断记录返回(数据量大的时候会造成内存溢出),故而不可取,而通过分页插件的配置即可达到物理分页效果。

新建一个MybatisPlusConfig配置类文件,代码如下所示:

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分页插件

*/

@Bean

public PaginationInterceptor paginationInterceptor() {

PaginationInterceptor paginationInterceptor = new PaginationInterceptor();

return paginationInterceptor;

}

}

二、使用分页进行单表的查询

对于单表的分页查询,ServiceImpl 类已经为我们提供了对应的方法 selectPage(),并将结果封装到Page 对象中:

在项目开发当中,都会将分页的一些参数封装成一个类 PageReq(不要在意这个Req 为什么不是全大写)->import java.io.Serializable;

public class PageReq implements Serializable {

/**

* 每页显示大小

*/

private long size;

/**

* 当前页码

*/

private long current;

/**

* 最大页数

*/

private long maxCurrent;

/**

* 数据总条数

*/

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(lonbVVCdxEg size, long current, long total) {

this.size = size;

this.current = current;

this.total = total;

setTotal(total);

}

}

功能编写:

执行完之后,会将查询的接口封装到我们 Page的 对象中:

三、多表关联分页查询

对于多表关联的查询时,还是需要编写 VO 类和 手动的在Mapper.xml 中编写sql,虽然是可以不用创建VO,用Map 的方式接受返回的结果,但这样只会更麻烦,甚至VO 是很有可能在其他地方使用的

先准备个VO类:

编写Mapper接口,添加一个分页查询的方法

package com.eiot.e_view.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

impbVVCdxEort 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 {

List getRoomPageList(Page page, @Param("roomPageReq")RoomPageReq roomPageReq);

}

编写sql,和我们使用Mybatis 没有区别:

编写Server :

执行结果:

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java pom.xml parent引用报错问题解决方案
下一篇:Spring Boot Admin 动态修改日志级别的方法步骤
相关文章

 发表评论

暂时没有评论,来抢沙发吧~