mybatis plus 联表查询分页问题

1.主体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("blog")
@ApiModel(value = "博客")
public class Blog extends BaseEntity {
    @TableId
    @ApiModelProperty("博客id")
    private Long blogId;

    @ApiModelProperty(value = "博客标题")
    private String blogTitle;

    @ApiModelProperty(value = "博客内容")
    private String content;

    @ApiModelProperty(value = "博客markdown内容")
    private String mkContent;

    @ApiModelProperty(value = "用户id")
    private long userId;

    @ApiModelProperty(value = "分类id")
    private long typeId;

    @ApiModelProperty(value = "封面路径")
    private String coverUrl;

    @ApiModelProperty(value = "博客摘要")
    private String blogAbstract;

    @ApiModelProperty(value = "标签列表")
    @TableField(exist = false)
    private List<String> tags;
}

2.mapper

重点在于 collection 的select字段,select绑定的是下方的方法id,
在进行博客查询时会自动调用getTagsByBlogId方法查询标签,
可以解决联表查询的分页问题

<?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.pw.mapper.BlogMapper">
    <resultMap type="com.pw.vo.BlogVO" id="blogMap">
        <id property="blogId" column="blog_id"/>
        <result property="blogTitle" column="blog_title"/>
        <result property="content" column="content"/>
        <result property="mkContent" column="mk_content"/>
        <result property="userId" column="user_id"/>
        <result property="typeId" column="type_id"/>
        <collection property="tags" javaType="list" ofType="String" select="getTagsByBlogId" column="{blogId = blog_id}">
            <result column="tag_name"/>
        </collection>
    </resultMap>

    <!--博客列表查询-->
    <select id="list" resultMap="blogMap">
        select
        b.blog_id,
        b.blog_title,
        b.content,
        b.mk_content,
        b.user_id,
        b.type_id,
        b.create_time,
        b.update_time
        from `blog` b
        <where>
            <if test="blogTitle != null and blogTitle!=''">
                and b.blog_title like "%${blogTitle}%"
            </if>
            <if test="userId != null and userId!=''">
                and
                b.user_id = "${userId}"
            </if>
            <if test="typeId != null and typeId!=''">
                and
                b.type_id = "${typeId}"
            </if>
            <if test="startTime != null and startTime!='' and endTime != null and endTime!=''">
                and
                b.create_time between startTime and endTime
            </if>
        </where>
        order by create_time desc
        <if test="pageNum != null and pageNum!='' and pageSize != null and pageSize!=''">
            LIMIT ${(pageNum-1)*pageSize},${pageSize}
        </if>
    </select>

    <select id="getTagsByBlogId" resultType="String">
        select
        btr.tag_name
        from `blog` b
        join `blog_tag_relation` btr ON b.blog_id = btr.blog_id
    </select>
</mapper>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值