(十)Mybatis多表联查xml当中应该如何写?

这篇文章主要讲述Mybatis多表联查xml当中的一些实现方式,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

一对一的场景

也就是对象里面套了一个对象,一般用于学生和班级,或者员工和部门的关系等等。
此处省略了get和set方法等等,主要看重点即可。
Employee类

public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	private Integer dId; //Department关联主键id
	private Department dept;
}

Department 类

public class Department {
	
	private Integer id;
	private String departmentName;

}

1、resultMap封装对象

column是数据库名称,property就是java属性名称
dept.id,也就是子对象的java属性名称

	<resultMap type="com.gzl.mybatis.bean.Employee" id="MyDifEmp">
		<id column="id" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="gender" property="gender"/>
		<result column="email" property="email"/>
		<result column="did" property="dept.id"/>
		<result column="dept_name" property="dept.departmentName"/>
	</resultMap>
	<select id="getEmpAndDept" resultMap="MyDifEmp">
		SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
		d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
		WHERE e.d_id=d.id AND e.id=#{id}
	</select>

2、resultMap配合association封装对象

	<resultMap type="com.gzl.mybatis.bean.Employee" id="MyDifEmp2">
		<id column="id" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="gender" property="gender"/>
		
		<!--  association可以指定联合的javaBean对象
		property="dept":指定哪个属性是联合的对象
		javaType:指定这个属性对象的类型[不能省略]
		-->
		<association property="dept" javaType="com.gzl.mybatis.bean.Department">
			<id column="did" property="id"/>
			<result column="dept_name" property="departmentName"/>
		</association>
	</resultMap>
	<select id="getEmpAndDept" resultMap="MyDifEmp2">
		SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
		d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
		WHERE e.d_id=d.id AND e.id=#{id}
	</select>

3、resultMap配合collection封装对象

property=“dept”:指定哪个属性是联合的对象
ofType:指定这个属性对象的类型[不能省略]

<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp3">
		<id column="id" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="gender" property="gender"/>
		
		<collection property="dept" ofType="com.atguigu.mybatis.bean.Department">
			<!-- 定义这个集合中元素的封装规则 -->
			<id column="did" property="id"/>
			<result column="dept_name" property="departmentName"/>
		</collection>
	</resultMap>
	<!--  public Employee getEmpAndDept(Integer id);-->
	<select id="getEmpAndDept" resultMap="MyDifEmp3">
		SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
		d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
		WHERE e.d_id=d.id AND e.id=#{id}
	</select>

一对多的场景

也就是对象里面套了一个List,一般用于根据班级查询学生等等。。
Department 类

public class Department {
	
	private Integer id;
	private String departmentName;
	private List<Employee> emps;

}

Employee 类

public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	
}

resultMap配合collection封装对象

	<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
	<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<!-- 
			collection定义关联集合类型的属性的封装规则 
			ofType:指定集合里面元素的类型
		-->
		<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
			<!-- 定义这个集合中元素的封装规则 -->
			<id column="eid" property="id"/>
			<result column="last_name" property="lastName"/>
			<result column="email" property="email"/>
			<result column="gender" property="gender"/>
		</collection>
	</resultMap>
	<!-- public Department getDeptByIdPlus(Integer id); -->
	<select id="getDeptByIdPlus" resultMap="MyDept">
		SELECT d.id did,d.dept_name dept_name,
				e.id eid,e.last_name last_name,e.email email,e.gender gender
		FROM tbl_dept d
		LEFT JOIN tbl_employee e
		ON d.id=e.d_id
		WHERE d.id=#{id}
	</select>

测试
这是数据库当中执行得出的结果,也就是一对多查出来的两条
在这里插入图片描述
collection会帮我们把两条封装到父对象当中。

在这里插入图片描述

总结

collection可以用来接单个对象嵌套,也可以接对象里面套list
association只可以接单个对象
平时我们返回结果集对象类型都是用resultType,当我们用自定义的时候需要用resultMap

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪 咖@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值