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>