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.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
随机推荐
- 4.three.js中的坐标系
Three.js中的坐标系 three.js中坐标系使用的是左手坐标系 左手坐标系和右手坐标系的对比: 当然three.js中使用的是右手坐标系 three.js中的旋转的定义 但是three.js中 ...
- Codeblocks自动代码格式化快捷键(自带)
代码区域右击 点format use AStyle 估计也就是考试竞赛逼着用这个
- SSH 报错解决方法记录汇总
SSH 密钥签名失败 情景: 使用 SSH 密钥验证身份时 报错: sign_and_send_pubkey: signing failed: agent refused operation 环境: ...
- uniq命令详解
基础命令学习目录首页 原文链接:http://man.linuxde.net/uniq 删除重复行: uniq file.txt sort file.txt | uniq sort -u file.t ...
- 第一章 HTML介绍
1.1 Html和CSS的关系 学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容就 ...
- 2-Tenth Scrum Meeting20151210
任务分配 闫昊: 今日完成:请假.(编译) 明日任务:参加会议讨论,安排任务分工. 唐彬: 今日完成:请假.(编译) 明日任务:参加会议讨论,安排任务分工. 史烨轩: 今日完成:请假.(编译) 明日任 ...
- 第三次实验报告 敏捷开发与XP实践
一. 实验内容 (一)敏捷开发与XP 摘要:一项实践在XP环境中成功使用的依据通过XP的法则呈现,包括:快速反馈.假设简单性.递增更改.提倡更改.优质工作.XP软件开发的基石是XP的活动,包括:编码 ...
- 初学Cocos2dx
初学cocos2dx Cocos2dx 中的主要概念包括:应用.导演.场景.层.精灵.动画.动作. Cocos2dx里面的主要类 1.CCObject Object Object Object 是co ...
- A Survey on the Security of Stateful SDN Data Planes
论文摘要: 本文为读者提供新兴的SDN带状态数据平面,集中关注SDN数据平面编程性带来的隐患. I部分 介绍 A.带状态SDN数据平面的兴起 B.带状态数据平面带来的安全隐患 引出带状态数据平面的安全 ...
- Sprint--5.21
看到作业要求组长就召开小组成员开了一个简短的会议,会议内容大致是这样的: 1.再次明确任务:就是每一个人都要清楚知道自己扮演的角色应该做些什么,怎么去做: 2.组长定时更新博客,每一位小组成员也要写进 ...