MySQL mybatis 3中集合映射和关联映射的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12425384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between collection and association mapping in mybatis 3
提问by user533
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.
谢谢你。
回答by quux00
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).
我将假设您在项目和员工之间存在多对多关系,这就是您创建项目分配表的原因。这个项目分配表/对象可能只有两个字段/列:项目 id 到员工 id 的映射 - 一个经典的“桥表”(又名“连接”或“连接”表)。
When you map this model to an object graph, you have three options:
将此模型映射到对象图时,您有三个选项:
- A Project object can have a list of all employees assigned to it
- An Employee object can have a list of projects s/he is assigned to
- Create a Project Assignment object that has a mapping of each projects to its employee and each employee to his/her project.
- Project 对象可以拥有分配给它的所有员工的列表
- Employee 对象可以有一个他/她被分配到的项目列表
- 创建一个项目分配对象,该对象具有每个项目到其员工以及每个员工到他/她的项目的映射。
In your example you chose the last option.
在您的示例中,您选择了最后一个选项。
Association
协会
An association is a single mapping for a "has-one" relationship.
关联是“一对一”关系的单个映射。
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 成为对象图中的“主要”焦点,那么您可以将其与他/她的项目关联起来:
<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:
现在对逆进行建模 - 我们将 Project 作为主要焦点。一个项目与 Employee 有一个“有很多”关系,所以它会有一个列表或集合,所以我们使用一个“集合”映射:
<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 对象,您需要:
- A single Project Association object that maps all projects to employees and vice versa
- One Project Association object per project, mapping a project to its employees
- One Project Association object per employee, mapping an employee to his/her projects
- 将所有项目映射到员工的单个项目关联对象,反之亦然
- 每个项目一个项目关联对象,将项目映射到其员工
- 每个员工一个项目关联对象,将员工映射到他/她的项目
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 可以参与多个项目,那么使用 acollection
而不是association
我上面使用的来建立“多”关系。
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”association
和“has-many”collection
的示例,请参阅我创建的 MyBatis Koans:https: //github.com/midpeter444/mybatis-koans。Koans 10 和 11 证明了这一点。