具体代码如下:
sql:
SELECT aid,acontent,adate,like_count,ui.uid AS uid,wid,nickname,portrait,
(SELECT COUNT(1) FROM bug_answer WHERE FIND_IN_SET(aid,get_answerSon_list(11)) AND wid != 0) AS `count`
FROM bug_answer ba JOIN user_info ui ON ba.uid = ui.uid WHERE bid = 11 AND wid = 0
==此时是能查询出多条的==
mybatis
<!-- 查询指定Bug文章下的回答集合 -->
<select id="findAllBugAnswerByBid" resultMap="findAllBugAnswerMap">
SELECT aid as aid,acontent,adate,like_count,ui.uid AS uid,wid,nickname,portrait,
(SELECT COUNT(1) FROM bug_answer WHERE FIND_IN_SET(aid,get_answerSon_list(ba.aid)) AND wid != 0) AS `count`
FROM bug_answer ba JOIN user_info ui ON ba.uid = ui.uid WHERE bid = #{bid} AND wid = #{wid}
</select>
mybatis 映射:
<!-- 回答 与 用户 联通映射 -->
<resultMap id="findAllBugAnswerMap" type="com.talentscout.vo.BugAnswerAndUserInfo">
<result property="count" column="count"/>
<association property="bugAnswer" javaType="com.talentscout.pojo.bug.BugAnswer">
<result property="aid" column="aid"/>
<result property="aContent" column="aContent"/>
<result property="aDate" column="aDate"/>
<result property="like_count" column="like_count"/>
<result property="uid" column="uid"/>
<result property="wid" column="wid"/>
</association>
<association property="userInfo" javaType="com.talentscout.pojo.user.UserInfo">
<result property="nickName" column="nickName"/>
<result property="portrait" column="portrait"/>
</association>
</resultMap>
实体
@Data
@ToString
public class BugAnswerAndUserInfo {
private BugAnswer bugAnswer; // 回答实体
private int count; // 子回复数量
private UserInfo userInfo; // 用户信息实体
private UserInfo userInfoSon; // 子用户信息实体
}
测试
@Override
public List<BugAnswerAndUserInfo> findAllBugAnswerByBid(Integer bid, Integer wid) {
for (BugAnswerAndUserInfo bugAnswerAndUserInfo : bugMainMapper.findAllBugAnswerByBid(bid, wid)) {
System.out.println(bugAnswerAndUserInfo.toString());
}
return bugMainMapper.findAllBugAnswerByBid(bid,wid);
}
结果
BugAnswerAndUserInfo(bugAnswer=BugAnswer(aid=32, aContent=WebSocket yyds!, aDate=Tue Oct 26 06:15:13 CST 2021, like_count=0, bid=0, uid=NO_2, wid=0), count=0, userInfo=UserInfo(uid=null, nickName=勇敢牛牛, age=0, sex= , register_date=null, brief=null, direaction=null, collective=null, portrait=t2.jpg), userInfoSon=null)
最离谱的是我特喵没动,之前是能查询出来的,莫名其妙就只查询出一条了