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

MyBatis中的一对多和对多一,多对多 
主要就是resultMap中 
association – 一个复杂的类型关联;许多结果将包成这种类型(一对多) 
collection – 复杂类型的集合(多对一) 
这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,使用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类属性的名字
            多个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>

原文地址:https://www.cnblogs.com/satisfysmy/p/9086181.html

时间: 2024-11-10 01:25:36

MyBatis一对多,多对一,多对多的相关文章

mybatis 一对多和多对一关联查询

首先  数据库量表之间字段关系(没有主外键) studentmajor表的id字段对应student表里major字段 两个实体类 package com.model; import java.util.Date; public class Student { private Integer sno; private String sname; private String ssex; private Integer sclass; private StudentMajor studentmaj

MyBatis一对多和多对多xml配置

MyBatis一对多和多对多xml配置 <?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="com.ktcx.

Mybatis的多表(多对多)查询

Mybatis的多表(多对多)查询 示例:用户和角色 一个用户可以有多个角色 一个角色可以赋予多个用户 解决办法 建立两张表:用户表,角色表 让用户表和角色表具有多对多的关系.需要使用中间表,中间表包含各自的主键,在中间表中是外键. 建立两个实体类:用户实体类和角色实体类 让用户和角色的实体类能体现出来多对多的关系 各自包含对方一个集合引用 建立两个配置文件 用户的配置文件 角色的配置文件 实现配置 当我们查询用户时,可以同时得到用户所包含的角色信息. 当我们查询角色时,可以同时得到角色所赋予的

hibernate(四)一对多单向、多对一双向、自身双向关联

?经过前几篇文章的简略总结,主要认识了关于hibernate的基本认识,顺便认识了hibernate的简单的一个原理.具体参见 Hibernate (一)hibernate入门 Hibernate(二)hibernate原理简单实现对比JDBC hibernate(三)入门级--hibernate实例 从这篇文章开始就接触具体hibernate是如何来配置使用,更多的是一些细节的掌握.首先需要声明的是作者也是在诸多资料和工作学习中不断在更新自己关于知识的理解,难免出现一些在现阶段的主观认识和片面

mybatis一对多关联查询+pagehelper-&gt;分页错误(toSolve)

mybatis一对多关联查询+pagehelper->分页错误. 现象: 网上其他人遇到的类似问题:https://segmentfault.com/q/1010000009692585 解决: todo 疑惑: 之前有人提过类似的issue(https://github.com/pagehelper/Mybatis-PageHelper/issues/149)为什么被关闭了 原文地址:https://www.cnblogs.com/goingforward/p/8492448.html

mybatis 一对多和多对一

在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而导致一对多的"多"中也只有一条数据.id和result的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性.这帮助来改进整体表现,特别是缓存和嵌入结果映射.所以不同数据的id应该唯一区别,不然导致数据结果集只有一条数据. 一.表 二.实体 1.person [java] view plain copy package com.kerwin.mybatis.pojo;

MyBatis——一对多、多对一

遇见你之前心猿意马.可遇见你之后,猿跳走了,马飞奔而去,只剩心意,全然为你. 1.多对一处理 多对一: 多个学生,对应一个老师 对于学生这边而言,关联,多个学生,关联一个老师[多对一] 对于老师而言,集合,一个老师有很多学生[一对多] SQL: CREATE TABLE `teacher`( `id` int(10) Not null, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`) )ENGINE=INNODB DEFAULT CHARS

Mybatis一对多,多对一中主子表字段名一致产生的问题及解决方案

数据库两张表的字段名都和两个类的属性名称一样,这时在关联查询出现一个问题. 学生类: package com.pojo; public class Student { private int id; private String name; private int age; private ClassInfo classInfo; public Student() { } public Student(String name, int age) { super(); this.name = na

MyBatis 一对多,多对一关联查询的时候Mapper的顺序

要先写association,然后写collection:这是由DTD决定的: <resultMap ...> <association ...> </association> <collection ...> </collection > </resultMap>