mybatis 3中的集合和关联映射之间的区别

时间:2021-11-25 13:27:52

I am doing mysql queries execution from mybatis3. I am new to this. What is the difference between collection and association mapping in mybatis 3?

我正在从mybatis3执行mysql查询执行。我是新来的。 mybatis 3中的集合和关联映射有什么区别?

Specific example below.

具体例子如下。

SELECT e.empid AS empid,e.empname AS empname,
       e.empsalary AS empsalary,p.proname AS proname,p.proid AS proid 
FROM projects p,employees e,projectassigns pa 
WHERE pa.empid=e.empid AND pa.proid=p.proid; 

I need all the details of employee and project.

我需要员工和项目的所有细节。

I have given the result map as follows.

我给出了如下结果图。

<resultMap id="resultProjects" type="com.pratap.model.ProjAssigns"> 
  <association property="employee" javaType="com.pratap.model.Employee"
               resultMap="resultEmployees" />
  <association property="project" javaType="com.pratap.model.Project"     
               resultMap="resultProjects" />  
</resultMap>

Can anybody explain the difference taking my example or your own example?

任何人都可以用我的例子或你自己的例子解释这个区别吗?

I am confused with this..

我对此很困惑..

Thank you.

谢谢。

1 个解决方案

#1


18  

I am going to assume that you have a many to many relationship between Projects and Employees, which is why you created a Project Assignment table. This Project Assignment table / object may only have two fields/columns: a mapping of project id to employee id - a classic "bridge table" (aka "join" or "junction" table).

我将假设您在Projects和Employees之间存在多对多的关系,这就是您创建Project Assignment表的原因。此项目分配表/对象可能只有两个字段/列:项目ID到员工ID的映射 - 经典的“桥表”(又名“连接”或“连接”表)。

When you map this model to an object graph, you have three options:

将此模型映射到对象图时,您有三个选项:

  1. A Project object can have a list of all employees assigned to it
  2. Project对象可以包含分配给它的所有员工的列表
  3. An Employee object can have a list of projects s/he is assigned to
  4. Employee对象可以包含他/她被分配到的项目列表
  5. Create a Project Assignment object that has a mapping of each projects to its employee and each employee to his/her project.
  6. 创建一个Project Assignment对象,该对象具有每个项目与其员工和每个员工到他/她的项目的映射。

In your example you chose the last option.

在您的示例中,您选择了最后一个选项。


Association

An association is a single mapping for a "has-one" relationship.

关联是“has-one”关系的单一映射。

Suppose an Employee can only be assigned to one Project at a time. Some models call this a "has-one" or "belongs to" relationship. If you want to make Employee your "primary" focus in the object graph, then you would map it with an association to his/her Project:

假设一次只能将一个Employee分配给一个Project。有些模型称之为“有一个”或“属于”关系。如果您想让Employee成为对象图中的“主要”焦点,那么您可以使用与他/她的Project的关联来映射它:

<resultMap id="employeeResultMap" type="Employee">
  <constructor>
    <idArg column="employee_id" javaType="_integer"/>
  </constructor>
  <result property="firstName"  column="first_name"/>
  <result property="lastName" column="last_name"/>
  <!-- etc. for other simple properties of Employee -->

  <!-- Project is a "complex property" of Employee, so we use an -->
  <!-- association to grab all of the Projects properties also -->
  <association property="assignedProject" resultMap="projectResultMap"/>
</resultMap>

In this case your objects would look like this:

在这种情况下,您的对象将如下所示:

public Employee {
  int id;
  String firstName;
  String lastName
  Project assignedProject;
}

public Project {
  int id;
  String name;
  String abc;
}


Collection

An collection is a "list" or "set" of associations.

集合是关联的“列表”或“集合”。

Now model the inverse - we make Project the primary focus. A Project has a "has-many" relationship with Employee, so it will have a list or collection of those, so we use a "collection" mapping:

