• mybatis使用collection查询集合属性规则


    接上篇mybatis使用associaton进行分步查询

    相关的类还是上篇中的类。

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

    DepartmentMapper.xml

    <!--嵌套结果集的方式,使用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>
    

    collection分步查询

    先通过部门表的id查出部门信息,再通过员工表的部门id查出所有的员工信息,也就是Department中的private List<Employee> emps;的属性信息

    DepartmentMapper.xml:首先通过id="getDeptByIdStep"的sql查出部门信息

    再通过collection中的select="com.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"调用EmployeeMapper.xml中的查询语句,column="id"为传递的查询条件的值,也就是将这个值赋给EmployeeMapper.xml中的#{deptId}

    	<!-- 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.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
    			column="id"></collection>
    	</resultMap>
    	<!-- public Department getDeptByIdStep(Integer id); -->
    	<select id="getDeptByIdStep" resultMap="MyDeptStep">
    		select id,dept_name from tbl_dept where id=#{id}
    	</select>
    

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

    最后呢,也就是将查询到的员工信息,即多条Employee记录封装给Departmentemps属性。

    注意:collection的分步查询也是可以延迟加载的,具体配置与上篇中的association一致


    另外,collection元素中还有个fetchType类型,也是用来控制延迟加载的,不过比全局配置的优先级更高。

    fetchType 可选的。有效值为 lazyeager。 指定属性后,将在映射中忽略全局配置参数 lazyLoadingEnabled,使用属性的值。

    补充:collection中的column属性是数据库中的列名,或着是列的别名,用来传递给select属性所指定语句中的参数,那如果需要传递多个参数该怎么写?

    官方文档:

    你所看得到的天才不过是在你看不到的时候还在努力罢了!
  • 相关阅读:
    Atitit.安全性方案规划设计4gm  v1 q928
    Atitit ati licenseService    设计原理
    Atitit.js图表控件总结
    Atitit. null错误的设计 使用Optional来处理null
    System.Web.Mvc 命名空间
    provider: SQL Network Interfaces, error: 26 Error Locating Server/Instance Specified
    Visual Studio 2010 实用功能总结
    My First J2ME
    Java开发利器ideaIU最新版本10.5的keygen
    Happy New Year for 2012
  • 原文地址:https://www.cnblogs.com/heliusKing/p/11173362.html
Copyright © 2020-2023  润新知