---例子:多个员工同属于一个部门。

//部门对象的接口、映射文件省略,跟员工逻辑差不多/* 员工对象的接口 */public interface EmployeeMapper { Employee get(Long id);} <!--员工对象的映射文件--><!-- 解决列名和属性名不匹配问题 --><resultMap id="BaseResultMap" type="Employee"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="dept_id" property="dept.id"/> </resultMap><!-- 查询操作 --><select id="get" resultMap="BaseResultMap"> select id, name, dept_id from employee2 where id = #{id}</select>/* 测试:查询指定员工id、name、所属的部门名称的信息 */ @Test public void testGet() throws Exception { SqlSession session = MyBatisUtil.getSession(); EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class); Employee e = employeeMapper.get(1L); System.out.println(e); //需要通过查询得到的dept_id查询获取得到部门对象 /** * 额外的查询语句,可以通过配置resultMap的association属性,让myBatis帮我们执行 */ //手动添加额外查询语句// Long dept_id = e.getDept().getId();// DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);// Department d = departmentMapper.get(dept_id);// e.setDept(d); System.out.println(e.getDept()); session.commit(); session.close(); } <!-- 解决列名和属性名不匹配问题 --> <resultMap id="BaseResultMap" type="Employee"> <id column="id" property="id"/> <result column="name" property="name"/> <!--<result column="dept_id" property="dept.id"/> --> <!-- 额外的SQL配置方式 association元素:配置单一元素的关联关系 property 属性:对象的属性 select 属性:发送额外的sql column 属性: 将指定的列的值传递给额外sql --> <association property="dept" select="com.shan.hello.mappe r.DepartmentMapper.get" column="dept_id" /> </resultMap>额外的 SQL 做映射配置会导致的问题:N+1问题例如:每个员工的部门编号dept_id 是不同的,当查询所有员工的id、name、所属的部门名称的信息时就会发送额外N条SQL语句。
N:发送额外N条SQL语句去查询员工所属的部门名称: select name from department where id = dept_id;
1:查询所有员工的id、name、所属的部门的编号dept_id:select * from employee;
----解决:使用内联映射(多表查询),此时一条 SQL 语句搞定。
----处理多表查询的结果集的方法:内联映射。
子元素result进行映射配置[内联映射-使用result]:
子元素association进行映射配置[内联映射-使用association]
★使用association元素,配置单一对象属性。
---例子:一个部门有多个员工。
<!-- 针对单一对象的属性,使用association元素 --><!-- 针对集合类型的属性,使用collection元素 --> <!-- 额外的SQL配置方式 collection元素:配置集合类型元素的关联关系 property 属性:对象的属性 select 属性:发送额外的sql column 属性: 将指定的列的值传递给额外sql--><resultMap id="BaseResultMap" type="Department"> <id column="id" property="id"/> <result column="name" property="name"/> <!--<result column="" property="emps"/> --> <collection property="emps" select="com.shan.hello.mapper.EmployeeMapper.get" column="id" > </collection></resultMap><select id="get" resultMap="BaseResultMap"> select id, name from department where id = #{id} </select><!-- 针对单一对象的属性,使用association元素 --><!-- 针对集合类型的属性,使用collection元素 --> <resultMap id="BaseResultMap" type="Department"> <id column="id" property="id"/> <result column="name" property="name"/> <!--<result column="" property="emps"/> --> <!-- 内联查询:ofType是集合中泛型的类型 --> <collection property="emps" ofType="Employee"> <result column="e_id" property="id"/> <result column="e_name" property="name"/> <result column="e_dept_id" property="deptId"/> </collection></resultMap><select id="get" resultMap="BaseResultMap"> <!-- select id, name from department where id = #{id} --> select d.id, d.name, e.id e_id, e.name e_name, e.dept_id e_dept_id from department d join employee2 e on d.id = e.dept_id where d.id = #{id} </select> <!-- 全局配置文件 --> <settings> <!-- 懒加载/延迟加载,开启延迟加载功能 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 设置不要积极地去查询关联对象 --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 延迟加载的触发方法 --> <setting name="lazyLoadTriggerMethods" value="close"/> </settings>assoication元素,通常需要使用多表查询操作,即内联查询collection元素,通常还要使用延迟加载,即额外SQL处理