日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

mybaits十一:使用association分步查询

發(fā)布時(shí)間:2025/6/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybaits十一:使用association分步查询 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。