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

概述

MyBatis中的一对多、多对一和多对多,主要就是resultMap中:

  • association:一个复杂的类型关联。许多结果将包成这种类型(一对多)
  • collection:复杂类型的集合(多对一)
    这2个属性的使用,而一对多和多对一都是相互的,只是站的角度不同。

实例:
这个实例只说这2个属性的使用方法。

  1. 首先是数据库,以员工管理系统的数据库为例。
  2. 实体类:
    这里只列要实现映射功能用到的类user、role、department。
  • department和user,是一对多关系,一个部门可以拥有多个员工。
  • role和user是多对多关系,一个角色可能有多个员工,而一个员工也可能有多个角色。而要实现多对多,在程序中则是拆分成两个一对多,详情见下面的实体类注释。
    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;

    //一对多,一个用户可能有多个角色。
    private List roles;

    //多对一,一个部门多个员工
    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对应一个department,使用association




    
        
        
        
        
        
        
        
        
            
        
            
            
        
    

    

2)UserMapper.xml:一对多,使用collection,一个user对应多个role,同时也是多对多中user这一方的一对多。如果要实现多对多,还需要完成一个role对应多个user,即完成两个一对多,写法同下。





    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
            
            
        
    

    



不管是一对多、多对一,还是多对多,只需要知道这2个属性哪个代表多哪个代表一,就可以很好的在实体的mapper文件中配置出来。

你可能感兴趣的:(MyBatis一对多,多对一,多对多)