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的更多相关文章

  1. mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map

    用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...

  2. 解决 Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat

    在配置 mybatis mapper.xml文件时, 一不小心就会报如下类似的异常: Caused by: org.springframework.beans.factory.BeanCreation ...

  3. Mybatis中的collection、association来处理结果映射

    前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:)   标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...

  4. mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".

    启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...

  5. association ,collection

    mybatis 出现这个错误Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 2 ...

  6. MyBatis——Mapper XML 文件

    Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...

  7. MyBatis Mapper XML 详解

    MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...

  8. mybatis Result Maps collection already contains value for com.ebways.dictionary.dao.impl.PtInfoDaoImpl.beanMap

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  9. mybatis Mapper XML 文件

    MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...

随机推荐

  1. Spark源码系列(四)图解作业生命周期

    这一章我们探索了Spark作业的运行过程,但是没把整个过程描绘出来,好,跟着我走吧,let you know! 我们先回顾一下这个图,Driver Program是我们写的那个程序,它的核心是Spar ...

  2. [转]Sql Server 给表与字段添加描述

    /* 在SQL语句中通过系统存储过sp_addextendedproperty可为表字段添加上动态的说明(备注)下面是SQL SERVER帮助文档中对sp_addextendedproperty存储过 ...

  3. Oracle数据库入门——物化视图语法

    一.Oracle物化视图语法 create materialized view [view_name]refresh [fast|complete|force][on [commit|demand] ...

  4. pdf嵌入字体

    论文提交时,要求所有的字体都是嵌入的,为这个问题折腾了很久,发现了一个很好的答案,记一下: http://stackoverflow.com/questions/4231656/how-do-i-em ...

  5. BATCH(BAT批处理命令语法)

    bat语法备忘扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的文件就是批处理文件[@more@] bat语法备忘扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的 ...

  6. SkipList 跳表

    1.定义描述      跳跃列表(也称跳表)是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间).      基本上,跳跃列表是对有序的链表增加 ...

  7. 全中国的省市县镇乡村数据获取以及展示java源代码

    第一步.准备工作(数据源+工具): 数据源(截止目前最全面权威的官方数据):http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/ 爬取数据的工具 ...

  8. Java知多少(完结篇)

    Java知多少(1)语言概述 Java知多少(2)虚拟机(JVM)以及跨平台原理 Java知多少(3) 就业方向 Java知多少(4)J2SE.J2EE.J2ME的区别 Java知多少(5) Java ...

  9. call方法和new对象的关系

    call只能改变this的指向,而使用new对象不仅会自动调用call方法改变这个对象的this指向,而且还会继承构造函数的原型. var fn = function(a){ this.a = a; ...

  10. CentOS linux系统搭建LAMP环境

    准备工作: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state –state NEW -m tcp -p tcp –dpo ...