嵌套映射

1. 多对一嵌套查询映射使用案例

package com.zixue.dao;import com.zixue.annotation.MyBatisRepository;
import com.zixue.entity.Emp;/*** 员工表的DAO组件* */
@MyBatisRepository
public interface EmpDao {void save(Emp emp);Emp findById(int id);Emp findById2(int id);
}
EmpDao.java
<!-- 使用嵌套查询 在查询一条员工数据时 关联查询出对应的部门 --><select id="findById" parameterType="java.lang.Integer" resultMap="empMap">select * from t_emp where empno=#{id}</select><select id="findDept" parameterType="java.lang.Integer" resultType="com.zixue.entity.Dept">select * from t_dept where deptno=#{deptno}</select><resultMap type="com.zixue.entity.Emp" id="empMap"><association property="dept" column="deptno" javaType="com.zixue.entity.Dept" select="findDept"></association></resultMap>

2.多对一嵌套结果映射使用案例

 <!-- 使用嵌套结果 在查询一条员工数据时 关联查询出对应的部门 --><select id="findById2" parameterType="java.lang.Integer" resultMap="empMap">select e.*,d.*  from t_emp e inner join t_dept d on e.deptno=d.deptno where e.empno=#{id}</select><resultMap type="com.zixue.entity.Emp" id="empMap2"><id property="empno" column="empno"/><result property="ename" column="ename"/><result property="job" column="job"/><result property="mgr" column="mgr"/><result property="hiredate" column="hiredate"/><result property="sal" column="sal"/><result property="comm" column="comm"/><result property="deptno" column="deptno"/><association property="dept" column="deptno" javaType="com.zixue.entity.Dept"><id property="deptno" column="deptno"/><result property="dname" column="dname"/><result property="loc" column="loc"/></association></resultMap>
