Mybatis 一對多 多對一 xml處理

例子店鋪表和店鋪圖片表是一對多的關係

店鋪對應實體:

public class StoreVo implements Serializable {
    private static final long serialVersionUID = 1L;

    //
    private Long id;
    //會員id
    private Long userId;
    //主體類型 1.個人 2.個體工商戶
    private Integer registrationType;
    //店鋪名稱
    private String storeName;
    //店鋪地址
    private String storeAddress;
    //工商註冊號
    private String registrationNo;
    //營業執照
    private String registrationImgUrl;
    //主營產品
    private String storeProduct;
    //負責人
    private String principalName;
    //聯繫電話
    private String telNumber;
    //認證狀態:1.已認證 0未認證 2已駁回
    private Integer authStatus;
    //創建時間
    private Date createDate;

    //店鋪圖片列表
    private List<StoreImgVo> storeImgVos;
}

 

 店鋪圖片實體:

public class StoreImgVo implements Serializable {
    private static final long serialVersionUID = 1L;

    //
    private Long id;
    //店鋪Id
    private Long storeId;
    //店鋪圖片
    private String storeImgUrl;
}

 

 店鋪xml

<resultMap type="com.snserver.entity.StoreVo" id="storeMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="registrationType" column="registration_type"/>
        <result property="storeName" column="store_name"/>
        <result property="storeAddress" column="store_address"/>
        <result property="registrationNo" column="registration_no"/>
        <result property="registrationImgUrl" column="registration_img_url"/>
        <result property="storeProduct" column="store_product"/>
        <result property="principalName" column="principal_name"/>
        <result property="telNumber" column="tel_number"/>
        <result property="authStatus" column="auth_status"/>
        <result property="createDate" column="create_date"/>
    </resultMap>
    <resultMap type="com.snserver.entity.StoreVo" id="stortImgMap"
               extends="storeMap">
        <collection property="storeImgVos" ofType="com.snserver.entity.StoreImgVo">
            <id property="id" column="sid" />
            <result property="storeId" column="storeId" />
            <result  property="storeImgUrl" column="storeImgUrl"/>
        </collection>
    </resultMap>
     <select id="queryUserId" resultMap="stortImgMap">
		select
			a.`id`,
			a.`user_id`,
			a.`registration_type`,
			a.`store_name`,
			a.`store_address`,
			a.`registration_no`,
			a.`registration_img_url`,
			a.`store_product`,
			a.`principal_name`,
			a.`tel_number`,
			a.`auth_status`,
			a.`create_date`,
			b.`id` as sid,
			b.`store_id` as storeId,
			b.`store_img_url` as storeImgUrl
		from tbl_store as a left join tbl_store_img as b on a.id = b.store_id
		where user_id = #{userId}
	</select>
</mapper>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章