ibatis 实现多表关联,一对多,多对多关系

多张表关联查询,一个产品对应多张图片结果集,同时对应多个联系人实现:

数据结构如下:  

整体结构:

  @Data
  public class FeaturedProducts {
    private String characteristicProductId;//特色产品id
    private String productsTitle;//产品标题
    private String productsDescription;//产品描述
    private List<String> productsImage;//产品图片结果集
    private List<Contacts> contacts;//特色产品联系人结果集

  }

 

  list<Bean>集合结构:

  @Data
  public class Contacts {
    private String contactId;//联系人id
    private String name;//联系人姓名
    private String phone;//联系人电话
    private String qrcode;//联系人二维码

  }

最后得到结果集:

[
  FeaturedProducts(

    characteristicProductId=7b1e2676-067b-11ea-88ec-00ff07e25c4a,

    productsTitle=dsd, productsDescription=sdd, 

    productsImage=[

      www.222.png,

      https://mmwtest2.oss-cn-hangzhou.aliyuncs.com/image/avatarPic/upload/201712/cardPic1513777343927.jpg,

      https://mmwtest2.oss-cn-hangzhou.aliyuncs.com/image/avatarPic/upload/201712/cardPic1513777343927.jpg,

      www.444.png, https://mmwtest2.oss-cn-hangzhou.aliyuncs.com/image/avatarPic/upload/201712/cardPic1513777343927.jpg,

      https://mmwtest2.oss-cn-hangzhou.aliyuncs.com/image/avatarPic/upload/201712/cardPic1513777343927.jpg

    ],
    contacts=[

      Contacts(contactId=2a7f8080-067d-11ea-88ec-00ff07e25c4a, name=mr.wu, phone=13200000000, qrcode=www.qrcode2.com),

      。。。可有多个

    ]

  ),
  FeaturedProducts(

    characteristicProductId=d775aaae-067b-11ea-88ec-00ff07e25c4a,

    productsTitle=fdffd,    

    productsDescription=sdsdad,

    productsImage=[

      www.555.png,

      www.333.png

    ], 

    contacts=[

      Contacts(contactId=f8976924-067c-11ea-88ec-bbbbbbbbbb, name=mr.zh, phone=13700000000, qrcode=www.qrcode.com),

      。。。可有多个

    ]

  )

]

ibatis  xml文件:

 1、com.mmw100.enterpriseDetailandProfile.bean.FeaturedProducts为整体结构bean;featuredProductsResultMap对应下面select查询的resultMap;characteristicProductId为分组id

<resultMap class="com.mmw100.enterpriseDetailandProfile.bean.FeaturedProducts" id="featuredProductsResultMap" groupBy="characteristicProductId">
  <result column="characteristicProductId" property="characteristicProductId"/>
  <result column="productsTitle" property="productsTitle"/>
  <result column="productsDescription" property="productsDescription"/>
  <result property="productsImage" resultMap="enterpriseDetailSql.imagesResultMa"/>//enterpriseDetailSql为xml命名空间名;imagesResultMa 下面resultMap的id
  <result property="contacts" resultMap="enterpriseDetailSql.contactMap"/>//enterpriseDetailSql为xml命名空间名;imagesResultMa 下面resultMap的id
</resultMap>

2、返回List<String>结构   class定义为String
<resultMap id="imagesResultMap" class="java.lang.String">
  <result property="url" column="image_url"/>//需要的字段名,如果有多个字段,会默认选第一个
</resultMap>

3、返回List<Bean>结构 class为bean目录  groupBy按照该字段分组
<resultMap id="contactMap" class="com.mmw100.enterpriseDetailandProfile.bean.Contacts" groupBy="contactId">
  <result property="contactId" column="contactId"/>
  <result property="name" column="name"/>
  <result property="phone" column="phone"/>
  <result property="qrcode" column="qrcode"/>
</resultMap>

4、select 关联查询  一个商品对应多张图片,多个人员信息
<select id="getFeaturedProducts" parameterClass="com.mmw100.enterpriseDetailandProfile.bean.EnterpriseDetailandProfileInput" resultMap="featuredProductsResultMap">
  SELECT
    cop.contact_person_id as contactId,cp.characteristic_product_id as characteristicProductId,cp.product_name as productsTitle,pi.product_image_id as imgId,pi.image_url,cp.product_description as productsDescription,cop.`name` as name,cop.telephone     AS phone,cop.wechat_qr_code as qrcode
  FROM
  characteristic_product cp
  LEFT JOIN product_image pi ON cp.characteristic_product_id=pi.product_id AND pi.del_flag='0'
  LEFT JOIN contact_person cop ON cop.characteristic_product_id=cp.characteristic_product_id AND cop.del_flag='0'
  WHERE
  cp.ex_enterprise_id=#enterpriseId#
  AND cp.del_flag='0'
  ORDER BY cp.create_time
</select>

posted @ 2019-11-15 17:38  MustangLQT  阅读(491)  评论(0编辑  收藏  举报