MyBatis

您所在的位置:网站首页 mybatis联表查询并分页 MyBatis

MyBatis

2023-03-27 07:08| 来源: 网络整理| 查看: 265

在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 对象中:

MyBatis-Plus - 实现多表分页查询_Mapper

在项目开发当中,都会将分页的一些参数封装成一个类 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(long size, long current, long total) { this.size = size; this.current = current; this.total = total; setTotal(total); }}

功能编写

MyBatis-Plus - 实现多表分页查询_分页_02

MyBatis-Plus - 实现多表分页查询_分页_03

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

MyBatis-Plus - 实现多表分页查询_MyBatis-Plus_04

三、多表关联分页查询

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

先准备个VO类:

MyBatis-Plus - 实现多表分页查询_MyBatis-Plus_05

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

package com.eiot.e_view.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import 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 没有区别:

MyBatis-Plus - 实现多表分页查询_实现多表分页查询_06

MyBatis-Plus - 实现多表分页查询_MP_07

编写Server

MyBatis-Plus - 实现多表分页查询_分页_08

执行结果

MyBatis-Plus - 实现多表分页查询_MyBatis-Plus_09



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3