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
    评论
在Java MyBatis中,可以使用collection来实现一对多的关系。具体步骤如下: 1. 首先,在数据库中创建两个表,一个是主表(例如:Order),另一个是从表(例如:Item)。主表和从表之间通过外键进行关联。 2. 在Java代码中,创建对应的实体类(例如:Order和Item),并为它们添加相应的属性和getter/setter方法。 3. 创建Mapper接口(例如:OrderMapper),定义查询方法,用于获取主表数据及其关联的从表数据。 4. 在Mapper XML文件中,编写SQL语句,使用MyBatis提供的collection标签来映射一对多的关系。示例代码如下: ```xml <!-- 查询订单及其对应的商品列表 --> <select id="getOrderWithItems" resultMap="orderResultMap"> SELECT o.id, o.order_no, i.id as item_id, i.name as item_name, i.price as item_price FROM orders o LEFT JOIN items i ON o.id = i.order_id WHERE o.id = #{orderId} </select> <resultMap id="orderResultMap" type="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <!-- 使用collection标签映射一对多关系 --> <collection property="items" ofType="Item"> <id property="id" column="item_id"/> <result property="name" column="item_name"/> <result property="price" column="item_price"/> </collection> </resultMap> ``` 5. 在Mapper接口中定义对应的方法,用于调用SQL语句并返回结果。 6. 在Java代码中,通过MyBatisSqlSessionFactory和Mapper接口来执行查询操作,并获取一对多关系的数据。 这样,你就可以使用Java MyBatis实现一对多关系的查询了。希望能够帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值