标签:column private ApiModelProperty value 联表 Mybatis 注解 property Result
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"])
标签:column,private,ApiModelProperty,value,联表,Mybatis,注解,property,Result
来源: https://blog.csdn.net/wang9820/article/details/122115154
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。