MyBatis.4关联
关联.多对一关联查询
package org.mybatis.example.dao; import java.util.Date; //雇员类 public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Integer sal; private Integer comm; private Dept dept; public Emp() { // TODO Auto-generated constructor stub } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getSal() { return sal; } public void setSal(Integer sal) { this.sal = sal; } public Integer getComm() { return comm; } public void setComm(Integer comm) { this.comm = comm; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }
//EmpMapper.java接口
package org.mybatis.example.dao; import java.util.List; public interface EmpMapper { public List<Emp>selectManytoOne(); }
关联关系,体现的是两个类之间的一种强依赖关系。比如在员工类中,有一个属性是部门类的对象;先看第一种 嵌套查询:
通过执行另外一个SQL映射语句来返回语气的复杂类型。
//整体mybatis配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/dao/DeptMapper.xml"/> <mapper resource="org/mybatis/example/dao/EmpMapper.xml"/> </mappers> </configuration>
//EmpMapper.xml文件
<?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"> <mapper namespace="org.mybatis.example.dao.EmpMapper"> <resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap"> <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"/> <result column="comm" property="comm"/> <association property="dept" column="deptno" javaType="org.mybatis.example.dao.Dept"> <id column="deptno" property="deptno"/> <result column="dname" property="dname"/> <result column="loc" property="loc"/> </association> </resultMap> <select id="selectManytoOne" resultMap="getEmpresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select> </mapper>
//测试类
import java.util.List; import org.apache.ibatis.session.SqlSession; import org.mybatis.example.dao.Emp; import org.mybatis.example.dao.EmpMapper; import org.mybatis.example.dao.SqlSessionFactoryUtil; public class Test21 { public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); EmpMapper empmapper=session.getMapper(EmpMapper.class); List<Emp>empList=empmapper.selectManytoOne(); for(Emp emp:empList){ System.out.println(emp.getEname()+"的部门是:"+emp.getDept().getDname()); } } }
2.嵌套结果查询:使用嵌套结果映射来处理重复的联合结果的子集。
EmpMapper.xml
<?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"> <mapper namespace="org.mybatis.example.dao.EmpMapper"> <resultMap id="empResult" type="org.mybatis.example.dao.Emp"> <association property="dept" column="deptno" javaType="org.mybatis.example.dao.Dept" select="selectDept"/> </resultMap> <select id="selectEmp" parameterType="int" resultMap="empResult"> select * from emp where empno=#{id} </select> <select id="selectDept" parameterType="int" resultType="org.mybatis.example.dao.Dept"> select * from dept where deptno=#{id} </select> <select id="selectManytoOne" resultMap="getEmpresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select> </mapper>
EmpMapper.java接口
增加方法:public List<Emp>selectEmp(int id);
测试类
public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); EmpMapper empmapper=session.getMapper(EmpMapper.class); List<Emp>empList=empmapper.selectEmp(2); System.out.println(empList.size()); for(Emp emp:empList){ System.out.println(emp.getEname()+"的部门是:"+emp.getDept().getDname()); } }
但是这种方法,在查询的时候只能孤立的查询某个员工id比较复杂,可以使用如下EmpMapper.xml的方式,来实现第一种的结果;
<?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"> <mapper namespace="org.mybatis.example.dao.EmpMapper"> <resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap"> <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"/> <result column="comm" property="comm"/> <association property="dept" column="deptno" javaType="org.mybatis.example.dao.Dept" resultMap="deptresultmap"> </association> </resultMap>
<resultMap type="org.mybatis.example.dao.Dept" id="deptresultmap">
<id column="deptno" property="deptno"/> <result column="dname" property="dname"/> <result column="loc" property="loc"/>
</result> <select id="selectManytoOne" resultMap="getEmpresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select> </mapper>
//一对多关联集合查询
重写下Dept类
增加多的一方的集合属性,private List<Emp>emps;并且添加相应的getter/setter方法;
重新配置DeptMapper.xml
<?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"> <mapper namespace="org.mybatis.example.dao.DeptMapper"> <resultMap type="org.mybatis.example.dao.Dept" id="deptresultMap"> <id column="deptno" property="deptno"/> <result column="dname" property="dname"/> <result column="loc" property="loc"/> <collection property="emps" ofType="org.mybatis.example.dao.Emp" resultMap="empresultmap"> </collection> </resultMap> <resultMap type="org.mybatis.example.dao.Emp" id="empresultmap"> <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"/> <result column="comm" property="comm"/> </resultMap> <select id="selectOnetoMany" resultMap="deptresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select> </mapper>
测试类代码
public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); DeptMapper deptmapper=session.getMapper(DeptMapper.class); List<Dept>deptList=deptmapper.selectOnetoMany(); for(Dept dept:deptList){ System.out.println("部门名称:"+dept.getDname()); System.out.println("所属员工的个数:"+dept.getEmps().size()); } }
MyBatis.4关联的更多相关文章
- MyBatis实现关联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- Mybatis之关联查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- MyBatis——实现关联表查询
原文:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创 ...
- mybatis 一对一关联 association 返回空值
mybatis 一对一关联 association 返回空值 最近学习spring mvc + mybatis开发,看的书是<Spring MVC+Mybatis开发 从入门到精通>,在学 ...
- SpringBoot+Mybatis实现关联查询
SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...
- Java基础-SSM之mybatis一对一关联
Java基础-SSM之mybatis一对一关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建husbands和wifes表并建 ...
- Mybatis系列(三):Mybatis实现关联表查询
原文链接:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 ...
- Mybatis表关联多对一
在上章的 一对多 中,我们已经学习如何在 Mybatis 中关联多表,但在实际项目中也是经常使用 多对一 的情况,这些查询是如何处理的呢,在这一节中我们来学习它.多表映射的多对一关系要用到 mybit ...
- Mybatis之关联查询及动态SQL
前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...
- MyBatis—实现关联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
随机推荐
- PLSQL触发器,游标
--触发器 drop table emp_log create table emp_log( empno number, log_date date, new_salary number, actio ...
- 你也可以手绘二维码(二)纠错码字算法:数论基础及伽罗瓦域GF(2^8)
摘要:本文讲解二维码纠错码字生成使用到的数学数论基础知识,伽罗瓦域(Galois Field)GF(2^8),这是手绘二维码填格子理论基础,不想深究可以直接跳过.同时数论基础也是 Hash 算法,RS ...
- 高可用OpenStack(Queen版)集群-7.Neutron控制/网络节点集群
参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...
- java后端面试题汇总
转载链接:https://www.nowcoder.com/discuss/90776?type=0&order=0&pos=23&page=0 基础篇 数据结构与算法 线性表 ...
- bg,fg,job命令详解
基础命令学习目录首页 原文链接:http://www.cnblogs.com/chjbbs/p/6307333.html linux提供的fg和bg命令,可以让我们轻松调度正在运行的任务 假如你发现前 ...
- 利用Cocoapods创建基于SVN的私有库podspec
由于项目年后要进行组件化,考虑到公司内部实现的一些私有组件,不对外公开,而又想在不同项目中使用,该怎么办呢?由于cocoapods有了强大的功能,可以自己创建podspec,更可以设置私有的库.那么利 ...
- 解决SecureCRT小键盘乱码
SecureCRT软件菜单,Options -> Session Options ->Terminal -> Emulation,右侧面板中"Terminal"选 ...
- 第十周psp作业
本周psp 本周进度条 代码累积折线图 博文字数累积折线图 饼状图
- 20162328蔡文琛 week09 大二
20162328蔡文琛 大二week09 教材学习内容总结 堆是一棵完全二叉树,其中每个元素大于等于其所有子节点的值. 向堆中添加一个元素的方法是,首先将这个元素添加为叶节点然后将其向上移动到合适的位 ...
- 20145214《网络攻防》逆向及Bof基础实践
20145214<网络攻防>逆向及Bof基础实践 实践说明 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...