1、概述

2、实体类
主要体现在Java实体类中
● 双向:双方都可以访问到对方
○ Customer:包含Order的集合属性
○ Order:包含单个Customer的属性
● 单向:双方中只有一方能够访问到对方
○ Customer:不包含Order的集合属性,访问不到Order
○ Order:包含单个Customer的属性
public class Customer {
private Integer customerId;
private String customerName;
private List<Order> orderList;// 体现的是对多的关系
public class Order {
private Integer orderId;
private String orderName;
private Customer customer;// 体现的是对一的关系
3、对一
创建OrderMapper接口
public interface OrderMapper {
Order selectOrderWithCustomer(Integer orderId);
}
Mapper.xml配置文件
<!-- 创建resultMap实现“对一”关联关系映射 -->
<!-- id属性:通常设置为这个resultMap所服务的那条SQL语句的id加上“ResultMap” -->
<!-- type属性:要设置为这个resultMap所服务的那条SQL语句最终要返回的类型 -->
<resultMap id="selectOrderWithCustomerResultMap" type="com.atguigu.mybatis.entity.Order">
<!-- 先设置Order自身属性和字段的对应关系 -->
<id column="order_id" property="orderId"/>
<result column="order_name" property="orderName"/>
<!-- 使用association标签配置“对一”关联关系 -->
<!-- property属性:在Order类中对一的一端进行引用时使用的属性名 -->
<!-- javaType属性:一的一端类的全类名 -->
<association property="customer" javaType="com.atguigu.mybatis.entity.Customer">
<!-- 配置Customer类的属性和字段名之间的对应关系 -->
<id column="customer_id" property="customerId"/>
<result column="customer_name" property="customerName"/>
</association>
</resultMap>
<!-- Order selectOrderWithCustomer(Integer orderId); -->
<select id="selectOrderWithCustomer" resultMap="selectOrderWithCustomerResultMap">
SELECT order_id,order_name,c.customer_id,customer_name
FROM t_order o
LEFT JOIN t_customer c
ON o.customer_id=c.customer_id
WHERE o.order_id=#{orderId}
</select>
4、对多
创建OrderMapper接口
public interface CustomerMapper {
Customer selectCustomerWithOrderList(Integer customerId);
}
Mapper.xml配置文件
<!-- 配置resultMap实现从Customer到OrderList的“对多”关联关系 -->
<resultMap id="selectCustomerWithOrderListResultMap"
type="com.atguigu.mybatis.entity.Customer">
<!-- 映射Customer本身的属性 -->
<id column="customer_id" property="customerId"/>
<result column="customer_name" property="customerName"/>
<!-- collection标签:映射“对多”的关联关系 -->
<!-- property属性:在Customer类中,关联“多”的一端的属性名 -->
<!-- ofType属性:集合属性中元素的类型 -->
<collection property="orderList" ofType="com.atguigu.mybatis.entity.Order">
<!-- 映射Order的属性 -->
<id column="order_id" property="orderId"/>
<result column="order_name" property="orderName"/>
</collection>
</resultMap>
<!-- Customer selectCustomerWithOrderList(Integer customerId); -->
<select id="selectCustomerWithOrderList" resultMap="selectCustomerWithOrderListResultMap">
SELECT c.customer_id,c.customer_name,o.order_id,o.order_name
FROM t_customer c
LEFT JOIN t_order o
ON c.customer_id=o.customer_id
WHERE c.customer_id=#{customerId}
</select>
测试参考代码
@Test
public void testRelationshipToMulti() {
CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
// 查询Customer对象同时将关联的Order集合查询出来
Customer customer = customerMapper.selectCustomerWithOrderList(1);
System.out.println("customer.getCustomerId() = " + customer.getCustomerId());
System.out.println("customer.getCustomerName() = " + customer.getCustomerName());
List<Order> orderList = customer.getOrderList();
for (Order order : orderList) {
System.out.println("order = " + order);
}
}
5、对多分步查询
为了实现延迟加载,对Customer和Order的查询必须分开,分成两步来做,才能够实现。为此,我们需要单独查询Order,也就是需要在Mapper配置文件中,单独编写查询Order集合数据的SQL语句。
5.1 编写查询Customer的SQL语句
<resultMap id="selectCustomerWithOrderTowStepResultMap" type="com.atguigu.mybatis.entity.Customer">
<!-- 第一部分:映射Customer自己的对应关系 -->
<id column="customer_id" property="customerId"/>
<result column="customer_name" property="customerName"/>
<!-- 第二部分:映射关联关系 -->
<!-- property属性:指定在Customer实体类中建立关联关系的orderList属性 -->
<!-- column属性:用来给select属性指定的SQL语句传参数 -->
<!-- select属性:指定根据customerId查询Order集合的SQL语句 -->
<collection property="orderList"
column="customer_id"
select="com.atguigu.mybatis.mapper.OrderMapper.selectOrderListByCustomerId"/>
</resultMap>
<!-- Customer selectCustomerWithOrderTowStep(Integer customerId); -->
<select id="selectCustomerWithOrderTowStep" resultMap="selectCustomerWithOrderTowStepResultMap">
select customer_id,customer_name from t_customer where customer_id=#{customerId}
</select>
5.2 编写查询Order的SQL语句
<!-- 定位到当前SQL语句的方式:com.atguigu.mybatis.mapper.CustomerMapper.selectCustomerById -->
<!-- 这条SQL语句仅仅是根据id查询Customer -->
<select id="selectOrderList" resultType="com.atguigu.mybatis.entity.Order">
select order_id,order_name from t_order where customer_id=#{customer_id}
</select>
5.3 延迟加载
查询到Customer的时候,不一定会使用Order的List集合数据。如果Order的集合数据始终没有使用,那么这部分数据占用的内存就浪费了。对此,我们希望不一定会被用到的数据,能够在需要使用的时候再去查询。
例如:对Customer进行1000次查询中,其中只有15次会用到Order的集合数据,那么就在需要使用时才去查询能够大幅度节约内存空间。
延迟加载的概念:对于实体类关联的属性到需要使用时才查询。也叫懒加载。
较低版本
<!-- 使用settings对Mybatis全局进行设置 -->
<settings>
<!-- 开启延迟加载功能:需要配置两个配置项 -->
<!-- 1、将lazyLoadingEnabled设置为true,开启懒加载功能 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 2、将aggressiveLazyLoading设置为false,关闭“积极的懒加载” -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
较高版本
<!-- Mybatis全局配置 -->
<settings>
<!-- 开启延迟加载功能 -->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
测试参代码
@Test
public void testSelectCustomerWithOrderList() throws InterruptedException {
CustomerMapper mapper = session.getMapper(CustomerMapper.class);
Customer customer = mapper.selectCustomerWithOrderList(1);
// 这里必须只打印“customerId或customerName”这样已经加载的属性才能看到延迟加载的效果
// 这里如果打印Customer对象整体则看不到效果
System.out.println("customer = " + customer.getCustomerName());
// 先指定具体的时间单位,然后再让线程睡一会儿
TimeUnit.SECONDS.sleep(5);
List<Order> orderList = customer.getOrderList();
for (Order order : orderList) {
System.out.println("order = " + order);
}
}
效果:刚开始先查询Customer本身,需要用到OrderList的时候才发送SQL语句去查询
DEBUG 11-30 11:25:31,127 ==> Preparing: select customer_id,customer_name from t_customer where customer_id=? (BaseJdbcLogger.java:145)
DEBUG 11-30 11:25:31,193 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 11-30 11:25:31,314 <== Total: 1 (BaseJdbcLogger.java:145)
customer = c01
DEBUG 11-30 11:25:36,316 ==> Preparing: select order_id,order_name from t_order where customer_id=? (BaseJdbcLogger.java:145)
DEBUG 11-30 11:25:36,316 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 11-30 11:25:36,321 <== Total: 3 (BaseJdbcLogger.java:145)
order = Order{orderId=1, orderName='o1'}
order = Order{orderId=2, orderName='o2'}
order = Order{orderId=3, orderName='o3'}
博客围绕MyBatis展开,介绍实体类关联关系,包括双向和单向关联。阐述对一、对多查询的接口创建与配置文件编写,重点讲解对多分步查询及延迟加载,通过分开编写SQL语句,实现按需查询,节约内存空间。

2万+

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



