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.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
随机推荐
- CHAPTER 19 Ordering the World 第19章 分类世界
CHAPTER 19 Ordering the World 第19章 分类世界 Our planet is home to a bewildering variety of plants and an ...
- 傻瓜式搭建私有云就用这两组合:宝塔+kodexplorer
介绍 宝塔面板:是一款linux/windows平台均可使用的服务器管理软件,自带环境包,主要基于centos操作系统,可一键包装nginx.apache.php.mysql.pureftpd.php ...
- ossec代理
代理端 OSSEC有两种代理端:可安装的代理端和哑代理端(免安装).可安装的代理端被安装在主机上,通过OSSEC的加密协议将主机的信息发送到OSSEC服务器.亚代理端则不需在远端主机进行安装.他作为O ...
- Mysql Order By注入总结
何为order by 注入 本文讨论的内容指可控制的位置在order by子句后,如下order参数可控"select * from goods order by $_GET['order' ...
- Linux 做网关
首先创建两张路由表,只需要添加到相应的文件中即可,Linux一共支持255个路由表,rt_tables文件中默认已经存在了三张路由表,分别是: 255 local 254 main 2 ...
- imooc-c++学习感悟
imooc--慕课网c++课程链接:[课程链接](http://www.imooc.com/course/list?c=C+puls+puls) Imooc 慕课网c++学习感悟 1.课程名称:c++ ...
- ubuntu下安装matlab2015b
========= 安装过程 1.下载MATLAB2015b破解版 操作系统:Ubuntu 16.04 LTS 程序文件:Matlab2015b-glnxa64破解版 解压提取文件:在ubuntu系统 ...
- java9初探
最近研究了一下java9的新特性,也看完了<Modularity Programming in Java 9>,有一些收获写博客记录一下. 1.java9初探
- 【第二周】PSP
日期 C类别 C内容 S开始时间 E结束时间 I间隔(单位:分钟) T净时间(单位:分钟) 9月8日 编程 结对编程 12:15 13:15 10 50 编程 结对编程 16:35 17:30 ...
- web_custom_request和web_submit_data
网络上很多说明这2个函数区别的文章,我就从其他摘抄了内容,其中区别自己查看附录,我主要说明2点 (1)用web_custom_request提交请求如果是json,则会会使用关键字符{},但是{},是 ...