• 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

        ....

    }

  • 相关阅读:
    问题解决:System.TypeLoadException: 未能从程序集“XXX”中加载类型“XXX
    域名格式验证
    .NET 中String 和StringBuilder 以及他们的区别
    mysql 重设管理员密码 或忘记管理员密码 (必杀)
    CMD 命令
    C# 给多线程传参的三种方式
    django 表单数据的验证实现原理
    django上传文件
    django signal
    Django如何处理语言偏好(根据此可以设置中文)
  • 原文地址:https://www.cnblogs.com/rocky-fang/p/5319951.html
Copyright © 2020-2023  润新知