栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis联表查询:多对多(注解实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis联表查询:多对多(注解实现)

1、数据库表结构

2、返回结果类封装 CommentWithTag .java
@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 baseMapper {

    @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);
}
TagMapper
@Mapper
public interface TagMapper extends baseMapper {

    @Select("select * from tb_tag where id in (select tag_id from tb_comment_tag where comment_id = #{commentId})")
    List selectTagByCommentId(Integer commentId);
}
4、编写测试类
@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"])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号