MyBatis的collection集合封装规则

本文探讨了MyBatis中collection集合的封装规则,以DepartmentMapper.java为例,展示了如何进行分段查询,涉及MyBatis的级联查询和分布式查询策略。
摘要由CSDN通过智能技术生成

MyBatis的collection集合封装规则



DepartmentMapper.java

package com.cn.mybatis.dao;

import java.util.List;
import java.util.Map;


import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;

import com.cn.zhu.bean.Department;
import com.cn.zhu.bean.Employee;

public interface DepartmentMapper {
	public Department getDeptById(Integer id);
	public Department getDeptByIdPlus(Integer id);
	
	public Department getDeptByIdStep(Integer id);
	public List<Employee> getEmpsByDeptId(Integer deptId);
 }
   
EmployeeMapperPlus.java

</

MyBatis 中的 collection 标签用于处理查询语句返回多条记录时,将多条记录封装到一个集合中。collection 标签只能用于 resultMap 元素中。 在 resultMap 元素中使用 collection 标签,需要指定以下属性: - property:指定集合类型属性的名称。 - ofType:指定集合类型属性的数据类型。 - resultMap:指定为集合中元素的映射关系。 - select:指定查询语句的 id。 下面是 collection 标签的一个示例: ```xml <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <collection property="orders" ofType="Order" resultMap="orderMap"> <id property="id" column="id"/> <result property="userId" column="user_id"/> <result property="orderNo" column="order_no"/> <result property="amount" column="amount"/> </collection> </resultMap> <resultMap id="orderMap" type="Order"> <id property="id" column="id"/> <result property="userId" column="user_id"/> <result property="orderNo" column="order_no"/> <result property="amount" column="amount"/> </resultMap> ``` 在上述示例中,我们定义了两个 resultMap,一个是 userMap,另一个是 orderMap。userMap 中使用了 collection 标签,将订单封装到了 User 对象的 orders 属性中。 注意,使用 collection 标签需要指定 ofType 属性,指定集合中元素的数据类型。在上述示例中,orders 是一个 List<Order> 类型的属性,因此我们指定了 ofType 属性为 Order。 同时,在 collection 标签中还可以使用 select 属性,指定查询语句的 id。这个查询语句的返回结果将作为集合的元素。 ```xml <collection property="orders" ofType="Order" select="findOrdersByUserId"/> ``` 在上述示例中,我们使用了 select 属性,指定了一个查询语句的 id,这个查询语句的返回结果将作为 orders 集合的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值