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.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
随机推荐
- sklearn 中的 Pipeline 机制
转载自:https://blog.csdn.net/lanchunhui/article/details/50521648 from sklearn.pipeline import Pipeline ...
- codeforces 1140E Palindrome-less Arrays
题目链接:http://codeforces.com/contest/1140/problem/E 题目大意: 如果一个数组的存在一个奇数长的回文就不好. 不是不好的数组是好的. 你可以把-1用1到k ...
- VisionPro相机操作类
在网站上看到这个,保存下来,以后用到了,再看一下.谢谢原创的分享! #region 获得相机信息方法 /// <summary> /// 公有静态方法,查找单个相机.例如“Basler” ...
- linq与lambda 常用查询语句写法对比
LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...
- webpack构建Vue项目引入jQ时发生“'$' is defined but never used”的处理
今天公司需要新建个数据后台,就按照查到的方法构建了Vue框架的项目,引入jQ.bootstrap时,按照在线方法配置,发现 main.js 里的引用jQ一直显示红标,没多想,在按照网上配置完后,npm ...
- 深入理解JavaScript函数参数
前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传进来的参数是什么数据类型,甚至可以不传参数. arguments javascri ...
- 在NodeJS中使用Redis缓存数据
Redis数据库采用极简的设计思想,最新版的源码包还不到2Mb.其在使用上也有别于一般的数据库. node_redis redis驱动程序多使用 node_redis 此模块可搭载官方的 hiredi ...
- oracle安装出错/runInstaller
http://blog.csdn.net/yabingshi_tech/article/details/48313955 http://www.cnblogs.com/lihaozy/archive/ ...
- 第十二次作业psp
psp 进度条 代码累积折线图 博文累积折线图 psp饼状图
- 做一名合格的程序员(learning of a previous team)
合格程序员的基本素质 作一个真正合格的程序员,或者说就是可以真正合格完成一些代码工作的程序员,应该具 有的素质. 1 团队精神和协作能力 把它作为基本素质,并不是不重要,恰恰相反,这是程序员应该具备 ...