1、数据库表结构
2、返回结果类封装
CommentWithTag .java
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(value = {"handler"})
public class CommentWithTag implements Serializable {
/**
* 编号
*/
@ApiModelProperty(value="编号")
private Integer id;
/**
* 图片,最多5张 [aa.jpg,bb.jpg,cc.jpg]
*/
@ApiModelProperty(value="图片,最多5张 [aa.jpg,bb.jpg,cc.jpg]")
private String pics;
/**
* 评论内容
*/
@ApiModelProperty(value="评论内容")
private String content;
/**
* 星级:1~5
*/
@ApiModelProperty(value="星级:1~5")
private Integer stars;
/**
* 其他用户对这条评论的评论个数
*/
@ApiModelProperty(value="其他用户对这条评论的评论个数")
private Integer comments;
/**
* 其他用户对这条评论的点赞个数
*/
@ApiModelProperty(value="其他用户对这条评论的点赞个数")
private Integer agrees;
/**
* 上一级评论编号,如果为null表示是购买者评论
*/
@ApiModelProperty(value="上一级评论编号,如果为null表示是购买者评论")
private Integer pid;
/**
* 评论时间
*/
@TableField(value = "create_time")
@ApiModelProperty(value="评论时间")
private LocalDateTime createTime;
/**
* 状态
*/
@TableField(value = "`status`")
@ApiModelProperty(value="状态")
private Integer status;
/**
* 标签内容列表
*/
@ApiModelProperty(value="标签内容列表")
private List<String> titleList;
}
3、mapper层注解
CommentMapper
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
@Select("select * from tb_comment where user_id = #{userId}")
@Results({
@Result(id = true,column = "id",property = "id"),
@Result(column = "pics",property = "pics"),
@Result(column = "content",property = "content"),
@Result(column = "stars",property = "stars"),
@Result(column = "comments",property = "comments"),
@Result(column = "agrees",property = "agrees"),
@Result(column = "pid",property = "pid"),
@Result(column = "createTime",property = "create_time"),
@Result(column = "status",property = "status"),
@Result(
column = "id",property = "titleList",
many = @Many(select = "com.hc.mapper.TagMapper.selectTagByCommentId",fetchType = FetchType.LAZY )
)
})
List<CommentWithTag> selectCommentWithTagByUserId(Long userId);
}
TagMapper
@Mapper
public interface TagMapper extends BaseMapper<Tag> {
@Select("select * from tb_tag where id in (select tag_id from tb_comment_tag where comment_id = #{commentId})")
List<Tag> selectTagByCommentId(Integer commentId);
}
4、编写测试类
@SpringBootTest
class CommentMapperTest {
@Resource
private CommentMapper commentMapper;
@Test
void selectCommentWithTagByUserId(){
List<CommentWithTag> commentWithTags = commentMapper.selectCommentWithTagByUserId(220L);
System.out.println(JsonUtil.obj2String(commentWithTags));
}
}
注意
返回的结果类必须加下面这行注解
@JsonIgnoreProperties(value = {"handler"})
否则产生错误信息
No serializer found for class org.apache.ibatis.executor.loader.javassist
.JavassistProxyFactory$EnhancedResultObjectProxyImpl
and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
(through reference chain: java.util.ArrayList[0]
->com.hc.res.CommentWithTag_$$_jvst497_0["handler"])