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>測試方法:?
@Testpublic 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>?
總結
以上是生活随笔為你收集整理的mybaits十一:使用association分步查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits十:关联查询
- 下一篇: mybaits十二:使用collecti