星空网 > 软件开发 > Java

mybatis mapper association collection

1.Question Description:

  sometimes, POJO bean contains another bean or collection as property,

  it's suitable for select data from more than one table.

2. Solution:

  2.1 mybatis mapper file , for example:

<mapper namespace="cn.net.syl.dao.ProductsMapper" >
  <resultMap id="BaseResultMap" type="cn.net.syl.model.Products" >
    <id column="product_id" property="productId" jdbcType="BIGINT" />
    <result column="product_name" property="productName" jdbcType="VARCHAR" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="descript" property="descript" jdbcType="VARCHAR" />
    <result column="price" property="price" jdbcType="DOUBLE" />
    ......
  </resultMap>

  .....

<resultMap type="cn.net.syl.model.Products" id="BigResultMap" extends="BaseResultMap">
        <association property="prodExt" javaType="cn.net.syl.model.ProductsExt">
           <id column="product_ext_id" property="productExtId" jdbcType="BIGINT" />
          <result column="product_id" property="productId" jdbcType="BIGINT" />
         ....
        </association>
        <collection property="prodPropImgList" ofType="cn.net.syl.model.ProductsPreviewImages">
               <id column="prev_id" property="prevId" jdbcType="BIGINT" />
            <result column="product_id" property="productId" jdbcType="BIGINT" />
            ..........
        </collection>
  </resultMap>

  <select id="getProductInfo" resultMap="BigResultMap" parameterType="java.lang.Long">
      select
     a.*, b.detail_descript,
     c.image_url,
     c.product_id,
     c.property_id,
     c.value_id,
     d.value_name
    FROM
     syl_products a,
     syl_products_ext b,
     syl_products_preview_images c,
     syl_property_values d
    where a.product_id = b.product_id
      and a.product_id = c.product_id
      and c.value_id = d.value_id
      and a.product_id = #{productId}
  </select>
</mapper>

  2.2  POJO bean , for example:

public class Products {
    
    private String idStr;
    
    private Long productId;

    private String productName;

    ............
    
    //add field for sql
    private ProductsExt prodExt;
    
    
    //add field for sql
    List<ProductsPreviewImages> prodPropImgList;
    
    //add field for sql
    private List<ProductPropertyEntity> baseProdPropList;

    //getter and setter method

    ....

}




原标题:mybatis mapper association collection

关键词:mybatis

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

乐言科技:https://www.goluckyvip.com/tag/37230.html
乐一番:https://www.goluckyvip.com/tag/37231.html
乐盈商学院:https://www.goluckyvip.com/tag/37232.html
乐鱼跨境:https://www.goluckyvip.com/tag/37233.html
了解跨境电商:https://www.goluckyvip.com/tag/37235.html
雷陈琴:https://www.goluckyvip.com/tag/37236.html
石象湖景区门票-石象湖景区门票优惠政策:https://www.vstour.cn/a/411243.html
北京到嵩山自驾游沿途景点 北京距离嵩山有多远:https://www.vstour.cn/a/411244.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流