mybatis的resultMap属性----association和collection

一、association

1、级联查询的时候,可以在一个 java bean里定义一个要关系 的类类型。在resultMap中写bean.属性名从而关联别一个表的字段, eg;

@Alias("emp")
public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Department dept;
}   
<!-- 
    场景一:
        查询Employee的同时查询员工对应的部门
        Employee===Department
        一个员工有与之对应的部门信息;
        id  last_name  gender    d_id     did  dept_name (private Department dept;)
     -->


    <!--
        联合查询:级联属性封装结果集
      -->
    <resultMap type="com.mybatis.bean.Employee" id="MyDifEmp">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <result column="did" property="dept.id"/>
        <result column="dept_name" property="dept.departmentName"/>
    </resultMap>
<!--  public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpAndDept" resultMap="MyDifEmp">
        SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
        d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
        WHERE e.d_id=d.id AND e.id=#{id}
    </select>

2、如果javaBea里的属性是一个对象时,除了上述的对象名.属性名之外还可以使用 association指定联合的javaBean对象,eg:

    <!-- 
        使用association定义关联的单个对象的封装规则;
     -->
     <resultMap type="com.mybatis.bean.Employee" id="MyDifEmp">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <!--  association可以指定联合的javaBean对象
        property="dept":指定哪个属性是联合的对象
        javaType:指定这个属性对象的类型[不能省略]
        -->
        <association property="dept" javaType="com.mybatis.bean.Department">
           <id column="d_id" property="id"/>
           <resutl column="dept_name" property="departmentName"/>
        </association>

    </resultMap>
    <!--  public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpAndDept" resultMap="MyDifEmp">
        SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
        d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
        WHERE e.d_id=d.id AND e.id=#{id}
    </select>

3、使用association进行分步查询

    <!-- 使用association进行分步查询:
        1、先按照员工id查询员工信息
        2、根据查询员工信息中的d_id值去部门表查出部门信息
        3、部门设置到员工中;
     -->

     <!--  id  last_name  email   gender    d_id   -->
     <resultMap type="com.mybatis.bean.Employee" id="MyEmpByStep">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <!-- association定义关联对象的封装规则
            select:表明当前属性是调用select指定的方法查出的结果
            column:指定将哪一列的值传给这个方法

            流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
         -->
        <association property="dept"  select="com.mybatis.dao.DepartmentMapper.getDeptById" 
        column="d_id">
        </association>
    </resultMap>
    <!--  public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpByIdStep" resultMap="MyEmpByStep">
        SELECT * FROM tbl_employee WHERE id=#{id}
    </select>
public class Department {

    private Integer id;
    private String departmentName;
}
<?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.mybatis.dao.DepartmentMapper">
    <!--public Department getDeptById(Integer id);  -->
    <select id="getDeptById" resultType="com.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>
</mapper>

注:第三种方式使用association分部查询可以实现延迟加载。即为使用的时候才调用查询,不使用的时候,即不调用,节省资源。例如,上述的分部查询,如果不使用dept这个对象属性的,第三次的查询则不会被调用 。此时要使用setting里的lazyLoadingEnabled和aggressiveLazyLoading配置进行实现:

<settings>
        <!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
        <setting name="jdbcTypeForNull" value="NULL"/>
        <!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

其他查询写法均如上,只是添加了一个 setting里的配置项,这样一来,在调用Employee对象时,如果不调用Department对象属性,则其关联的那个departemnt表的查询就不会被启用,如果用到了 department对象属性,才会调用其实分部查询里关联的这个查询,从而实现了延迟加载。

二、 collection

association是关联一个对象,如果一个员工只能属性一个部们,此时查询员工作时定义一个部门对象属性即可获得一个员工的部门信息。然而一个部门里会有很多的员工,在查询部门时,想查询其中所有的员工是就要定义一个集合了,对象属性是实现的不了的,因为是一对多的关系,现在就来说下resultMap里怎么定义集体:

