One-to-many and to many one, many-to-many in MyBatis
It's mainly in resultMap
association– A complex type association; many results will be packaged into this type (many to one)
Nested result maps – The association itself can be a resultMap element, or reference a
Collection (collection)– Collection of complex types (one to many)
Nested result maps – The collection itself can be a resultMap element, or reference a
More detailed information can be found in the official mybatis document. It is recommended that you can read the official document directly
Mybatis official document
What is association:
In mybatis, it is used to deal with the "has a" relationship. For example, an employee has a department, that is, an employee is associated with a department, so the association can be used to process the so-called one-to-one, many-to-one relationship in our data (a department has multiple employees, but for employees, an employee can only associate with one department).
What is a collection:
Collect the results of the nested mapping into a list. For example, a department has multiple employees, that is, one department corresponds to multiple employees. For a department, a department has multiple employees.
It can be seen that when processing our data relationship, we use these two attributes, and one-to-many and many-to-one are mutual, but the angles of each station are different.
Example:
This example only talks about the usage methods of these two attributes. The specific configuration and operation results areSSM environment constructionWritten in the article.
1. First of all, the database is used to follow the database design of the previous employee management system.Click here, The above is a database design I wrote before, which contains SQL statements.
2. Entity class:
Here I only list the user, role, and department classes used to implement the mapping function. Department and user are 1-to-many relationships, and one department has multiple employees.
role and user are many-to-many relationships. One role may have multiple employees, and one employee may also have multiple roles. To achieve many-to-many, in the program, it is split into 2 one-to-many. See the entity class comment below for details.
1)
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;
//One to many, one user may have multiple roles.
private List<Role> roles;
//One to one, 1 user belongs to one department
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) {
= department;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
= roles;
}
@Override
public String toString() {
return "User [user_, 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_, roles=" + roles
+ ", department=" + department + "]\n";
}
}
2)
public class Role {
private int role_id;
private String role_name;
//One to many, one role may also be owned by multiple users, so it is a 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) {
= users;
}
@Override
public String toString() {
return "Role [role_, role_name=" + role_name
+ ", users=" + users + "]";
}
}
3)
public class Department {
private int department_id;
private String department_name;
//1 to many users in 1 department
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;
= 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) {
= users;
}
@Override
public String toString() {
return "Department [department_, department_name=" + department_name + ", users=" + users
+ "]"+"\n";
}
}
3. Entity class mapper file
1) Many to one, multiple users correspond to 1 department. For each user, only care about his own department, that is, each user associates his own department, and uses association
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/">
<mapper namespace="employee_management.">
<resultMap type="employee_management."
>
<id property="user_id" column="user_id" javaType="" />
<result property="user_name" column="user_name" javaType="" />
<result property="user_gender" column="user_gender" javaType="" />
<result property="user_email" column="user_email" javaType="" />
<result property="user_phone" column="user_phone" javaType="" />
<result property="user_address" column="user_address" javaType="" />
<result property="user_birthday" column="user_birthday"
javaType="" />
<result property="department_id" column="department_id"
javaType="" />
<!-- property is the name of the department class attribute in the user entity class
1 user has 1 department-->
<association property="department"
javaType="employee_management.">
<id property="department_id" column="department_id" javaType="" />
<result property="department_name" column="department_name"
javaType="" />
</association>
</resultMap>
<select resultMap="userDepartmentList">
select u.*,d.department_name from user u left join department d on u.department_id=d.department_id;
</select>
</mapper>
2) One-to-many, use collection, 1 user corresponds to multiple roles, and it is also a one-to-many side of the user in many-to-many. If you want to implement many-to-many, you still need to complete 1 role corresponding to multiple users, that is, complete 2 1-to-many. The writing method is the same as here, so I will not go into details.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/">
<mapper namespace="employee_management.">
<!-- get user list only -->
<resultMap type="employee_management." >
<id property="user_id" column="user_id" javaType="" />
<result property="user_name" column="user_name" javaType="" />
<result property="user_gender" column="user_gender" javaType="" />
<result property="user_email" column="user_email" javaType="" />
<result property="user_phone" column="user_phone" javaType="" />
<result property="user_address" column="user_address" javaType="" />
<result property="user_birthday" column="user_birthday"
javaType="" />
<result property="department_id" column="department_id"
javaType="" />
</resultMap>
<!-- get user list with role -->
<resultMap type="employee_management."
extends="userList">
<!-- The collection is not javaType but ofType. It is also known through property that it is a list, that is, multiple.
The extends attribute, that is, the result of the child resultMap is added to the parent resultMap, and here is the department information. -->
<collection property="roles" ofType="employee_management.">
<id property="role_id" column="role_id" javaType="" />
<result property="role_name" column="role_name" javaType="" />
</collection>
</resultMap>
</resultMap>
<select resultMap="userList">
select * from user
</select>
<select 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>
Conclusion: Whether it is one-to-many, many-to-one, or many-to-many, you only need to know whether the data needs to be one-to-one correlation or multiple results are mapped to 1 list, and you can write it well in the pojo and mapper file of the entity.