mybatis plus多对多场景分页

相信大家都会用分页插件或mybtais plus分页,相对来说多对多分页还是比较少见的,以下记录分享:


在一次角色管理的场景中,前端要求我把角色的列表和角色拥有的菜单id一并给他,这很明显是多对多的情况,但是在列表的情况下都是要分页的,于是写了一个联表查询,但前端告诉我添加的数据查不出来,经过排查发现mybais plus的分页是在联表查询的基础上进行的分页,于是有了下面这些操作;特此记录:

这里用的是mybtatis的插件,先配置下分页

@Configuration
public class MybatisPlusConfig {
    /**
     * 分页插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor paginationInnerInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor =new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }
}

这里说明下场景:

  • 角色表- role [记录角色信息]
  • 角色菜单关系表- role_menu [记录角色和菜单的id]
  • 菜单表- menu [记录菜单权限等信息]

我们主要查询角色和角色拥有的菜单,多对多


Mapper接口

    /**
     * 查询角色的信息
     * @param page
     * @return
     */
    Page<RoleListVo> getRoleListInfo(Page<RoleListVo> page);
    

映射实体类:

/**
 * @author: MR.rp
 * @Date: 2021/12/23
 * @Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RoleListVo extends Role {

    @ApiModelProperty("菜单id")
    private List<Integer> menuIds;


}
/**
 * <p>
 * 用户角色表
 * </p>
 *
 * @author MR.RP
 * @since 2021-12-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="Role对象", description="用户角色表")
@AllArgsConstructor
@NoArgsConstructor
public class Role implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "部门id")
    private Integer departmentId;

    @ApiModelProperty(value = "角色名")
    private String name;

    @ApiModelProperty(value = "是否删除")
    @TableField(fill = FieldFill.INSERT)
    @TableLogic
    private Integer isDel;

    @ApiModelProperty(value = "描述")
    private String remarks;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date gmtCreate;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date gmtModified;


}
    <!-- 通用查询映射结果 -->
    <resultMap id="RoleListInfoResultMap" type="com.rubik.huaxianzi.entity.vo.RoleListVo">
        <id column="id" property="id" />
        <result column="department_id" property="departmentId" />
        <result column="name" property="name" />
        <result column="is_del" property="isDel" />
        <result column="remarks" property="remarks" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
        <collection property="menuIds"  ofType="int" column="id"  select="selectMenu">
            <result column="menu_id"/>
        </collection>
    </resultMap>
    <select id="getRoleListInfo" resultMap="RoleListInfoResultMap">
        select  role.*  from   role  where  is_del = 0
    </select>
    <select id="selectMenu" resultType="int">
        select  menu.id  as   menu_id  from  menu
        inner   join  role_menu  on  menu.id = role_menu.menu_id
        where   role_menu.role_id  = #{id}
    </select>

我们试着用接口调用一下:
在这里插入图片描述

总结:
我们在查询时将查询sql拆分成两个,以传参的形式,以column字段传入,传入后写入关联的sql关系即可;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值