Mybatis的分段查询association和collection(对象属性是另一个对象)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38409944/article/details/82491506

联合查询 :
级联属性封装结果集
就是 对象中有个属性也是对象或者对象集合

一般这个时候用分段查询:

分段的部分:

前者是针对对象属性
后者是针对对象集合属性

方法1:传统方法

 <mapper namespace="dao.TeacherMapperPlus">
    <resultMap type="entity.Teacher" id="Teacher">
    <id column="id" property="id"/>
    <result column="gender" property="teacher_gender"/>
    <result column="name" property="name"/>
    <result column="sid" property="student.id"/>
    <result column="sname" property="student.name"/>
    </resultMap>
    <select id="getTeacherById" resultMap="Teacher">
         select t.id id,t.name name,t.gender gender,s.name sname,s.id sid
         from teachers t
         join schools s
         on t.school_id=s.id and t.id =#{id}
    </select>
</mapper>

问题:对象中有个对象属性

方法2:association 指定联合的javaBean对象,存放对象属性

property 指定哪个属性是联合的对象
javaType 指定这个属性对象的类型

重点:使用association进行分步查询

    ssociation定义关联对象的封装规则
    select:表明当前属性是调用select指定的方法查出的结果
    column:指定将哪一列的值传给这个方法

流程: select 指定方法 (传入column制定的这列参数的值)查出对象 并封装给property指定的对象

举例:
1、先按照学校id查询学校信息
2、根据查询学校信息中的school_id值去学校表查出学校信息
3、学校设置到教师中;

//查询学校信息
<mapper namespace="dao.schoolMapper">   
    <resultMap type="entity.school" id="schools">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    </resultMap>
    <select id="getSchoolById" resultMap="schools">
        SELECT * from schools where id=#{id}
    </select>
</mapper>

//查询教师信息,并添加学校信息(分步查询)
    <resultMap type="entity.Teacher" id="Teacher">
    <id column="id" property="id"/>
    <result column="gender" property="teacher_gender"/>
    <result column="name" property="name"/>
    <association property="school" select="dao.schoolMapper.getSchoolById" column="id">
        <id property="id"/>
        <result column="name" property="name"/>
    </association>
    </resultMap>
    <select id="getTeacherById" resultMap="Teacher">
        SELECT *from teachers where school_id=#{id}
    </select>

问题:对象中有个对象的List集合

方法3:collection标签存放对象集合

        1、先按照员工id查询员工信息
        2、根据查询员工信息中的d_id值去部门表查出部门信息
        3、部门设置到员工中;

采用collection分段查询:

部门查询:

<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <id column="dept_name" property="departmentName"/>
        <!-- emps是存放的属性 -->
        <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>

查询部门的时候将部门对应的所有员工信息也查询出来:

<select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where d_id=#{deptId}
</select>

如何传递多列的参数:
将多列的值封装map传递

column=“{key1=column1,key2=column2}”

collection还可以局部修改是否延迟加载
fetchType=”lazy”:表示使用延迟加载;

- lazy:延迟  //只要当需要的时候才会加载属性
- eager:立即

总结:
association和collection都可以分段查询 都可以使用column传递参数给

猜你喜欢

转载自blog.csdn.net/qq_38409944/article/details/82491506