作者简介:大家好,我是码炫码哥,前中兴通讯、美团架构师,现任某互联网公司CTO,兼职码炫课堂主讲源码系列专题
代表作:《jdk源码&多线程&高并发》,《深入tomcat源码解析》,《深入netty源码解析》,《深入dubbo源码解析》,《深入springboot源码解析》,《深入spring源码解析》,《深入redis源码解析》等
联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬。码炫课堂的个人空间-码炫码哥个人主页-面试,源码等
回答
支持。Mybatis 通过 association、collection 标签来实现一对一和一对多的关联查询。
一对一关联查询
一对一关联查询支持两种方式:嵌套查询和结果集嵌套映射。核心是通过 association 标签支持。
public class User {
private Integer id;
private String name;
private Address address;
// getters and setters
}
public class Address {
private Integer id;
private String city;
private String province;
// getters and setters
}
- 嵌套查询(Nested Queries)
<mapper namespace="com.damingge.mapper.UserMapper">
<resultMap id="userMap" type="com.damingge.entity.User">
<id property="id" column="id" />
<result property="username" column="username" />
<association property="address" resultMap="addressMap" />
</resultMap>
<resultMap id="addressMap" type="com.damingge.entity.Address">
<id property="id" column="address_id" />
<result property="city" column="city" />
<result property="country" column="country" />
</resultMap>
<select id="selectUserById" resultMap="userResultMap">
SELECT u.id, u.name, a.id as address_id, a.city, a.province
FROM t_user u
LEFT JOIN t_address a ON u.address_id = a.id
WHERE u.id = #{id}
</select>
</mapper>
- 结果集嵌套映射(Result Map With Association)
<mapper namespace="com.damingge.mapper.UserMapper">
<resultMap id="userResultMap" type="com.damingge.entity.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<association property="address" column="address_id" javaType="com.damingge.entity.Address">
<id property="id" column="address_id"/>
<result property="city" column="city"/>
<result property="province" column="province"/>
</association>
</resultMap>
<select id="selectUserById" resultMap="userResultMap">
SELECT u.id, u.name, a.id as address_id, a.city, a.province
FROM t_user u
LEFT JOIN t_address a ON u.address_id = a.id
WHERE u.id = #{id}
</select>
</mapper>
一对多关联查询
一对多关联查询也支持两种方式:嵌套查询和结果集嵌套映射。核心是通过 collection 标签支持。
public class Department {
private Integer id;
private String name;
private List<Employee> employees;
// getters and setters
}
public class Employee {
private Integer id;
private String name;
private Integer departmentId;
// getters and setters
}
- 嵌套查询(Nested Queries)
<mapper namespace="com.damingge.mapper.DepartmentMapper">
<resultMap id="departmentResultMap" type="com.damingge.entity.Department">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="employees" ofType="com.damingge.entity.Employee" select="selectOrdersByDepartmentId"/>
</resultMap>
<resultMap id="employeeResultMap" type="com.damingge.entity.Employee">
<id property="id" column="employee_id"/>
<result property="name" column="employee_name"/>
<result property="departmentId" column="department_id"/>
</resultMap>
<select id="selectDepartmentById" resultMap="departmentResultMap">
SELECT d.id, d.name, d.employee_id
FROM t_department d
WHERE d.id = #{id}
</select>
<select id="selectEmployeesByDepartmentId" resultMap="employeeResultMap">
SELECT e.id, e.name, e.department_id
FROM t_employee e
WHERE e.department_id = #{departmentId}
</select>
</mapper>
- 集合嵌套结果映射
<mapper namespace="com.damingge.mapper.DepartmentMapper">
<resultMap id="departmentResultMap" type="com.damingge.entity.Department">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="employees" ofType="com.damingge.entity.Employee">
<id property="id" column="employee_id"/>
<result property="name" column="employee_name"/>
<result property="departmentId" column="department_id"/>
</collection>
</resultMap>
<select id="selectDepartmentById" resultMap="departmentResultMap">
SELECT d.id, d.name, e.id as employee_id, e.name as employee_name, e.department_id
FROM t_department d
LEFT JOIN t_employee e ON d.id = e.department_id
WHERE d.id = #{id}
</select>
</mapper>
扩展
嵌套查询 VS 嵌套结果
- 嵌套查询:嵌套查询是指在主查询中,对于每一条记录,再单独执行一次子查询来获取关联数据。这种方式通过多次数据库查询实现关联数据的获取,存在 N+1 问题,适用于数据量较小的场景。
- 嵌套结果:嵌套结果是指一次查询就获取所有关联数据,然后在 MyBatis 中通过 ResultMap 进行结果集的解析和映射。单次查询获取所有关联数据,适用于数据量较大的场景,但 SQL 语句复杂度高。
638

被折叠的 条评论
为什么被折叠?



