@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(value = {"handler"})
public class CommentWithTag implements Serializable {
@ApiModelProperty(value="编号")
private Integer id;
@ApiModelProperty(value="图片,最多5张 [aa.jpg,bb.jpg,cc.jpg]")
private String pics;
@ApiModelProperty(value="评论内容")
private String content;
@ApiModelProperty(value="星级:1~5")
private Integer stars;
@ApiModelProperty(value="其他用户对这条评论的评论个数")
private Integer comments;
@ApiModelProperty(value="其他用户对这条评论的点赞个数")
private Integer agrees;
@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 titleList;
}
3、mapper层注解
CommentMapper
@Mapper public interface CommentMapper extends baseMapperTagMapper{ @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 selectCommentWithTagByUserId(Long userId); }
@Mapper public interface TagMapper extends baseMapper4、编写测试类{ @Select("select * from tb_tag where id in (select tag_id from tb_comment_tag where comment_id = #{commentId})") List selectTagByCommentId(Integer commentId); }
@SpringBootTest
class CommentMapperTest {
@Resource
private CommentMapper commentMapper;
@Test
void selectCommentWithTagByUserId(){
List 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"])