现在模拟逆 - 我们使项目成为主要焦点。项目与Employee有“has-many”关系,因此它将有一个列表或集合,所以我们使用“集合”映射:

<resultMap id="projectResultMap" type="Project">
  <constructor>
    <idArg column="project_id" javaType="_integer"/>
    <arg column="name" javaType="String"/>
  </constructor>
  <result property="abc" column="abc"/>

  <!-- This tells mybatis that there can be multiple Employees -->
  <!-- to look up and get their properties -->
  <collection property="employees" ofType="Employee">
    <constructor>
      <idArg column="employee_id" javaType="_integer"/>
    </constructor>
    <result property="firstName"  column="first_name"/>
    <result property="lastName" column="last_name"/>
  </collection>
</resultMap>

Now your objects would look like this:

现在你的对象看起来像这样:

public Employee {
  int id;
  String firstName;
  String lastName
}

public Project {
  int id;
  String name;
  String abc;
  List<Employee> employees;
}


Project Association

To have a Project Association object, you would either need:

要拥有Project Association对象,您需要:

  1. A single Project Association object that maps all projects to employees and vice versa
  2. 单个Project Association对象,将所有项目映射到员工,反之亦然
  3. One Project Association object per project, mapping a project to its employees
  4. 每个项目一个项目关联对象,将项目映射到其员工
  5. One Project Association object per employee, mapping an employee to his/her projects
  6. 每个员工一个项目关联对象,将员工映射到他/她的项目

The first option is rather complex and messy - you would be trying to do relational mapping with object graphs (hash tables most likely).

第一个选项相当复杂和混乱 - 您将尝试使用对象图(最有可能的哈希表)进行关系映射。

I would choose to make one of the entities (Project or Employee) the primary focus and then model it as I showed above. The one case I didn't cover is if Employee is your primary focus and an Employee can be on multiple projects, then make that a "has-many" relationship using a collection rather than the association I used above.

我会选择将其中一个实体(项目或员工)作为主要焦点,然后按照上面的说明对其进行建模。我没有涉及的一个案例是,如果Employee是您的主要关注点,而Employee可以在多个项目上,那么使用集合而不是我上面使用的关联使其成为“有很多”关系。

Final Note: if it would help to see examples of using a "has-one" association and a "has-many" collection, see the MyBatis Koans I created: https://github.com/midpeter444/mybatis-koans. Koans 10 and 11 demonstrate this.

最后注意:如果有助于查看使用“has-one”关联和“has-many”集合的示例,请参阅我创建的MyBatis Koans:https://github.com/midpeter444/mybatis-koans。 Koans 10和11证明了这一点。

#1


18  

I am going to assume that you have a many to many relationship between Projects and Employees, which is why you created a Project Assignment table. This Project Assignment table / object may only have two fields/columns: a mapping of project id to employee id - a classic "bridge table" (aka "join" or "junction" table).

我将假设您在Projects和Employees之间存在多对多的关系,这就是您创建Project Assignment表的原因。此项目分配表/对象可能只有两个字段/列:项目ID到员工ID的映射 - 经典的“桥表”(又名“连接”或“连接”表)。

When you map this model to an object graph, you have three options:

将此模型映射到对象图时,您有三个选项:

  1. A Project object can have a list of all employees assigned to it
  2. Project对象可以包含分配给它的所有员工的列表
  3. An Employee object can have a list of projects s/he is assigned to
  4. Employee对象可以包含他/她被分配到的项目列表
  5. Create a Project Assignment object that has a mapping of each projects to its employee and each employee to his/her project.
  6. 创建一个Project Assignment对象,该对象具有每个项目与其员工和每个员工到他/她的项目的映射。

In your example you chose the last option.

在您的示例中,您选择了最后一个选项。


Association

An association is a single mapping for a "has-one" relationship.

关联是“has-one”关系的单一映射。

