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

MyBatis一对多,多对一,多对多MyBatis中的一对多和对多一,多对多主要就是resultMap中association–一个复杂的类型关联;许多结果将包成这种类型(一对多)collection–复杂类型的集合(多对一)这2个属性的使用,而一对多和多对一都是相互的,只是站的角度不同。实例:这个实例只说这2个属性的使用方法,具体的配置和运行结果在SSM环境搭建文章中写出。1.首先是数据库,沿用之前员

大家好,欢迎来到IT知识分享网。

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<Role> 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<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> 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<User> 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<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> 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<User> users;
	
	public Department() {
		super();
	}

	public Department(int department_id, String department_name,
			List<User> 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<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> 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

<?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="employee_management.mapper.UserDepartmentMapper">
	<resultMap type="employee_management.entity.User"
		id="userDepartmentList">
		<id property="user_id" column="user_id" javaType="java.lang.Integer" />
		<result property="user_name" column="user_name" javaType="java.lang.String" />
		<result property="user_gender" column="user_gender" javaType="java.lang.String" />
		<result property="user_email" column="user_email" javaType="java.lang.String" />
		<result property="user_phone" column="user_phone" javaType="java.lang.String" />
		<result property="user_address" column="user_address" javaType="java.lang.String" />
		<result property="user_birthday" column="user_birthday"
			javaType="java.util.Date" />
		<result property="department_id" column="department_id"
			javaType="java.lang.Integer" />
			<!-- property是user实体类中department类属性的名字
			1个user有1个department-->
		<association property="department"
			javaType="employee_management.entity.Department">
			<id property="department_id" column="department_id" javaType="java.lang.Integer" />
			<result property="department_name" column="department_name"
				javaType="java.lang.String" />
		</association>
	</resultMap>
	
	<select id="getAll" resultMap="userDepartmentList">
		select u.*,d.department_name from user u left join department d on u.department_id=d.department_id;
	</select>
</mapper>

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

<?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="employee_management.mapper.UserMapper">

	<!-- get user list only -->
	<resultMap type="employee_management.entity.User" id="userList">
		<id property="user_id" column="user_id" javaType="java.lang.Integer" />
		<result property="user_name" column="user_name" javaType="java.lang.String" />
		<result property="user_gender" column="user_gender" javaType="java.lang.String" />
		<result property="user_email" column="user_email" javaType="java.lang.String" />
		<result property="user_phone" column="user_phone" javaType="java.lang.String" />
		<result property="user_address" column="user_address" javaType="java.lang.String" />
		<result property="user_birthday" column="user_birthday"
			javaType="java.util.Date" />
		<result property="department_id" column="department_id"
			javaType="java.lang.Integer" />
	</resultMap>

	<!-- get user list with role -->
	<resultMap type="employee_management.entity.User" id="userRoleList"
		extends="userList">
		<!-- collection中不是javaType而是ofType,通过property也知道是list,即多个。
		extends属性,即在父resultMap的基础上再加上子resultMap的结果,在这里即再加上部门的信息。 -->
		<collection property="roles" ofType="employee_management.entity.Role">
			<id property="role_id" column="role_id" javaType="java.lang.Integer" />
			<result property="role_name" column="role_name" javaType="java.lang.String" />
		</collection>
	</resultMap>

	</resultMap>
	
<select id="getAll" resultMap="userList">
		select * from user
	</select>
<select id="getAllUserAndRole" resultMap="userRoleList">
		select u.*,r.* from
		user u left join user_role ur on u.user_id=ur.user_id
		left join role r
		on r.role_id=ur.role_id;
	</select>

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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23758.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信