mybatis异常:Mapped Statements collection does not contain value

在上手mybatis一对多时就遇到了个巨坑:

Mapped Statements collection does not contain value

在网上查了大量资料都无法解决,或者说是我不够理解那些解决方案。

 <select id="selectComment" resultMap="shareCommentResult">
        select c.*,
               l.id is not null is_like,
               #{param2} as     u_account
        from cat_share_comment c
                 left join cat_share_like l on c.id = l.host_id and l.part_code = 2 and l.account = #{param2}
        where c.host_id = #{param1}
    </select>
<resultMap id="shareCommentResult" type="xyz.ganbug.gdutdemo.model.cat.CatShareCommentVo">
        <id column="id" property="id"/>
        <result column="account" property="account"/>
        <result column="wx_name" property="wxName"/>
        <result column="host_id" property="hostId"/>
        <result column="avatar_url" property="avatarUrl"/>
        <result column="text" property="text"/>
        <result column="like_num" property="likeNum"/>
        <result column="comment_num" property="commentNum"/>
        <result column="is_like" property="isLike"/>
        <result column="create_date" property="createDate"/>
        <result column="create_time" property="createTime"/>
        <result column="timestamp" property="timestamp"/>
        <collection property="comments" ofType="xyz.ganbug.gdutdemo.model.cat.CatShareComment2Vo"
                    select="xyz.ganbug.gdutdemo.mapper.CatShareMapper.selectComment2sForComment"
                    column="{id=id,account=u_account}"/>
    </resultMap>


    <select id="selectComment2sForComment" resultMap="shareComment2">
        select c.*,
               l.id is not null is_like
        from cat_share_comment2 c
                 left join cat_share_like l on c.id = l.host_id and l.part_code = 2 and l.account = '3117002575'
        where c.host_id = #{id}
    </select>

分析:selectCommentselect c.*,要注意,我天真的以为它等同于没有前缀c.,如c.id 跟 id一样,事实是在一般的映射中确实如此,都是如果要映射到属性column中那就大错特错了。
所以应该这样修改:

<collection property="comments" ofType="xyz.ganbug.gdutdemo.model.cat.CatShareComment2Vo"
                    select="xyz.ganbug.gdutdemo.mapper.CatShareMapper.selectComment2sForComment"
                    column="{id=id,account=u_account}"/>

修改成(不要忽略前缀c.)

<collection property="comments" ofType="xyz.ganbug.gdutdemo.model.cat.CatShareComment2Vo"
        select="xyz.ganbug.gdutdemo.mapper.CatShareMapper.selectComment2sForComment"
        column="{id=c.id,account=u_account}"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值