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不能隨便自定義了,應(yīng)該是接口類的全限定名 --> <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可以指定聯(lián)合的javaBean對(duì)象select:表明當(dāng)前屬性是調(diào)用select指定的方法查出的結(jié)果column:指定將哪一列的值傳給這個(gè)方法流程: 使用select指定的方法(傳入colomn指定的這列作為參數(shù)的值)查出對(duì)象,并封裝給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>測(cè)試方法:?
@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,程序也不報(bào)錯(cuò)EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);System.out.println(em.getClass()); // 動(dòng)態(tài)代理類// Employee ee = em.getEmpAndDeptByEmpno(7369);Employee ee = em.getEmpAndDeptByStep(7369);System.out.println(ee);}finally{// 關(guān)閉openSession.close();}}測(cè)試結(jié)果: 分步執(zhí)行了兩條sql語(yǔ)句
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><!--開(kāi)啟延遲加載--><setting name="lazyLoadingEnabled" value="true"/><!--關(guān)閉積極加載--><setting name="aggressiveLazyLoading" value="false"/></settings>?
總結(jié)
以上是生活随笔為你收集整理的mybaits十一:使用association分步查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mybaits十:关联查询
- 下一篇: mybaits十二:使用collecti