Suppose an Employee can only be assigned to one Project at a time. Some models call this a "has-one" or "belongs to" relationship. If you want to make Employee your "primary" focus in the object graph, then you would map it with an association to his/her Project:

假设一次只能将一个Employee分配给一个Project。有些模型称之为“有一个”或“属于”关系。如果您想让Employee成为对象图中的“主要”焦点,那么您可以使用与他/她的Project的关联来映射它:

<resultMap id="employeeResultMap" type="Employee">
  <constructor>
    <idArg column="employee_id" javaType="_integer"/>
  </constructor>
  <result property="firstName"  column="first_name"/>
  <result property="lastName" column="last_name"/>
  <!-- etc. for other simple properties of Employee -->

  <!-- Project is a "complex property" of Employee, so we use an -->
  <!-- association to grab all of the Projects properties also -->
  <association property="assignedProject" resultMap="projectResultMap"/>
</resultMap>

In this case your objects would look like this:

在这种情况下,您的对象将如下所示:

public Employee {
  int id;
  String firstName;
  String lastName
  Project assignedProject;
}

public Project {
  int id;
  String name;
  String abc;
}


Collection

An collection is a "list" or "set" of associations.

集合是关联的“列表”或“集合”。

Now model the inverse - we make Project the primary focus. A Project has a "has-many" relationship with Employee, so it will have a list or collection of those, so we use a "collection" mapping:

现在模拟逆 - 我们使项目成为主要焦点。项目与Employee有“has-many”关系,因此它将有一个列表或集合,所以我们使用“集合”映射:

<resultMap id="projectResultMap" type="Project">
  <constructor>
    <idArg column="project_id" javaType="_integer"/>
    <arg column="name" javaType="String"/>
  </constructor>
  <result property="abc" column="abc"/>

  <!-- This tells mybatis that there can be multiple Employees -->
  <!-- to look up and get their properties -->
  <collection property="employees" ofType="Employee">
    <constructor>
      <idArg column="employee_id" javaType="_integer"/>
    </constructor>
    <result property="firstName"  column="first_name"/>
    <result property="lastName" column="last_name"/>
  </collection>
</resultMap>

Now your objects would look like this:

现在你的对象看起来像这样:

public Employee {
  int id;
  String firstName;
  String lastName
}

public Project {
  int id;
  String name;
  String abc;
  List<Employee> employees;
}


Project Association

To have a Project Association object, you would either need:

要拥有Project Association对象,您需要:

  1. A single Project Association object that maps all projects to employees and vice versa
  2. 单个Project Association对象,将所有项目映射到员工,反之亦然
  3. One Project Association object per project, mapping a project to its employees
  4. 每个项目一个项目关联对象,将项目映射到其员工
  5. One Project Association object per employee, mapping an employee to his/her projects
  6. 每个员工一个项目关联对象,将员工映射到他/她的项目

The first option is rather complex and messy - you would be trying to do relational mapping with object graphs (hash tables most likely).

第一个选项相当复杂和混乱 - 您将尝试使用对象图(最有可能的哈希表)进行关系映射。

I would choose to make one of the entities (Project or Employee) the primary focus and then model it as I showed above. The one case I didn't cover is if Employee is your primary focus and an Employee can be on multiple projects, then make that a "has-many" relationship using a collection rather than the association I used above.

我会选择将其中一个实体(项目或员工)作为主要焦点,然后按照上面的说明对其进行建模。我没有涉及的一个案例是,如果Employee是您的主要关注点,而Employee可以在多个项目上,那么使用集合而不是我上面使用的关联使其成为“有很多”关系。

Final Note: if it would help to see examples of using a "has-one" association and a "has-many" collection, see the MyBatis Koans I created: https://github.com/midpeter444/mybatis-koans. Koans 10 and 11 demonstrate this.

最后注意:如果有助于查看使用“has-one”关联和“has-many”集合的示例,请参阅我创建的MyBatis Koans:https://github.com/midpeter444/mybatis-koans。 Koans 10和11证明了这一点。