ibatis的一对多查询

时间:2021-04-24 11:56:10

在Mybatis中,遇到需要一对多和多对多查询的对象封装,可以使用<collection>标签加载

而在Mybatis的前身ibatis中,不支持该标签,要进行类似的封装可以使用resultMap属性加载


在MyBatis中如下的配置

    <!-- Result Map -->
    <!-- goods resultmap -->
    <resultMap id="goodsResultMap" type="com.qunar.scoresystem.bean.Goods">
        <id property="goodsId" column="goods_id" />
        <result property="goodsName" column="goods_name" />
        <result property="goodsStorageNum" column="goods_storage_num" />
        <result property="goodsScore" column="goods_score" />
        <result property="goodsDescription" column="goods_description" />
        <result property="goodsStatus" column="goods_status" />
        <collection property="goodsImgList" resultMap="goodsImgResult" />
    </resultMap>

    <!-- goodsimage resultmap -->
    <resultMap id="goodsImgResult" type="com.qunar.scoresystem.bean.GoodsImg">
        <id property="imgId" column="img_id" />
        <result property="goodsId" column="goods_id" />
        <result property="imgDir" column="img_dir" />
        <result property="imgSize" column="img_size" />
        <result property="imgName" column="img_name" />
    </resultMap>


ibatis不支持<collection>标签,可以用下列的方法,效果一致

用groupBy属性代替<id>标签

直接在result中使用resultMap属性引用其他resultMap,ibatis会自动封装

    <!-- Result Map -->
    <!-- goods resultmap -->
    <resultMap id="goodsResultMap" type="com.qunar.scoresystem.bean.Goods" groupBy="goodsId">
        <result property="goodsId" column="goods_id" />
        <result property="goodsName" column="goods_name" />
        <result property="goodsStorageNum" column="goods_storage_num" />
        <result property="goodsScore" column="goods_score" />
        <result property="goodsDescription" column="goods_description" />
        <result property="goodsStatus" column="goods_status" />
        <result property="goodsImgList" resultMap="goodsImgResult" />
    </resultMap>

    <!-- goodsimage resultmap -->
    <resultMap id="goodsImgResult" type="com.qunar.scoresystem.bean.GoodsImg">
        <result property="imgId" column="img_id" />
        <result property="goodsId" column="goods_id" />
        <result property="imgDir" column="img_dir" />
        <result property="imgSize" column="img_size" />
        <result property="imgName" column="img_name" />
    </resultMap>



还有一种方法可以实现,但效率太低,不推荐使用

    <!-- Result Map -->
    <!-- goods resultmap -->
    <resultMap id="goodsResultMap" type="com.qunar.scoresystem.bean.Goods">
        <result property="goodsId" column="goods_id" />
        <result property="goodsName" column="goods_name" />
        <result property="goodsStorageNum" column="goods_storage_num" />
        <result property="goodsScore" column="goods_score" />
        <result property="goodsDescription" column="goods_description" />
        <result property="goodsStatus" column="goods_status" />
        <result property="goodsImgList" column="{goodsId = goods_id}" select="SelectGoodsImgList" />
    </resultMap>

    <!-- goodsimage select -->
    <select id="SelectGoodsImgList" resultClass="com.qunar.scoresystem.bean.GoodsImg">
        select * from XXX where id = #goodsId#
    </select>