Mybatis中的association级联查询

本文探讨了在Mybatis中如何使用association标签进行级联查询,以封装复杂对象。通过这种方式,可以实现对POJO中对象属性的联合查询,有效地组织数据。

POJO中的属性可能会是一个对象,我们可以使用联合查询,并以级联属性的方式封装对象.使用association标签定义对象的封装规则

public class Department {
	private Integer id ; 
	private String departmentName ;
//  省略 get/set方法
}
public class Employee {
	private Integer id ; 
	private String lastName; 
	private String email ;
	private String gender ;
	private Department dept ;
    // 省略 get/set方法
}

使用级联的方式:

<select id="getEmployeeAndDept" resultMap="myEmpAndDept" >
		SELECT e.id eid, e.last_name, e.email,e.gender ,d.id did, d.dept_name FROM tbl_employee e , tbl_dept d   WHERE e.d_id = d.id  AND e.id = #{id}
</select>
	
	<resultMap type="com.atguigu.mybatis.beans.Employee" id="myEmpAndDept">
		<id column="eid" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="email" property="email"/>
		<result column="gender" property="gender"/>
	    <!-- 级联的方式 -->
		<result column="did" property="dept.id"/>
	<result column="dept_name" property="dept.departmentName"/>
</resultMap>

Association的用法

<resultMap type="com.atguigu.mybatis.beans.Employee" id="myEmpAndDept">
		<id column="eid" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="email" property="email"/>
		<result column="gender" property="gender"/>
		<association property="dept" javaType="com.atguigu.mybatis.beans.Department">
			<id column="did" property="id"/>
			<result column="dept_name" property="departmentName"/>
		</association>
</resultMap>
在 Spring Boot 中使用 MyBatis 进行联查可以通过两种方式实现:使用 MyBatis 自带的 `association` 和 `collection` 标签或者使用注解 `@Results`。 1. 使用 MyBatis 自带的 `association` 和 `collection` 标签 `association` 标签用来描述一对一关系,`collection` 标签用来描述一对多关系。下面是一个使用 MyBatis 自带标签进行联查的示例: ```xml <select id="findUserById" parameterType="int" resultMap="userMap"> select * from user where id = #{id} </select> <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <association property="userInfo" resultMap="userInfoMap"/> <collection property="orders" resultMap="orderMap"/> </resultMap> <resultMap id="userInfoMap" type="UserInfo"> <id property="id" column="id"/> <result property="nickname" column="nickname"/> <result property="age" column="age"/> </resultMap> <resultMap id="orderMap" type="Order"> <id property="id" column="id"/> <result property="orderId" column="order_id"/> <result property="orderName" column="order_name"/> <result property="userId" column="user_id"/> </resultMap> ``` 在上面的例子中,`userMap` 这个结果映射中使用了 `association` 和 `collection` 标签,表示查用户时同时查用户信息和订单信息。其中 `association` 标签用来描述用户信息和用户之间的一对一关系,`collection` 标签用来描述用户和订单之间的一对多关系。MyBatis 会自动将查结果封装成对应的 Java 对象,完成联查。 2. 使用注解 `@Results` 除了使用 MyBatis 自带的标签外,也可以使用注解 `@Results` 来进行联查。下面是一个使用注解 `@Results` 进行联查的示例: ```java @Results({ @Result(property = "id", column = "id"), @Result(property = "username", column = "username"), @Result(property = "password", column = "password"), @Result(property = "userInfo", column = "id", one = @One(select = "com.example.demo.mapper.UserMapper.findUserInfoById")), @Result(property = "orders", column = "id", many = @Many(select = "com.example.demo.mapper.UserMapper.findOrdersByUserId")) }) User findUserById(int id); UserInfo findUserInfoById(int id); List<Order> findOrdersByUserId(int userId); ``` 在上面的例子中,使用了注解 `@Results` 来定义结果映射。其中 `@One` 和 `@Many` 注解用来描述一对一和一对多关系,分别指向查对应对象的方法。MyBatis 会自动通过这些注解来完成联查
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值