对于自定义对象一般使用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. zabbix 硬盘健康监控

    #!/bin/sh function sh { sd=`ls /dev/ | grep '^sd' |grep -v '[0-9]$'` echo '' > /usr/local/zabbix/ ...

  2. BigDecimal 使用 静态方法总结

    转自:http://myrecord.top/?post=18 -------------千位加, public static String getQs(String old){ boolean re ...

  3. PHP获取不带后缀的文件名方法

    $filename = "test.txt"; $houzhui = substr(strrchr($filename, '.'), 1); $result = basename( ...

  4. 转化一个数字数组为function数组(每个function都弹出相应的数字)

    从汤姆大叔的博客里看到了6个基础题目:本篇是第2题 - 转化一个数字数组为function数组(每个function都弹出相应的数字) 此题关键点: 1.如何将一个匿名函数存入数组? 2.如何锁住需要 ...

  5. Netty源码学习(二)NioEventLoopGroup

    0. NioEventLoopGroup简介 NioEventLoopGroup可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个Channel只对应于一个线 ...

  6. maven的知识图谱

    maven 1.maven的好处 java项目管理工具 依赖管理 对jar包统一管理 项目名称 公司/组织 版本信息 本地仓库 由于索引的存在,找jar包很快 项目构建 依赖管理 传统项目 很大 包含 ...

  7. poj1743(后缀数组)

    poj1743 题意 给出一个数字序列(串),现在要去寻找一个满足下列条件的子串: 长度不小于 5 存在重复的子串(如果把一个子串的所有数字都加上或减去一个值,与另一子串的数字对应相同,我们称它们重复 ...

  8. adb 查看android手机中应用的包名和安装位置

    1. 查看是否连接手机 adb devices 2. 进入指定的device的shell adb shell 或者 adb -s ********* shell 3. adb 查看所有安装的包 pm ...

  9. nginx.conf及server配置

    #服务运行用户 user sysadmin www; #工作进程数 worker_processes 4; #错误日志位置 error_log /data/sysadmin/service_logs/ ...

  10. A system tap script to detect UDP beacons

    https://gist.github.com/jbradley89/178bbf3944786c494bd78f3df16a5472