对于自定义对象一般使用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写法示例的更多相关文章

  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单个对象

    resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...

  3. EF4中多表关联查询Include的写法

    大家好,好久没有写作了,最近遇到了个问题,最终是靠自己的尝试写出来的,希望可以帮到有需要的人. 在我们查询时通常会遇到多级表关联的情况,很多时候有人会想写一个from LINQ语句来解决,那么冗长的代 ...

  4. 7.Mybatis关联表查询(这里主要讲的是一对一和一对多的关联查询)

    在Mybatis中的管理表查询这里主要介绍的是一对一和一对多的关联查询的resultMap的管理配置查询,当然你也可以用包装类来实现.不过这里不说,做关联查询的步骤可以简单的总结为以下的几步: 1.分 ...

  5. Mybatis学习系列(五)关联查询

    前面几节的示例基本都是一些单表查询,实际项目中,经常用到关联表的查询,比如一对一,一对多等情况.在Java实体对象中,一对一和一对多可是使用包装对象解决,属性使用List或者Set来实现,在mybat ...

  6. MyBatis入门(二)—— 输入映射和输出映射、动态sql、关联查询

    一.输入映射和输出映射 1. parameterType(输入类型) 1.1 传递简单类型 <select id="getUserById" parameterType=&q ...

  7. MyBatis多对一,一对多,多对多,一对多关联查询

    一.Person实体类 1 public class Person { 2 private Integer personId; 3 private String name; 4 private Int ...

  8. MyBatis-Plus不写任何resultMap和SQL执行一对一、一对多、多对多关联查询

    对于一对一,一对多的关联查询,Mybatis-Plus官方示例(mybatis-plus-sample-resultmap)在处理时,需要编写查询方法及配置resultMap,并且写SQL. 为了简化 ...

  9. 关联查询resultMap使用规则总结——(十一)

    resultType: 作用: 将查询结果按照sql列名pojo属性名一致性映射到pojo中. 场合: 常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用re ...

随机推荐

  1. OpenGL入门学习(三)

    http://developer.178.com/201103/94954704639.html 在第二课中,我们学习了如何绘制几何图形,但大家如果多写几个程序,就会发现其实还是有些郁闷之处.例如:点 ...

  2. c/c++中const用法总结

    1.修饰常量时: const int temp1;  //temp1为常量,不可变 int const temp2;  //temp2为常量,不可变 2.修饰指针时: 主要看const在*的前后,在前 ...

  3. 钩子注入呼出与隐藏DLL窗口

    / MFC_DLL.cpp : 定义 DLL 的初始化例程. // #include "stdafx.h" #include "MFC_DLL.h" #incl ...

  4. (7)java基础知识-原码、反码、补码、运算符

    一.原码.反码.补码 原码 一个数转化成二进制. 用最高位来表示正负,最高位为0表示正数,最高位为1表示负数. 例如: short i=5: 因为在java里short占2个字节转化成二进制就是 00 ...

  5. codeforces-540C

    题目连接:http://codeforces.com/problemset/problem/540/C C. Ice Cave time limit per test 2 seconds memory ...

  6. FZU -2212 Super Mobile Charger(水题)

     Problem 2212 Super Mobile Charger Accept: 1033    Submit: 1944Time Limit: 1000 mSec    Memory Limit ...

  7. 第八章 android-布局

    常用的布局实现方式:线性布局,框架布局,表格布局,相对布局,绝对布局 1,线性布局 (1)线性布局是一种很重要的布局,也是经常用到的一种布局 (2)在线性布局中,所有的元素都按照水平竖直的顺序在界面上 ...

  8. 单条sql性能分析与优化

    性能分析 1. explain 查看sql执行计划,得出索引使用情况等信息 2. show profiling 查看sql所有执行步骤以及用时,使用步骤如下 1)开启性能剖析 mysql> se ...

  9. @selector和SEL

    遇到selector发现不是很明白,网上搜到的零零星星的介绍也不成体系,索性自己翻译一下,加深一下印象.原文来自官方API文档下的Selectors. Selectors 在OC中,selector有 ...

  10. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...