MyBatis的一对多映射(九)

年轻的时候,遇见了一个人,便以为余生再没有江湖,后来,才懂,她才是江湖的起源。

上一章简单介绍了MyBatis的一对一映射(八),如果没有看过,请观看上一章

一. MyBatis的一对多关联映射

数据库中最常见的就是一对多的关联映射,业务开发中也很常见这一对多。 可以与Hibernate的一对多进行比较性学习: Hibernate的一对多映射的单向关联和双向关联(九)

还是以常见的部门Dept 与用户 User 进行关联。 一个部门下有多个用户,但是一个用户只属于一个部门。

部门图片。

员工图片。

其中,员工表里面的deptId 是关联的部门表里面的id, 看似是外键,但并没有设置外键,只是值一样而已。

员工类 User.java

package com.yjl.pojo;

/**
 @author:yuejl
 @date: 2019年6月15日 上午11:11:02
 @Description Mybatis 使用的基本类 User
*/
public class User {
	/**
	 * @param id id编号,自增
	 * @param name 姓名
	 * @param age 年龄
	 * @param sex 性别
	 * @param description 描述
	 */
	private Integer id;
	private String name;
	private Integer age;
	private String sex;
	private String description;
	//引入部门的对象。
	private Dept dept;
	public User(){
		
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", description=" + description
				+ "]";
	}
}

部门类 Dept.java

package com.yjl.pojo;

import java.util.List;

/**
 @author: yuejl
 @date: 2019年7月8日 上午10:15:08
 @Description 数据库中一的一方 部门实体
*/
public class Dept {
	/**
	 * @param id 部门的编号
	 * @param name 部门的名称
	 * @param description 部门的描述
	 */
	private Integer id;
	private String name;
	private String description;
	// 集合,用的是 List, 而不是Set
	private List<User> allUser;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public List<User> getAllUser() {
		return allUser;
	}
	public void setAllUser(List<User> allUser) {
		this.allUser = allUser;
	}
	@Override
	public String toString() {
		return "Dept [id=" + id + ", name=" + name + ", description=" + description + "]";
	}
}

二. 员工到部门的一对一 关联查询

可以先复习一下,员工到部门的一对一查询, 一个员工只属于一个部门。 传入的是员工的编号,查询出来的有部门的相应信息。 一对一采用的是嵌套查询的方式。

二.一 UserMapper.java 的接口

UserMapper.java 中的接口:

public User getDeptByIdWithSelect(int id);

二.二 DeptMapper.java 的接口

DeptMapper.java 中的接口:

public Dept getById(int id);

二.三 UserMapper.xml 的sql语句

<resultMap type="user" id="userDeptResultMapWithSelect">
		<!-- 普通的员工属性查询 -->
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sex" column="sex"/>
		<result property="age" column="age"/>
		<result property="description" column="description"/>
		<!-- 一对一的部门查询 ,传入参数是 deptId-->
		<association property="dept" javaType="dept" 
		column="deptId" select="com.yjl.mapper.DeptMapper.getById"></association>
	</resultMap>
	
	<select id="getDeptByIdWithSelect" parameterType="int" resultMap="userDeptResultMapWithSelect">
		select * from user u where u.id=#{id}
	</select>

二.四 DeptMapper.xml 的sql语句

<resultMap type="dept" id="deptResultMap">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="description" column="description"/>
	</resultMap>

<!-- 嵌套查询 -->
	<select id="getById" parameterType="int" resultMap="deptResultMap">
		select * from dept where id=#{id}
	</select>

二.五 测试方法

@Test
	public void getDeptByIdWithSelectTest(){
		SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		User user=userMapper.getDeptByIdWithSelect(1);
		System.out.println(user);
		Dept dept=user.getDept();
		System.out.println(dept);
	}

有图片。

三. 部门到员工的一对多关联映射

一对多关联映射,也有两种方式, 一种是一对多的嵌套结果, 一种是一对多的嵌套Select查询, 相比较一对一来说, 一对一查询出来的结果最多只有一个值,而一对多却可以查询出一个集合。 另外,一对一 用的是 javaType, 而一对多用的是 ofType. 这一点非常重要。

三.一 一对多的嵌套结果方式

DeptMapper.java 中接口:

public Dept getAllInfoByIdWithResult(int id);

DeptMapper.xml 中的sql语句:

<resultMap type="dept" id="deptCollectionResultMapWithResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="description" column="description"/>
		<!-- 用的是ofType的类型。 -->
		<collection property="allUser" ofType="user">
			<id property="id" column="id"/>
			<result property="name" column="name"/>
			<result property="sex" column="sex"/>
			<result property="age" column="age"/>
			<result property="description" column="description"/>
		</collection>
	</resultMap>
<select id="getAllInfoByIdWithResult" parameterType="int" resultMap="deptCollectionResultMapWithResult">
		select * from dept d,user u where d.id=u.deptId and d.id=#{id}
	</select>

测试方法:

@Test
	public void getAllInfoByIdWithResultTest(){
		SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
		DeptMapper deptMapper=sqlSession.getMapper(DeptMapper.class);
		Dept dept=deptMapper.getAllInfoByIdWithResult(1);
		System.out.println(dept);
		List<User> allUser=dept.getAllUser();
		allUser.forEach(n ->System.out.println(n));
	}

有图片。

三.二 一对多的嵌套Select 查询

DeptMapper.java 中的接口:

public Dept getAllInfoByIdWithSelect(int id);

UserMapper.java 中的接口:

传入参数,用 @Param 注解的形式。

public List<User> findUserByDeptId(@Param(value="deptId") int deptId);

DeptMapper.xml 中的sql语句:

<resultMap type="dept" id="deptCollectionResultMapWithSelect">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="description" column="description"/>
		<!-- 用的是ofType的类型。 -->
		<collection property="allUser" ofType="user" column="id"
		select="com.yjl.mapper.UserMapper.findUserByDeptId"></collection>
</resultMap>

<select id="getAllInfoByIdWithSelect" parameterType="int" resultMap="deptCollectionResultMapWithSelect">
		select * from dept where id=#{id}
</select>

UserMapper.xml 中的sql语句:

<resultMap type="user" id="userResultMap">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sex" column="sex"/>
		<result property="age" column="age"/>
		<result property="description" column="description"/>
</resultMap>
<select id="findUserByDeptId" parameterType="int" resultMap="userResultMap">
		select * from user u where u.deptId=#{deptId}
</select>

测试方法:

@Test
	public void getAllInfoByIdWithSelectTest(){
		SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
		DeptMapper deptMapper=sqlSession.getMapper(DeptMapper.class);
		Dept dept=deptMapper.getAllInfoByIdWithSelect(1);
		System.out.println(dept);
		List<User> allUser=dept.getAllUser();
		allUser.forEach(n ->System.out.println(n));
	}

有图片

谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

两个蝴蝶飞

你的鼓励,是老蝴蝶更努力写作的

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

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

打赏作者

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

抵扣说明:

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

余额充值