<!-- 
    javaBean:
    public class Department {
            private Integer id;
            private String departmentName;
            private List<Employee> emps;
       sql查询出的列字段:
      did  dept_name  ||  eid  last_name  email   gender  
     -->
    <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
    <resultMap type="com.mybatis.bean.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
            collection定义关联集合类型的属性的封装规则 
            ofType:指定集合里面元素的类型
        -->
        <collection property="emps" ofType="com.mybatis.bean.Employee">
            <!-- 定义这个集合中元素的封装规则 -->
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
    <!-- public Department getDeptByIdPlus(Integer id); -->
    <select id="getDeptByIdPlus" resultMap="MyDept">
        SELECT d.id did,d.dept_name dept_name,
                e.id eid,e.last_name last_name,e.email email,e.gender gender
        FROM tbl_dept d
        LEFT JOIN tbl_employee e
        ON d.id=e.d_id
        WHERE d.id=#{id}
    </select>

colledtion分步查询

<!-- collection:分段查询 -->
    <resultMap type="com.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <id column="dept_name" property="departmentName"/>
        <collection property="emps" 
            select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
            column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>
    <!-- public Department getDeptByIdStep(Integer id); -->
    <select id="getDeptByIdStep" resultMap="MyDeptStep">
        select id,dept_name from tbl_dept where id=#{id}
    </select>

com.mybatis.bean.Department此为一个查询员工的sql:

    <!-- 
    场景二:
        查询部门的时候将部门对应的所有员工信息也查询出来:注释在DepartmentMapper.xml中
     -->
    <!-- public List<Employee> getEmpsByDeptId(Integer deptId); -->
    <select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where d_id=#{deptId}
    </select>

注:也可以是按需加载,写法同上用setting的lazyLoadingEnabled和aggressiveLazyLoading控制
association和collection中都会有column,如果只有一个参数数,那可以看association里的写法则为:

<association property="dept" javaType="com.mybatis.bean.Department">
           <id column="d_id" property="id"/>
           <resutl column="dept_name" property="departmentName"/>
        </association>

如果查询需要多个参数时,多列的值传递过去:将多列的值封装map传递,如collection里的写法:

<!-- 扩展:多列的值传递过去:
            将多列的值封装map传递;
            column="{key1=column1,key2=column2}"
        fetchType="lazy":表示使用延迟加载;如果全局配置文件里开启了延迟加载,相让这个立即加载便可以用这个fetchType控制
                - lazy:延迟
                - eager:立即
                -
     -->
<resultMap type="com.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <id column="dept_name" property="departmentName"/>
        <collection property="emps" 
            select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
            column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,ResultMap是用来映射查询结果到Java对象的工具。AssociationResultMap中的一个标签,用于描述一个一对一的关联关系。 在ResultMap中使用Association标签可以定义一个复杂类型的关联关系,它可以将查询结果中的某些字段映射到一个子对象中。Association标签需要设置property属性来指定Java对象中对应的属性名,column属性来指定查询结果中对应的列名。 使用Association标签的示例代码如下所示: ``` <resultMap id="orderResultMap" type="Order"> <id property="id" column="order_id" /> <result property="orderNo" column="order_no" /> <association property="customer" javaType="Customer"> <id property="customerId" column="customer_id" /> <result property="customerName" column="customer_name" /> <result property="customerAddress" column="customer_address" /> </association> </resultMap> ``` 在这个示例中,我们定义了一个名为orderResultMapResultMap,映射的Java对象类型是Order。在Order对象中,有一个名为customer的属性,它是一个Customer类型的对象。使用Association标签,我们定义了customer属性的映射关系,将查询结果中的customer_id映射到Customer对象的customerId属性,将customer_name映射到customerName属性,将customer_address映射到customerAddress属性。 这样,在查询结果中如果有对应的关联数据,MyBatis就会自动将查询结果映射到Java对象的关联属性中。 希望这个解释能帮到你,如果有需要进一步了解的话可以收藏起来哦!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatisResultMap的associationcollection标签详解(图文例子)](https://blog.csdn.net/qq_52423918/article/details/120828850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mybatisresultmap、associationcollection使用](https://blog.csdn.net/weixin_44236424/article/details/125765174)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值