天天看點

mybaits十一:使用association分步查詢

DepartmentMapper.xml sql配置檔案 

<?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">
<!-- namespace不能随便自定義了,應該是接口類的全限定名 -->
<mapper namespace="com.atChina.dao.DepartmentMapper">
  
  <select id="getDepartByDeptno" resultType="com.atChina.bean.Department">
  	select * from depttest a where a.deptno = #{deptno}
  </select>
</mapper>           

EmployeePlusMapper.xml sql配置檔案 

<resultMap type="com.atChina.bean.Employee" id="defineEmpStep">
    	<id column="empno" property="empno" />
    	<result column="ename" property="ename" />
    	<result column="job" property="job" />
    	<result column="mgr" property="mgr" />
    	<result column="hiredate" property="hiredate" />
    	<result column="sal" property="sal" />
    	
    	<!-- association可以指定聯合的javaBean對象
    		 select:表明目前屬性是調用select指定的方法查出的結果
    		 column:指定将哪一列的值傳給這個方法
    		 
    		 流程: 使用select指定的方法(傳入colomn指定的這列作為參數的值)查出對象,
    		 并封裝給proterty指定的屬性.
    	 -->
    	<association property="dt" 
    		select="com.atChina.dao.DepartmentMapper.getDepartByDeptno"
    		column="deptno">
    	</association>
    </resultMap>
    <select id="getEmpAndDeptByStep" resultMap="defineEmpStep">
		select *
		 from emptest a where a.empno = #{empno}
	</select>           

測試方法: 

@Test
	public void test25() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession();
		try{
			// 命名空間.id,這樣别的配置檔案裡有同名的id,程式也不報錯
			EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);
			System.out.println(em.getClass()); // 動态代理類
			
			// Employee ee = em.getEmpAndDeptByEmpno(7369);
			Employee ee = em.getEmpAndDeptByStep(7369);
			System.out.println(ee);
		}finally{
			// 關閉
			openSession.close();
		}
	}           

測試結果: 分步執行了兩條sql語句

DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - ==>  Preparing: select * from emptest a where a.empno = ? 

DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - ==> Parameters: 7369(Integer)

DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO

DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - <==        Row: 7369, SMITH, CLERK, 7902, 1980-12-17 00:00:00, 800, null, 20

DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - ooo Using Connection [oracle.jdbc.driver.T4CConnection@786c1a82]

DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - ==>  Preparing: select * from depttest a where a.deptno = ? 

DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - ==> Parameters: 20(BigDecimal)

DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - <==    Columns: DEPTNO, DNAME, LOC, ID

DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - <==        Row: 20, RESEARCH, DALLAS, null

 延遲加載:

<settings>
		<!--開啟延遲加載-->
     	<setting name="lazyLoadingEnabled" value="true"/>
     	 <!--關閉積極加載-->
     	<setting name="aggressiveLazyLoading" value="false"/>
	</settings>