8.2、多对一映射处理
场景模拟:
查询员工信息以及员工所对应的部门信息
8.2.1、级联方式处理映射关系(这种方式,不推荐使用)
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<result column="did" property="dept.did"></result>
<result column="dname" property="dept.dname"></result>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid}
</select>
如果使用这种方式的话,相当于在Emp的实体类中就定义了不属于Emp的属性
8.2.2、使用association处理映射关系
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<association property="dept" javaType="Dept">
<id column="did" property="did"></id>
<result column="dname" property="dname"></result>
</association>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid}
</select>
8.2.3、分步查询
EmpMapper.java
/**
*通过分步查询查询员工信息
*@param eid
*@return
*/
Emp getEmpByStep(@Param("eid") int eid);
EmpMapper.xml
<resultMap id="empDeptStepMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<!--
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
-->
<association property="dept" select="com.softeem.MyBatis.mapper.DeptMapper.getEmpDeptByStep" column="did">
</association>
</resultMap>
<!--Emp getEmpByStep(@Param("eid") int eid);-->
<select id="getEmpByStep" resultMap="empDeptStepMap"> select * from t_emp where eid = #{eid}
</select>
DeptMapper.java
/**
*分步查询的第二步: 根据员工所对应的did查询部门信息
*@param did
*@return
*/
Dept getEmpDeptByStep(@Param("did") int did);
DeptMapper.xml
<!--Dept getEmpDeptByStep(@Param("did") int did);-->
<select id="getEmpDeptByStep" resultType="Dept"> select * from t_dept where did = #{did}
</select>
测试集:
package com.softeem.mappertest;import com.softeem.mapper.EmpMapper;
import com.softeem.pojo.Emp;
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.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class EmpMapperTest {private EmpMapper empMapper ;@Beforepublic void init() throws IOException {//InputStream in = UserMapperTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");//读取MyBatis的核心配置文件InputStream in = Resources.getResourceAsStream("mybatis-config.xml");//创建SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory,生产SqlSession对象SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(in);//创建SqlSession对象,此时通过SqlSession对象所操作的sql都必须手动提交或回滚事务//SqlSession sqlSession = sqlSessionFactory.openSession(false);//创建SqlSession对象,此时通过SqlSession对象所操作的sql都会自动提交SqlSession sqlSession = sqlSessionFactory.openSession(true);//通过代理模式创建UserMapper接口的代理实现类对象empMapper = sqlSession.getMapper(EmpMapper.class);}@Testpublic void findAll(){List<Emp> all = empMapper.findAll();all.forEach(System.out::println);}@Testpublic void getEmpAndDeptByEidTest(){Emp emp = empMapper.getEmpAndDeptByEid(7369);System.out.println("emp = " + emp);}@Testpublic void getEmpAndDeptByEid2Test(){Emp emp = empMapper.getEmpAndDeptByEid2(7369);System.out.println("emp = " + emp);}@Testpublic void getEmpByStepTest(){Emp empByStep = empMapper.getEmpByStep(7369);System.out.println("empByStep = " + empByStep);}
}