关联查询的resultMap写法示例
对于自定义对象一般使用association,对于集合一般使用collection。
对于一般的自定义对象
1、使用子查询:
<resultMap id="BaseResultMapWithItemInfo" type="com.xxx.biz.cases.model.ShCase" extends="BaseResultMap">
<association property="applyBillDetailList" column="SERVICE_ID" select="com.xxx.biz.dao.ApplybillDetailMapper.getBriefInfoByServiceId" />
<association property="commentSpList" column="SERVICE_ID" select="com.xxx.biz.dao.ServicePartcommentMapper.getCommentSpBriefInfoByServiceId" />
<association property="finalUseSpList" column="SERVICE_ID" select="com.xxx.biz.dao.ServicePartcommentMapper.getFinalUseSpBriefInfoByServiceId" />
</resultMap>
2、不使用子查询:
<resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
<id column="carid" property="id"/>
<result column="cartype" property="type"/>
<association property="engine" resultMap="engineResult"/>
<association property="brakes" resultMap="brakesResult"/>
</resultMap>
<resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">
<result column="enginetype" property="type"/>
<result column="enginecylinders" property="cylinders"/>
</resultMap>
<resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">
<result column="brakesType" property="type"/>
</resultMap>
或者类似如下写法:
<resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
<id column="carid" property="id"/>
<result column="cartype" property="type"/>
<association property="engine" javaType="org.apache.ibatis.submitted.associationtest.Engine">
<result column="enginetype" property="type"/>
<result column="enginecylinders" property="cylinders"/>
</association>
</resultMap>
这样会自动在sql查询结果里找到相应的字段来组成相应的对象。
对于list
1、使用子查询:
<resultMap type="com.xxx.base.sys.domain.User" id="userWithRolesMap" extends="BaseResultMap">
<collection property="roles" column="id" javaType="list" select="com.xxx.base.sys.dao.RoleMapper.selectUserRoleByUserId"> </collection>
</resultMap>
2、当然,也可以不使用子查询
<collection property="tags" javaType="list" ofType="Tag" >
<id property="id" column="tag_id"/>
</collection>
这会让mybatis自动进行一个类似group by的操作,将所有其他字段重复的数据合并,然后将tag_id作为Tag的id属性拼成一个List<Tag>,这样做效率高一些,但是使用场景也是有限的。
3、如果list中的对象是String
<collection property="tags" javaType="list" ofType="String" >
<result column="tag_id"/>
</collection>
注意,以上2和3中collection标签和1一样,都应该包裹在resultMap标签中,这里为了省事省略了
关联查询的resultMap写法示例的更多相关文章
- 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单个对象
resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...
- EF4中多表关联查询Include的写法
大家好,好久没有写作了,最近遇到了个问题,最终是靠自己的尝试写出来的,希望可以帮到有需要的人. 在我们查询时通常会遇到多级表关联的情况,很多时候有人会想写一个from LINQ语句来解决,那么冗长的代 ...
- 7.Mybatis关联表查询(这里主要讲的是一对一和一对多的关联查询)
在Mybatis中的管理表查询这里主要介绍的是一对一和一对多的关联查询的resultMap的管理配置查询,当然你也可以用包装类来实现.不过这里不说,做关联查询的步骤可以简单的总结为以下的几步: 1.分 ...
- Mybatis学习系列(五)关联查询
前面几节的示例基本都是一些单表查询,实际项目中,经常用到关联表的查询,比如一对一,一对多等情况.在Java实体对象中,一对一和一对多可是使用包装对象解决,属性使用List或者Set来实现,在mybat ...
- MyBatis入门(二)—— 输入映射和输出映射、动态sql、关联查询
一.输入映射和输出映射 1. parameterType(输入类型) 1.1 传递简单类型 <select id="getUserById" parameterType=&q ...
- MyBatis多对一,一对多,多对多,一对多关联查询
一.Person实体类 1 public class Person { 2 private Integer personId; 3 private String name; 4 private Int ...
- MyBatis-Plus不写任何resultMap和SQL执行一对一、一对多、多对多关联查询
对于一对一,一对多的关联查询,Mybatis-Plus官方示例(mybatis-plus-sample-resultmap)在处理时,需要编写查询方法及配置resultMap,并且写SQL. 为了简化 ...
- 关联查询resultMap使用规则总结——(十一)
resultType: 作用: 将查询结果按照sql列名pojo属性名一致性映射到pojo中. 场合: 常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用re ...
随机推荐
- Resin4 自定义端口
1. Resin4配置文件发生了较大变化,分为: app-default.xml web应用配置 cluster-default.xml 集群配置 health.xml -- 非pro版不支持 r ...
- java获取当前类名和方法名
Description Below I present you two different ways to get the current Class: Using Thread Using getC ...
- UVA 1025 A Spy in the Metro 【DAG上DP/逆推/三维标记数组+二维状态数组】
Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After s ...
- 51nod 1182 完美字符串【字符串排序+哈希】
1182 完美字符串 题目来源: Facebook Hacker Cup选拔 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 约翰认为字符串的完美度等 ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- logstash filter plugin
1. 基本语法%{NUMBER:duration} %{IP:client} 2. 支持的数据类型默认会把所有的匹配都当作字符串,比如0.043, 想要转成浮点数,可以%{NUMBER:num:flo ...
- iOS学习4_UITableView的使用
UITableView相当于Android里面的ListView.但功能却比ListView强大太多. 使用UITableView须要指定数据源和代理. 1.显示全部的行 遵守UITableViewD ...
- util.string.js
ylbtech-JavaScript-util: util.string.js 字符串处理工具 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1, ...
- js获取上传图片的尺寸大小
当上传图片时,有时候需要控制下上传图片的尺寸大小,需要给个提示 //获取图片的尺寸,控制尺寸大小 var reader = new FileReader(), img = new Image(); / ...
- GCD部分使用方法
1,用gcd延迟运行任务 假设我们须要某个方法在一段时间后运行.那么我们经常会调用这个方案 - (void)viewDidLoad{ [super viewDidLoad]; [self perfor ...