mybatis 一对多 collection 获取不到输出值

  1. 这是我的用到的两张表 tb_book 和 tb_book_pic数据表

2.所对应的实体类 TbBook和TbBookPic

 

 3.一开始我写的sql查询如下

 <resultMap id="selectUpdateBookReMap" type="TbBook">
        <id property="bookId" column="book_id"/>
        <result property="bookName" column="book_name"/>
        <!--<result property="typeId" column="type_id"/>-->
        <result property="bookPrice" column="book_price"/>
        <result property="bookPriceOld" column="book_price_old"/>
        <result property="bookAuthor" column="book_author"/>
        <result property="bookPress" column="book_press"/>
        <result property="bookLogoBig" column="book_logo_big"/>
        <result property="bookLogoSmall" column="book_logo_small"/>
        <result property="bookDescription" column="book_description"/>
        <result property="isNew" column="is_new"/>
        <result property="isHot" column="is_hot"/>
        <result property="status" column="status"/>
        <result property="storeCount" column="store_count"/>
        <!-- 对应的类型 -->
        <association property="tbType" column="type_id" select="selecUpdatetType">
            <id property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <result property="parentId" column="parent_id"/>
        </association>
        <!-- 对应的子图 -->
        <collection property="picName" select="selectUpdateBookPic" column="id">
            <id property="picId" column="pic_id"/>
            <result property="picName" column="pic_name"/>
            <result property="bookId" column="book_id"/>
        </collection>
    </resultMap>

    <!-- 查看需要修改书本 的书本信息 -->
    <select id="selectUpdateBook" resultMap="selectUpdateBookReMap">
        SELECT book_id , book_name,type_id,book_price,book_price_old,book_author,book_press ,
               book_logo_big,book_logo_small,book_description,is_new,is_hot,status,store_count
        FROM `tb_book`
        where book_id = #{book_id}
    </select>

    <!-- 查看需要修改书本 的书本信息 ==子图 -->
    <select id="selectUpdateBookPic" resultType="TbBookPic">
        select pic_name FROM tb_book_pic where book_id = #{book_id}
    </select>

    <!-- 查询本书类型 -->
    <select id="selecUpdatetType" resultType="TbType">
        SELECT * from`tb_type` where type_id = #{type_id}
    </select>

 4.结果测试发现 每本书对应的子图没有被查询出来,即 tbBook.getPicName() 方法结果为null, 说明selectUpdateBookPic 这个查询没被执行。看了一些帖子解决不了疑惑,后面自己感觉就是传值的时候出现问题了,可能是引文sql语句不知道book_id字段是哪个表的,于是起了个别名,发现就可以了。    需更改的地方如图所示

 

 5.执行tbBook.getPicName() 方法,顺利取得每本书的子图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值