一、延迟加载
延迟加载:通俗来讲,就是按需查询;当有一个业务需要查询多个表时,可以先进行单表的查询,然后根据需求去查询关联表的信息,把对关联表的查询就称作为延迟加载。
二、使用association简单的实现延迟加载功能(collection同association)
1、目标:查询一个订单表orders(id,user_id,number,createtime,note)以及它关联的用户信息表user(id,username,birthday,sex,address)
SQL语句:
select orders.*,
(select username from user where orders_id=user.id)username,
(select sex from user where orders_id=user.id)sex,
(select address from user where orders_id=user.id)address
from orders
思路:首先查询单表,也就是orders表,然后根据需求去查询用户表
1、配置ordersMapper.xml文件
因为,我的findUserById不在ordersMapperxml中,所以我的select中加了namespace,即:com.mybatis.map