@Testpublic void testFindById2(){ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");EmpDao dao =ac.getBean(EmpDao.class);Emp e =dao.findById(100);System.out.println(e.getEname()+" "+e.getJob()+" "+e.getEmpno()+" "+e.getDept().getDeptno()+" "+e.getDept().getDname()        );

 3.一对多嵌套查询映射

/*** 关联属性 用于封装部门对应的员工信息* */private List<Emp> emps;

  

package com.zixue.dao;import com.zixue.annotation.MyBatisRepository;
import com.zixue.entity.Dept;@MyBatisRepository
public interface DeptDao {Dept findById(int id);Dept findById2(int id);
}

  

<mapper namespace="com.zixue.dao.DeptDao">
<!-- 嵌套查询 -->
<select id="findById" parameterType="java.lang.Integer" resultMap="deptMap">
select * from t_dept where deptno=#{id}
</select>
<select id="findEmps" parameterType="java.lang.Integer" resultType="com.zixue.entity.Emp">
select * from t_emp where deptno=#{deptno}
</select>
<resultMap type="com.zixue.entity.Dept" id="deptMap">
<id property="deptno" column="deptno"/>
<collection property="emps" column="deptno" javaType="java.util.ArrayList" select="findEmps" ofType="com.zixue.entity.Emp"></collection>
</resultMap>
DeptMapper.xml

4.一对多嵌套结果查询

<!-- 嵌套结果 -->
<select id="findById2" parameterType="java.lang.Integer" resultMap="deptMap2">select d.*,e.* from t_dept d inner join t_emp e on d.deptno=e.deptno where d.deptno=#{id}</select><resultMap type="com.zixue.entity.Dept" id="deptMap2"><id property="deptno" column="deptno"/><result property="dname" column="dname" jdbcType="VARCHAR" javaType="string"/><result property="loc" column="loc" jdbcType="VARCHAR" javaType="string"/><collection property="emps" ofType="com.zixue.entity.Emp" javaType="java.util.ArrayList" column="deptno"><id property="empno" column="empno"/><result property="ename" column="ename"/><result property="job" column="job"/><result property="mgr" column="mgr"/><result property="hiredate" column="hiredate"/><result property="sal" column="sal"/><result property="comm" column="comm"/><result property="deptno" column="deptno"/></collection></resultMap>
DeptMapper.xml
/*** 一对多嵌套结果映射* 查询部门及部门下所有的员工* */@Testpublic void testFindEmps2(){ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");DeptDao dao =ac.getBean(DeptDao.class);Dept d =dao.findById2(10);System.out.println(d.getDeptno()+" "+d.getDname()+" "+d.getLoc()        );List<Emp> emps =d.getEmps();for(Emp e: emps){System.out.println(e.getEname()+" "+e.getJob()+" "+e.getEmpno());    }}
Test

 

转载于:https://www.cnblogs.com/erbinok/p/9091554.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280230.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gopro dataset_如何将GoPro安装到DSLR相机

gopro datasetIf you have a DSLR camera with a hot shoe, it’s easy to attach various flashes and other accessories right to your camera. But with a couple of cheap attachments on hand, you can mount your GoPro to your DSLR camera as well. 如果您的DSLR相机带…

jQuery已经过时了,还需要学吗?

说起jQuery&#xff0c;很多刚参加工作的程序员都没用过&#xff0c;甚至没听过。曾几何时jQuery可是秒杀一切Js库&#xff0c;大有一统江山的情况&#xff0c;可是在顶峰的时候&#xff0c;瞬间被Vue、React、Angela三大框架斩于马下。从百度指数&#xff0c;我们也看出在2015…

Bootstrap01

Bootstrap01内容概要 一.使用Bootstrap的步骤 1.下载Bootstrap类库,包含三个部分,fonts,css,Bootstrap 2.导入项目中,在头部引入JQ,css和Bootstrap 注意:JQ要引入在Bootstrap前面! 3.使用css样式时,全部使用class属性 二.全局CSS概要 1.仅支持H5文档格式 2.移动设备优先,需要在…

ios raise_如何在iOS 10中关闭“ Raise to Wake”

ios raiseRaise to Wake is a new Lock screen feature available in iOS 10. It allows you to wake your phone’s screen simply by picking up your phone. This feature is on by default, but if you’d rather not use it, it’s simple to turn off. “唤醒”是iOS 10中…

资源调度器调研

2019独角兽企业重金招聘Python工程师标准>>> 场景描述&#xff1a; 异步触发和Crontab触发 YARN(Yet Another Resource Negotiator)Hadoop 资源管理器 主要构成&#xff1a; RM(ResourceManager)是一个全局的资源管理器&#xff0c;负责整个系统的资源管理和分配。…

WPF-19 IValueConverter接口

我们先来看看微软官方给出的定语&#xff1a;提供将自定义逻辑应用于绑定的方法&#xff0c;我们来看一下该接口的定义&#xff0c;Convert提供了将数据源到UI的格式化&#xff0c;ConvertBack表示反向namespace System.Windows.Data {//// Summary:// Provides a way to a…

使用 Azure CLI 将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager 部署模型

以下步骤演示如何使用 Azure 命令行接口 (CLI) 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型。 本文中的操作需要 Azure CLI。 Note 此处描述的所有操作都是幂等的。 如果你遇到功能不受支持或配置错误以外的问题&#xff0c;建议你…

黑苹果不能imessage_如何修复iMessage在iOS 10中不显示消息效果

黑苹果不能imessageiMessage got a huge update in iOS 10, adding things like third-party app integration, rich links, and a number of fun graphical effects for messages. If you’re seeing messages that say something like “(sent with Invisible Ink)” instead…

从技术总监到开源社区运营:过去两年,我都做了点啥?

这是头哥侃码的第267篇原创今天&#xff0c;这是我离开前公司的第 7 天。相信有不少吃瓜群众都很好奇&#xff0c;你这些天都在干啥&#xff1f;是不是蓬莱乐逍遥&#xff0c;过上了那悠闲的神仙日子&#xff1f;还是趁着疫情管控逐渐放开&#xff0c;和家人一起去深山老林里吸…

查看模拟器使用端口_为什么我们仍然使用模拟音频端口?

查看模拟器使用端口When leaks about what the chassis of the iPhone 7 might look like hit headlines earlier this week, technology columnists and industry analysts jumped on the chance to report that Apple’s next device may finally ditch its 3.5mm audio port…

如何更改Windows 10锁定屏幕超时

By default, Windows 10’s lock screen times out and switches off your monitor after one minute. If you’d like it to stick around longer than that–say, if you have background picture you like looking at or you enjoy having Cortana handy–there’s a simple…

ios 开发账号 退出协作_如何在iOS 10中的Notes上进行协作

ios 开发账号 退出协作iOS’ Notes app provides a convenient way to remember the great ideas you come up with and all the things you have to do. The app has evolved over the years, and iOS 10 adds even more features–including collaboration. iOS的Notes应用程…

为什么Android Geeks购买Nexus设备

The Galaxy S III is the highest-selling Android phone, but much of the geeky buzz is around the Nexus 4 – and the Galaxy Nexus before it. Nexus devices are special because they don’t have some of Android’s biggest problems. Galaxy S III是最畅销的Android…

day4----函数-闭包-装饰器

day4----函数-闭包-装饰器 本文档内容&#xff1a; 1 python中三种名称空间和作用域 2 函数的使用 3 闭包 4 装饰器 一 python中三种名称空间和作用域 1.1名称空间&#xff1a; 当程序运行时&#xff0c;代码从上至下依次执行&#xff0c;它会将变量与值得关系存储在一个空间中…

滤波器和均衡器有什么区别_什么是均衡器,它如何工作?

滤波器和均衡器有什么区别It’s in your car, home theater system, phone, and audio player but it doesn’t have an instruction manual. It’s an equalizer, and with a little know-how you can tweak your audio and fall in love with it all over again. 它在您的汽车…

网络视频监控与人脸识别

明天又要去面试了&#xff0c;趁次机会也将以前做的东西总结一下&#xff0c;为以后理解提供方便&#xff0c;也再加深下印象。 网络视频监控与人脸识别主要由三个程序组成&#xff1a;1、视频采集与传输程序&#xff1b;2、接受与显示程序&#xff1b;3、人脸识别程序。下面就…

esxi.主机配置上联端口_为什么现代的电脑机箱仍然具有USB 2.0端口?

esxi.主机配置上联端口With USB 3.0 becoming more prevalent with each passing year now, you may have found yourself wondering why modern computers still have USB 2.0 ports built into them. With that in mind, today’s SuperUser Q&A post has the answers to…

使用命令导入、导出mysql数据

1.导出全部数据库 利用mysqldump的—all-databases参数可以一口气把你数据库root用户下的所有数据库一口气导出到一个sql文件里。然后&#xff0c;重装系统后使用source命令可以再一口气倒回来。 需要确定mysql安装的路径&#xff1a;本机是&#xff1a;C:\Program Files\MySQL…

【原理图操作】原理图更新PCB时未改动元器件布局变动问题?

转载PCB布局、布线完工之后&#xff0c;由于设计功能&#xff0c;发现不完善时, 原理图部分功能需要改动&#xff0c;再改原理图&#xff0c;修改完成后&#xff0c;导入PCB过程中&#xff0c;发现PCB中未改动&#xff08;部分&#xff09;的元器件 布局发生了变化&#xff0c;…

关闭edge任务栏预览_如何在Microsoft Edge中关闭选项卡预览

关闭edge任务栏预览Now that it has extension support, Microsoft Edge is becoming a more and more viable browser. One feature people seem to either love or hate is the pop-up preview you get when you hover over a tab. There’s no built-in setting that lets y…