association分步查询
场景一
查询Employee的同时查询员工对应的部门
Employee===Department
一个员工有与之对应的部门信息
Employee表:
Department表:
public interface DepartmentMapper {public Department getDeptById(Integer id);}
public interface EmployeeMapperPlus {public Employee getEmpByIdStep(Integer id);}
<!-- public Department getDeptById(Integer id);
--><select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">select id,dept_name departmentName from tb1_dept where id = #{id}</select>
<resultMap id="MyEmpStep" type="com.atguigu.mybatis.bean.Employee"><!-- 1.先按照员工id查询员工信息--><!-- 2.根据查询员工信息中的d_id值去部门表查出部门信息--><!-- 3.部门设置到员工中--><id column="id" property="id"></id><result column="last_name" property="lastName"></result><result column="email" property="email"></result><result column="gender" property="gender"></result><!-- association定义关联对象的封装规则select:表明当前属性是调用select指定的方法查出的结果column:指定将哪一列的值传给这个方法流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性--><association property="dept" select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"column="d_id"></association></resultMap><!-- public Employee getEmpByIdStep(Integer id);--><select id = "getEmpByIdStep" resultMap="MyEmpStep">select * from tb1_employee where id = #{id}</select>
@Testpublic void test03() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);Employee emp = mapper.getEmpByIdStep(1);System.out.println(emp);System.out.println(emp.getDept());}finally {sqlSession.close();}}
延迟加载
Employee ===> Dept
我们每次查询Employee对象的时候,都将一起查询出来
分段查询的基础上,使用延迟加载
可以使部门信息在我们使用的时候再去查询
lazyLoadingEnabled & aggressiveLazyLoading
<settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>
场景二
查询部门的时候将部门对应的所有员工信息也查询出来
public class Department {private Integer id;private String departmentName;private List<Employee> emps;}
public interface DepartmentMapper {public Department getDeptByIdPlus(Integer id);}
<resultMap id="MyDept" type="com.atguigu.mybatis.bean.Department"><id column="did" property="id"></id><result column="dept_name" property="departmentName"></result><!--collection定义关联集合类型的属性的封装规则ofType:指定集合里面元素的类型--><collection property="emps" ofType="com.atguigu.mybatis.bean.Employee" ><!--定义这个集合中元素的封装规则--><id column="eid" property="id"></id><result column="last_name" property="lastName"></result><result column="email" property="email"></result><result column="gender" property="gender"></result></collection></resultMap><!-- public Department getDeptByIdPlus(Integer id);--><select id="getDeptByIdPlus" resultMap = "MyDept">SELECT d.id did, d.dept_name dept_name ,e.id eid,e.last_name last_name,e.email email,e.gender genderFROM tb1_dept d
LEFT JOIN tb1_employee e
ON d.id = e.d_id
WHERE d.id = #{id}</select>
@Testpublic void test04() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);Department deptByIdPlus = mapper.getDeptByIdPlus(1);System.out.println(deptByIdPlus);System.out.println(deptByIdPlus.getEmps());}finally {sqlSession.close();}}
扩展:多列的值传递过去
将多列的值封装map传递
column="{key1=column1,key2=column2}"
fetchType=“lazy”:表示使用延迟加载;
lazy:延迟
eager:立即