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 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记录封装给Department的emps属性。

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

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

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

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

官方文档:
在这里插入图片描述

MyBatiscollection 元素可以用于处理一对多的关联查询,它可以将一个集合作为参数传递给 SQL 语句,从而实现对集合的批量操作。 下面是一个示例: ``` <select id="selectOrder" resultMap="orderResultMap"> SELECT * FROM orders WHERE order_id = #{orderId} </select> <resultMap id="orderResultMap" type="Order"> <id property="id" column="order_id" /> <result property="orderNo" column="order_no" /> <result property="status" column="status" /> <collection property="items" ofType="OrderItem"> <id property="id" column="item_id" /> <result property="name" column="item_name" /> <result property="price" column="price" /> </collection> </resultMap> ``` 在上面的示例中,我们定义了一个 select 标签和一个 resultMap 标签,其中 resultMap 标签定义了一个 Order 对象和一个 OrderItem 集合,它们之间是一对多的关系。 当执行 selectOrder 方法时,MyBatis 会根据 orderResultMap 映射规则查询结果映射为一个 Order 对象,其中 Order 对象的 items 属性是一个 OrderItem 集合,它会自动从数据库中查询出来并填充到集合中。 注意,这里的 collection 元素的属性包括 property(集合属性名)、ofType(集合元素类型)等。在 SQL 语句中,我们可以使用 #{propertyName} 占位符来引用集合属性。例如: ``` <select id="selectOrdersByUser" resultMap="orderResultMap"> SELECT * FROM orders WHERE user_id = #{userId} <foreach item="item" index="index" collection="itemIds" open="AND order_id IN (" separator="," close=")"> #{item} </foreach> </select> ``` 在上面的示例中,我们使用了 foreach 元素来遍历 itemIds 集合,并将其中的元素作为参数传递给 SQL 语句。注意,在 SQL 语句中,我们使用了 IN 关键字来查询多个订单,这个关键字中的参数是通过 foreach 元素动态生成的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值