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% ...
随机推荐
- Gamma校正与线性工作流
1 Gamma校正是什么?8位亮度值x(0-1)经过x^0.45的一个提亮过程. 2 为什么需要Gamma校正 人的眼睛是以非线性方式感知亮度,在自然界中,人感觉到的一半亮度其实只有全部能量的0.2, ...
- CentOS6下Haproxy的安装配置
Haproxy 是一个开源的负载均衡和反向代理软件,其提供了高可用的网络服务.其一般是应用于web服务,但同时也能为SMTP和终端服务等提供可靠的支持. 1.下载安装haproxy wget ftp: ...
- mysql 登录后 修改密码
- 提高c++性能的编程技术笔记
需要时再创建对象,比如在类中用if new 而不是在构造函数里创建类的成员. 用char 指针而不是string可以节省构造和析构string的开销. 虚函数无法内联的性能损失.
- iOS 模拟器键盘弹出以及中文输入
1.虚拟键盘的弹出与收起切换: 快捷键:command+shift+K 2.中文输入: Xcode 菜单项 --> Product --> Scheme --> Edit Schem ...
- HTML CSS——margin和padding的学习
你在学习margin和padding的时候是不是懵了,——什么他娘的内边距,什么他娘的外边距.呵呵呵,刚开始我也有点不理解,后来通过查资料学习总算弄明白了,现在我来谈一下自己对margin和paddi ...
- 转 -- Linux系列:Ubuntu虚拟机设置固定IP上网(配置IP、网关、DNS、防止resolv.conf被重写)
原文转自:http://www.cnblogs.com/lanxuezaipiao/p/3613497.html#undefined 虚拟机里设置上网方式为NAT最方便,因为无需手动设置即可上网,但是 ...
- right-click an action, missing "Go to slot"
According to the tutorial,to connect the actions to slots, right-click an action and select Go to sl ...
- php中关于js保存文件至本地的问题
最近在搞一个livezilla的在线客服聊天的东东,客户界面要求添加一个下载聊天记录的功能.于是我就是翻看了下网上的各种关于”js保存文件至本地“的资料,发现只能在IE下通过execCommand实现 ...
- 实战:ASP.NET MVC中把Views下面的视图放到Views文件夹外
园子里写的文章的都是把控制器从传统的项目中的Controllers拿出来单独放,但很少几乎没有把视图从Views拿出去这样的文章,今天来写一个. 其实很简单!一步步解决问题就行了,下面记录如下,供需要 ...