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. 命令行下打WAR包

    命令行下打WAR包: jar -cvf TestMem.war *

  2. 将数据库表导入到solr索引

    将数据库表导入到solr索引 编辑solrcofnig.xml添加处理器 <requestHandler name="/dataimport" class="org ...

  3. Git Tips

    撤销已经推送到远程仓库的最后一次提交,要小心这么操作,因为远程仓库还有别人在使用 $ git reset --hard HEAD^ $ git push -f origin master 从仓库中提出 ...

  4. Nao 类人机器人 相关资料

    Nao 类人机器人 相关资料: 1.兄妹 PEPPER :在山东烟台生产,http://www.robot-china.com/news/201510/30/26564.html 2.国内机器人领先公 ...

  5. MMO之禅(三)职业能力

    MMO之禅(三)职业能力 --技术九层阶梯 Zephyr 201304 有了精神,我们还需要实际的行动. 到底需要什么能力?自我分析,窃以为为有九层,无所谓高低,因为每一层都需要不断地砥砺,编程,本身 ...

  6. undercore & Backbone对AMD的支持(Require.js中如何使用undercore & Backbone)

    RequireJS填补了前端模块化开发的空缺,RequireJS遵循AMD(异步模块定义,Asynchronous Module Definition)规范,越来越多的框架支持AMD,像最近的jQue ...

  7. PHP 常见语法 集合

    1.die()与exit()的真正区别 die 为 exit 的别名, 执行过程 将释放内存,停止代码执行 echo "begin exec <br/>"; show( ...

  8. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  9. js类(继承)(一)

    //call() //调用一个对象的一个方法,以另一个对象替换当前对象. //call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) //参数 //thisObj / ...

  10. Query Object--查询对象模式(上)

    回顾 上两篇文章主要讲解了我对于数据层的Unit Of Work(工作单元模式)的理解,其中包括了CUD的操作,那么今天就来谈谈R吧,文章包括以下几点: 什么是Query Object 基于SQL的实 ...