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 mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map
用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...
- 解决 Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat
在配置 mybatis mapper.xml文件时, 一不小心就会报如下类似的异常: Caused by: org.springframework.beans.factory.BeanCreation ...
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- 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 ...
- association ,collection
mybatis 出现这个错误Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2 ...
- MyBatis——Mapper XML 文件
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
- MyBatis Mapper XML 详解
MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...
- 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 ...
- mybatis Mapper XML 文件
MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...
随机推荐
- 【NLP】word2vec
http://blog.csdn.net/mytestmy/article/details/26969149?utm_source=tuicool&utm_medium=referral
- Android WebView Demo
activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- Rhino -- 基于java的javascript实现
http://www.cnblogs.com/cczw/archive/2012/07/16/2593957.html
- 【笔记】《DirectX 9.0 3D游戏开发编程基础》:Direct3D初始化
Direct3D初始化大概分为4个步骤: 1.获取接口IDirect3D9的指针.(Direct3DCreate9函数调用). 该接口用户获取系统中物理硬件设备的信息并创建接口IDirect3DDev ...
- Invalid Image Path - No image found at the path referenced under key "CFBundleIconFile": Icon.png
I got the same error when uploading my app. Moving all icon files to the Asset Catalog works if your ...
- Clappr——开源的Web视频播放器
巴西著名的门户网站Globo.com(视频播放器),使用的是基于OSMF的Flash组件.在最近几年的发展过程中,Globo为视频平台陆续添加了不少额外功能,例如: 字幕,广告,画中画播放等.然而,由 ...
- java之多态(Polymorphic)、动态绑定(Dynamic Binding)、迟绑定(Late Binding)
今天,我们来说说java面向对象最核心的东西,多态.通过多态可以使我们的程序可复用性达到极致,这就是我们为什么要学多态的原因. “多态”(Polymorphic)也叫“动态绑定”(Dynamic Bi ...
- Ubuntu下的杀毒
Ubuntu 11.04 杀毒软件ESET NOD32 Linux防病毒,并不代表没有病毒,只是数量过少.因技术精英发现漏洞后即时打补丁,病毒很少. 有兴趣的同学可以尝试下Ubuntu 11. ...
- C++ string 用法详解
/////////////////////////////////////////////////////////////////////////////////// 任何人对本文进行引用都要标明作者 ...
- Eclipse项目名出现红叉
一.背景与原因 项目第一次加进来的时候,我用的是D:\Java\jdk1.7.0_17,后来由于配置将tomcat切换到jboss,说是JBOSS某个版本只支持jdk6,我就将“环境变量JAVA_HO ...