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;
}   



    
    <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>

    <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:

    
     <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 property="dept" javaType="com.mybatis.bean.Department">
           <id column="d_id" property="id"/>
           <resutl column="dept_name" property="departmentName"/>
        association>

    resultMap>
    
    <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进行分步查询

    

     
     <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 property="dept"  select="com.mybatis.dao.DepartmentMapper.getDeptById" 
        column="d_id">
        association>
    resultMap>
    
    <select id="getEmpByIdStep" resultMap="MyEmpByStep">
        SELECT * FROM tbl_employee WHERE id=#{id}
    select>
public class Department {

    private Integer id;
    private String departmentName;
}


<mapper namespace="com.mybatis.dao.DepartmentMapper">
    
    <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="jdbcTypeForNull" value="NULL"/>
        
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    settings>

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

二、 collection

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


    
    <resultMap type="com.mybatis.bean.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        
        <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>
    
    <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分步查询


    <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>
    
    <select id="getDeptByIdStep" resultMap="MyDeptStep">
        select id,dept_name from tbl_dept where id=#{id}
    select>

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

    
    
    <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里的写法:


<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>

你可能感兴趣的:(mybatis)