程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> mybatis mapper association collection,mybatismapper

mybatis mapper association collection,mybatismapper

編輯:JAVA綜合教程

mybatis mapper association collection,mybatismapper


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

    ....

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved