MyBatis一对多,多对一,多对多

MyBatis中的一对多和对多一,多对多
主要就是resultMap中
association(关联) – 一个复杂的类型关联;许多结果将包成这种类型(多对一)
嵌套结果映射 – 关联本身可以是一个 resultMap 元素,或者从别处引用一个
collection(集合) – 复杂类型的集合(一对多)
嵌套结果映射 – 集合本身可以是一个 resultMap 元素,或者从别处引用一个

更多详细信息,在mybatis官方文档就能找到,建议大家可以直接看官方文档
mybatis官方文档
什么是关联:
在mybatis中是用来处理"has a"关系,比如一个员工有一个部门,即一个员工关联一个部门,所以association能用来处理我们数据中所谓的一对一,多对一关系(一个部门有多个员工,但是对于员工来说,一个员工只能关联一个部门)。
什么是集合:
将嵌套映射的结果集合到一个list中,比如一个部门有多个员工,即1个部门对应多个员工,对于部门来说,一个部门有多个员工。
可以看到,在处理我们数据关系的时候,就是对这2个属性的使用,而一对多和多对一都是相互的,只是各自站的角度不同。
实例:
这个实例只说这2个属性的使用方法,具体的配置和运行结果在SSM环境搭建文章中写出。
1.首先是数据库,沿用之前员工管理系统的数据库设计点击这里,上面是之前写过的一篇数据库设计,包含sql语句。

2.实体类:
我这里只列要实现映射功能用到的类user,role,department。department和user,是1对多关系,1个部门拥有多个员工。
role和user是多对多关系,1个角色可能有多个员工,而1个员工也可能有多个角色。而要实现多对多,在程序中则是拆分成2个一对多,详情见下面的实体类注释。

1)User.java

public class User {
	private int user_id;
	private String user_name;
	private String user_gender;
	private String user_email;
	private String user_phone;
	private String user_address;
	private Date user_birthday;
	private int department_id;

	//一对多,1个用户可能有多个角色。
	private List roles;
	
	//一对一,1个用户属于一个部门
	private Department department;

	public User() {
		super();
	}

	public User(String user_name, String user_gender,
			String user_email, String user_phone, String user_address,
			Date user_birthday, int department_id) {
		super();
		this.user_name = user_name;
		this.user_gender = user_gender;
		this.user_email = user_email;
		this.user_phone = user_phone;
		this.user_address = user_address;
		this.user_birthday = user_birthday;
		this.department_id = department_id;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getUser_gender() {
		return user_gender;
	}

	public void setUser_gender(String user_gender) {
		this.user_gender = user_gender;
	}

	public String getUser_email() {
		return user_email;
	}

	public void setUser_email(String user_email) {
		this.user_email = user_email;
	}

	public String getUser_phone() {
		return user_phone;
	}

	public void setUser_phone(String user_phone) {
		this.user_phone = user_phone;
	}

	public String getUser_address() {
		return user_address;
	}

	public void setUser_address(String user_address) {
		this.user_address = user_address;
	}

	public Date getUser_birthday() {
		return user_birthday;
	}

	public void setUser_birthday(Date user_birthday) {
		this.user_birthday = user_birthday;
	}

	public int getDepartment_id() {
		return department_id;
	}

	public void setDepartment_id(int department_id) {
		this.department_id = department_id;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List getRoles() {
		return roles;
	}

	public void setRoles(List roles) {
		this.roles = roles;
	}

	@Override
	public String toString() {
		return "User [user_id=" + user_id + ", user_name=" + user_name
				+ ", user_gender=" + user_gender + ", user_email=" + user_email
				+ ", user_phone=" + user_phone + ", user_address="
				+ user_address + ", user_birthday=" + user_birthday
				+ ", department_id=" + department_id + ", roles=" + roles
				+ ", department=" + department + "]\n";
	}
}

2)Role.java

public class Role {
	private int role_id;
	private String role_name;
	
	//一对多,1个角色也可能被多个用户拥有,所以是list
	private List users;
	
	public Role() {
		
	}
	
	public Role(int role_id, String role_name) {
		super();
		this.role_id = role_id;
		this.role_name = role_name;
	}

	public int getRole_id() {
		return role_id;
	}

	public void setRole_id(int role_id) {
		this.role_id = role_id;
	}

	public String getRole_name() {
		return role_name;
	}

	public void setRole_name(String role_name) {
		this.role_name = role_name;
	}

	public List getUsers() {
		return users;
	}

	public void setUsers(List users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Role [role_id=" + role_id + ", role_name=" + role_name
				+ ", users=" + users + "]";
	}

}

3)Department.java

public class Department {
	private int department_id;
	private String department_name;
	
	//1对多,1个部门多个用户
	private List users;
	
	public Department() {
		super();
	}

	public Department(int department_id, String department_name,
			List users) {
		super();
		this.department_id = department_id;
		this.department_name = department_name;
		this.users = users;
	}

	public int getDepartment_id() {
		return department_id;
	}

	public void setDepartment_id(int department_id) {
		this.department_id = department_id;
	}

	public String getDepartment_name() {
		return department_name;
	}

	public void setDepartment_name(String department_name) {
		this.department_name = department_name;
	}

	public List getUsers() {
		return users;
	}

	public void setUsers(List users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Department [department_id=" + department_id
				+ ", department_name=" + department_name + ", users=" + users
				+ "]"+"\n";
	}
}

3.实体类的mapper文件
1)UserDepartmentMapper.xml,多对一,多个user对应1个department,对于每个user来说只关心自己的那个department,即每个user关联自己的department,使用association




	
		
		
		
		
		
		
		
		
			
		
			
			
		
	
	
	

2)UserMapper.xml,一对多,使用collection,1个user对应多个role,同时也是多对多中user这一方的一对多。如果要实现多对多,还需要完成1个role对应多个user,即完成2个1对多,写法同这里,就不在赘述。





	
	
		
		
		
		
		
		
		
		
	

	
	
		
		
			
			
		
	

	
	


结论:不管是一对多还是多对一,还是多对多,只需要知道数据是需要一对一关联还是多个结果映射到1个list,就可以很好的在实体的pojo,mapper文件中写出来。

你可能感兴趣的:(MyBatis)