SoFunction
Updated on 2025-05-09

One-to-one, one-to-many <association> configuration use in mybatis

<association>It is a mapping element in MyBatis that handles one-to-one or one-to-many relationships, which is used to map nested data in query results to properties of Java objects. It is often used to represent the association between objects, especially when complex database query results need to be mapped to nested objects.

1. Function and Use

  • One-to-one relationship: When an object contains an associated object (for example, aUserOnly correspond to oneAddress), can be used<association>Map one-to-one relationships.
  • One-to-many relationship: When an object contains multiple associated objects (for example, oneOrgCorresponding to multipleUser), can be used<association>Map one-to-many relationships.
  • Multi-level nested mapping: When a query involves multiple associated objects,<association>It can help automatically populate query results into Java objects and avoid manually writing complex mapping code.

2. Basic usage of <association>

  • property: The attribute name in a Java object used to store the associated object.
  • resultMap:Specify oneresultMap, indicating how to map fields in the query result to the associated object.
  • columnPrefix: Prefix the database column to avoid column name conflicts.

3. Example description: One-to-one and one-to-many mapping

Suppose there are two entities:UserandAddress,as well asOrgand multipleUser, we demonstrate how to use it separately<association>Perform one-to-one and one-to-many mapping.

4. Example 1: One-to-one association mapping

Data table structure

Suppose there are two tables in the database:usersandaddress

usersTable: Store user information

user_id user_name
1 Alice
2 Bob

addressTable: Store address information

address_id user_id street city
1 1 123 Main NYC
2 2 456 Elm LA

1.1 Defining Java classes

// 
public class User {
    private int id;
    private String name;
    private Address address;  // One-to-one association
    // getters and setters
}

// 
public class Address {
    private int id;
    private String street;
    private String city;

    // getters and setters
}

1.2 Create resultMap

<!-- Address resultMap -->
<resultMap  type="">
    <result property="id" column="address_id"/>
    <result property="street" column="street"/>
    <result property="city" column="city"/>
</resultMap>

<!-- User resultMap -->
<resultMap  type="">
    <id property="id" column="user_id"/>
    <result property="name" column="user_name"/>
    <association property="address" resultMap="addressResultMap"/>
</resultMap>

1.3 SQL Query

<select  resultMap="userResultMap">
    SELECT u.user_id, u.user_name, a.address_id, , 
    FROM users u
    LEFT JOIN address a ON u.user_id = a.user_id
    WHERE u.user_id = #{userId}
</select>

1.4 Result Mapping

If queryuser_id=1, return the following data:

user_id user_name address_id street city
1 Alice 1 123 Main NYC

The results will be mapped to:

User user = new User();
(1);
("Alice");

Address address = new Address();
(1);
("123 Main");
("NYC");

(address);

5. Example 2: One-to-many associative mapping

Data table structure

Suppose there are two tables:organdusers

  • orgTable: Store organizational information

    org_id org_name
    101 Finance
    102 HR
  • usersTable: Store user information

    user_id user_name org_id
    1 Alice 101
    2 Bob 101
    3 Carol 102

2.1 Defining Java classes

// 
public class User {
    private int userId;
    private String userName;

    // getters and setters
}

// 
public class Org {
    private int orgId;
    private String orgName;
    private List&lt;User&gt; users;  // One-to-many association
    // getters and setters
}

2.2 Create resultMap

<!-- User resultMap -->
<resultMap  type="">
    <result property="userId" column="user_id"/>
    <result property="userName" column="user_name"/>
</resultMap>

<!-- Org resultMap -->
<resultMap  type="">
    <result property="orgId" column="org_id"/>
    <result property="orgName" column="org_name"/>
    <association property="users" resultMap="userResultMap"/>
</resultMap>

2.3 SQL Query

<select  resultMap="orgResultMap">
    SELECT o.org_id, o.org_name, u.user_id, u.user_name
    FROM org o
    LEFT JOIN users u ON o.org_id = u.org_id
</select>

2.4 Result Mapping

The query returns the result:

org_id org_name user_id user_name
101 Finance 1 Alice
101 Finance 2 Bob
102 HR 3 Carol

Will map to:

List&lt;Org&gt; orgs = new ArrayList&lt;&gt;();

// org 1
Org org1 = new Org();
(101);
("Finance");

User user1 = new User();
(1);
("Alice");

User user2 = new User();
(2);
("Bob");

((user1, user2));

// org 2
Org org2 = new Org();
(102);
("HR");

User user3 = new User();
(3);
("Carol");

((user3));

// Add to list(org1);
(org2);

6. Summary

  • <association>Purpose:

    • One-to-one relationship:pass<association>Properties that map one object to another.
    • One-to-many relationship:pass<association>Map multiple objects to a collection property.
    • Nested result maps: Supports multi-level nested queries, mapping complex query results into multi-layer nested Java objects.
  • Common properties:

    • property: Specify the Java class attribute to populate.
    • resultMap: Point to the map used to process the associated objectresultMap
    • columnPrefix: Prefix the database column name to avoid column name conflicts.
  • Query result map:<association>The nested objects will be automatically populated into Java objects based on the results returned by the query, simplifying the processing of complex query results.

Through reasonable use<association>, you can easily handle one-to-one and one-to-many object relationships, avoid manually writing complex result mapping code, and improve the maintainability and readability of the code.

This is the article about the one-to-one and one-to-many configuration and use of mybatis. This is all about this article. For more related mybatis one-to-one and one-to-many configuration content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!