MyBatis的一对多映射(九)_mybatis一对多映射(1)

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 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 getAllUser() {
return allUser;
}
public void setAllUser(List 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语句



<select id="getDeptByIdWithSelect" parameterType="int" resultMap="userDeptResultMapWithSelect">
	select * from user u where u.id=#{id}
</select>

#### 二.四 DeptMapper.xml 的sql语句



<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);
}


![有图片。](https://img-blog.csdnimg.cn/20190708165325499.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly95dWVqbC5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70)


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


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


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


DeptMapper.java 中接口:



public Dept getAllInfoByIdWithResult(int id);


DeptMapper.xml 中的sql语句:



select * from dept d,user u where d.id=u.deptId and d.id=#{id}

测试方法:



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


![有图片。](https://img-blog.csdnimg.cn/20190708165337732.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly95dWVqbC5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70)


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


DeptMapper.java 中的接口:



public Dept getAllInfoByIdWithSelect(int id);


UserMapper.java 中的接口:


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



public List findUserByDeptId(@Param(value=“deptId”) int deptId);


DeptMapper.xml 中的sql语句:



select * from dept where id=#{id}

UserMapper.xml 中的sql语句:



select * from user u where u.deptId=#{deptId}

测试方法:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值