多表连接:一次查询获取对象及其关联对象的数据,然后将它们映射成对象及其关联的对象。
这种方式主要执行一次查询即可。
当关联实体是1时,使用<one…/>元素来映射关联实体。
package lee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.itcheng.app.dao.AddressMapper;
import org.itcheng.app.dao.PersonMapper;
import org.itcheng.app.domain.Address;
import org.itcheng.app.domain.Person;
public class PersonManager
{
// SqlSessionFactory应该是应用级别
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 1. 创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2. 打开SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// findPerson(sqlSession);
/* 结果
* DEBUG [main] org.itcheng.app.dao.PersonMapper.getPerson ==> Preparing: select p.*, ad.* from person_inf p join address_inf ad on ad.owner_id = p.person_id where person_id = ?
* DEBUG [main] org.itcheng.app.dao.PersonMapper.getPerson ==> Parameters: 3(Integer)
* DEBUG [main] org.itcheng.app.dao.PersonMapper.getPerson <== Total: 1
* 白鼠精
* -----------------
* 陷空山无底洞
* */
findAddresses(sqlSession);
/* 结果
* DEBUG [main] org.itcheng.app.dao.AddressMapper.findAddressesById ==> Preparing: select ad.*, p.* from address_inf ad join person_inf p on ad.owner_id = p.person_id where addr_id > ?
* DEBUG [main] org.itcheng.app.dao.AddressMapper.findAddressesById ==> Parameters: 1(Integer)
* DEBUG [main] org.itcheng.app.dao.AddressMapper.findAddressesById <== Total: 4
* 花果山水帘洞 --> 孙悟空
* 福陵山云栈洞 --> 猪八戒
* 陷空山无底洞 --> 白鼠精
* 积雷山摩云洞 --> 玉面狐狸
* -----------------
* */
}
public static void findPerson(SqlSession sqlSession)
{
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
Person p = personMapper.getPerson(3);
System.out.println(p.getName());
System.out.println("-----------------");
// Person获取关联实体是延迟加载,要等到真正访问关联实体时才去执行额外的SELECT语句
System.out.println(p.getAddr().getDetail());
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void findAddresses(SqlSession sqlSession)
{
AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
List<Address> addrList = addressMapper.findAddressesById(1);
// 由于Address获取关联实体是立即加载,因此它会立即执行额外的SELECT语句
// 对于立即加载策略,即使不访问关联实体,它也会立即执行额外的SELECT语句
addrList.forEach(e -> {
System.out.println(e.getDetail() + " --> " + e.getPerson().getName());
});
System.out.println("-----------------");
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
}
接口
package org.itcheng.app.dao;
import java.util.List;
import org.itcheng.app.domain.Address;
// Mapper组件相当于DAO组件
public interface AddressMapper
{
List<Address> findAddressesById(Integer id);
}
package org.itcheng.app.dao;
import org.itcheng.app.domain.Person;
// Mapper组件相当于DAO组件
public interface PersonMapper
{
Person getPerson(Integer id);
}
映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 根元素是mapper,该元素的namespace属性值相当于该mapper的唯一标识 -->
<mapper namespace="org.itcheng.app.dao.AddressMapper">
<select id="findAddressesById" resultMap="addrMap">
select ad.*, p.*
from address_inf ad
join person_inf p
on ad.owner_id = p.person_id
where addr_id > #{id}
</select>
<resultMap type="address" id="addrMap">
<id column="addr_id" property="id"/>
<result column="addr_detail" property="detail"/>
<!-- 由于select是多表连接查询,
因此关联实体直接使用resultMap来定义映射 -->
<association property="person"
resultMap="org.itcheng.app.dao.PersonMapper.personMap" />
</resultMap>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 根元素是mapper,该元素的namespace属性值相当于该mapper的唯一标识 -->
<mapper namespace="org.itcheng.app.dao.PersonMapper">
<select id="getPerson" resultMap="personMap">
select
p.*, ad.*
from person_inf p
join address_inf ad
on ad.owner_id = p.person_id
where person_id = #{id}
</select>
<resultMap type="person" id="personMap">
<id column="person_id" property="id"/>
<result column="person_name" property="name"/>
<result column="person_age" property="age"/>
<!-- 由于select是多表连接查询,
因此关联实体直接使用resultMap来定义映射 -->
<association property="addr" resultMap="org.itcheng.app.dao.AddressMapper.addrMap"/>
</resultMap>
</mapper>
▲ 优点:
无论是否有关联实体,无论有多少条关联实体,只要一条Select语句即可获取全部的记录及其关联记录,因此查询性能较好。
▲ 缺点:
当MyBatis获取某个记录时,程序总会立即加载它所有的关联实体——当关联实体有很多时,会带来较大的内存压力。
【性能建议】:
1. 当关联实体只是单个实体(或数量较少),此时建议将关联实体立即加载出来(带来的内存压力非常小),
此时建议使用“基于多表连接查询的关联映射”策略。
2. 当关联实体只是N个实体(且数量可能较多),此时建议采用延迟加载策略。
此时建议使用“嵌套select的关联映射” + “开启延迟加载”策略。