resultMap之collection聚集

简介: select c1.carid,c1.cartype,c2.enginetype,c2.enginecylinders from cars c1,cars c2 where c1.carid=c2.carid       聚集元素用来处理“一对多”的关系。

    <select id="getCarsWithCollection" resultMap="superCarResult">
        select c1.carid,c1.cartype,c2.enginetype,c2.enginecylinders from cars c1,cars c2
        where c1.carid=c2.carid
    </select>
    <resultMap type="tk.mybatis.springboot.model.Brakes" id="brakesResult">
        <result column="brakesType" property="type"/>
    </resultMap>
    <resultMap type="tk.mybatis.springboot.model.SuperCar" id="superCarResult">
        <id column="carid" property="id"/>
        <result column="cartype" property="type"/>
        <association property="brakes" resultMap="brakesResult"/>
        <collection property="engines" javaType="ArrayList" ofType="tk.mybatis.springboot.model.Engine">
            <result column="enginetype" property="type"/>
            <result column="enginecylinders" property="cylinders"/>
        </collection>
    </resultMap>

 

 

 

聚集元素用来处理“一对多”的关系。需要指定映射的Java实体类的属性,属性的javaType(一般为ArrayList);列表中对象的类型ofType(Java实体类);对应的数据库表的列名称;
不同情况需要告诉MyBatis 如何加载一个聚集。MyBatis 可以用两种方式加载:

1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活但会将执行多次嵌套的SQL语句。
2. resultMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

两种加载方式格式如下:

1.集合的嵌套查询(select)

<collection property="Java属性名" ofType="另一Java类名" javaType="ArrayList" column="关联主键ID(用于嵌套查询SQL语句传入参数,多个用逗号分开)" select="另一个select映射SQL的ID"/>

<select parameterType="int" resultType="另一Java类名" id="另一个select映射SQL的ID">

SQL语句

<select>

注意:column属性的值必须与相应的SQL查询语句中的列名相同。MyBatis会将第一条SQL语句查询出来的该列的值用于所嵌套的SQL映射语句的入参。因第一条SQL语句查询出来的每个该列的值都将用于执行另一个SQL语句,所以嵌套的SQL语句将被多次执行。

2.集合的嵌套结果(resultMap)

<collection property="Java属性名" ofType="另一Java类名" javaType="ArrayList" resultMap="另一个resultMap的ID"/>

<resultMap="另一个resultMap的ID" type="另一Java类名">

<id property="id" column="关联主键ID"/>

........

</resultMap>

注意:column属性的值必须与相应的SQL查询语句的列名一样。

集合的嵌套查询(select)示例:

 

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  
<mapper namespace="com.myapp.mapper.UserMapper">  
  <select id="getUserList" resultMap="userdetailResult">  
    select * from t_user where id between 1 and 10  
  </select>  
  <select id="selectRoles" resultType="com.myapp.domain.Role" parameterType="int">  
    select * from t_user_role a,t_role b where a.user_id=#{id} and a.role_id=b.id  
  </select>  
  <resultMap id="userdetailResult"  type="User">   
         <id property="id"  column="user_id" />  
    <result property="name" column="user_name"/>  
    <result property="createDate" column="create_date"/>  
    <collection property="roles"  ofType="Role" javaType="ArrayList"  column="id" select="selectRoles"/>  
  </resultMap>  
</mapper>  

 

 

 

 

 

集合的嵌套结果(result)示例:

 

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  
<mapper namespace="com.myapp.mapper.UserMapper">  
  <select id="getUserList" resultMap="userdetailResult">  
    SELECT  
        u.id as user_id,  
        u.name as user_name,  
        u.create_date,  
        r.id as role_id,  
        r.name as role_name  
    FROM t_user u  
    LEFT JOIN t_user_role ur ON(u.id=ur.user_id)  
    LEFT JOIN t_role r ON(r.id=ur.role_id) where u.id=1  
  </select>  
  <resultMap id="userdetailResultNew"  type="User">   
    <id property="id"  column="user_id" />  
    <result property="name" column="user_name"/>  
    <result property="createDate" column="create_date"/>  
    <collection property="roles"  ofType="Role" javaType="ArrayList">   
        <id property="id"  column="role_id"/>   
        <result property="name"  column="role_name"/>  
    </collection>   
  </resultMap>  
  <resultMap id="roleResult" type="Role">  
    <id property="id"  column="role_id"/>   
    <result property="name"  column="role_name"/>  
  </resultMap>  
  <resultMap id="userdetailResult"  type="User">   
    <id property="id"  column="user_id" />  
    <result property="name" column="user_name"/>  
    <result property="createDate" column="create_date"/>  
    <collection property="roles"  ofType="Role" javaType="ArrayList" resultMap="roleResult"/>   
  </resultMap>  
</mapper>  

 

 

 

如果你只是简单的嵌套,可以像id="userdetailResultNew" 那样将要嵌套的结果直接写在collection子元素中去。

 

相关文章
|
27天前
|
SQL 安全 Java
resultMap 与 resultType、parameterMap 与 parameterType的区别
resultMap 与 resultType、parameterMap 与 parameterType的区别
36 0
|
3月前
|
SQL XML Java
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
49 0
|
5月前
|
SQL 安全 Java
MyBatis动态语句且如何实现模糊查询及resultType与resultMap的区别---详细介绍
MyBatis动态语句且如何实现模糊查询及resultType与resultMap的区别---详细介绍
104 0
|
7月前
|
SQL Java 数据库连接
【MyBatis】动态SQL > 重点:${...}和#{...}与resultMap和resultType的区别
【MyBatis】动态SQL > 重点:${...}和#{...}与resultMap和resultType的区别
88 0
|
12月前
|
SQL XML Java
Mybatis开发要点-resultType和resultMap的区别?
Mybatis开发要点-resultType和resultMap的区别?
88 0
使用RresultMap中collection做嵌套查询
注意:<collection>标签中的column:是要传递给select查询语句的参数,如果传递多个参数,格式为column= " {参数名1=表字段1,参数名2=表字段2}"
|
XML Java 数据库连接
MyBatis之ResultMap的association和collection标签详解(图文例子)
MyBatis之ResultMap的association和collection标签详解(图文例子)
368 0
MyBatis之ResultMap的association和collection标签详解(图文例子)
|
SQL Java 数据库连接
ResultMap与ResultType的区别
ResultMap与ResultType的区别
ResultMap与ResultType的区别
|
SQL 存储 Java
ResultSet/ResultSetMetaData相关和用法
ResultSet/ResultSetMetaData相关和用法
|
关系型数据库 Java 数据库连接

热门文章

最新文章