In MyBatis, the method of using annotations for collection mapping is similar to the XML configuration method, and the mapping relationship needs to be defined through @Results
and @Result
annotations . The following is a specific example ( jump to the second point if you want to look directly at the key point ).
Explanation for these two annotations: Address -> Detailed Explanation of Common Annotations in MyBatis (Results, Result, ResultMap, ResultType, MapKey)
Suppose you have the following query statement:
SELECT g.id, g.name, u.id user_id, u.name user_name, u.age user_age
FROM group g
LEFT JOIN user u ON u.group_id = g.id
We need to map this query result into a list of Group objects, where each Group object contains a list of User objects, representing all user information under the group.
1. Define the entity class
Firstly, you need to define the Group and User entity classes, and add a List<User> type attribute to the Group class, which represents all user information under this group.
For example:
@Data
public class Group {
private Long id;
private String name;
private List<User> users;
}
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
2. Define the Mapper interface
Then you need to define a Mapper interface, and define a selectGroups() method in it to perform the above query operation. At the same time, you also need to use @Results
and @Result
annotations to define the mapping relationship.
Among them, @Results
annotations are used to define multiple mapping relationships, and @Result
annotations are used to define a single mapping relationship.
For example:
@Mapper
public interface GroupMapper {
@Select("SELECT g.id, g.name, u.id user_id, u.name user_name, u.age user_age " +
"FROM group g LEFT JOIN user u ON u.group_id = g.id")
@Results({
@Result(property = "id", column = "id", id = true),
@Result(property = "name", column = "name"),
@Result(property = "users", column = "id",
many = @Many(select = "com.example.demo.mapper.UserMapper.selectByGroupId"))
})
List<Group> selectGroups();
}
In the above example, we defined a selectGroups()
method called and used @Select
annotations to specify the SQL query statement. At the same time, annotations are used in this method @Results
to define multiple mapping relationships, including the mapping relationship between the id and name attributes of the Group object, and the mapping relationship between the users list attribute.
It should be noted that when using the annotation mapping collection type, the @Results
unique identifier of the result set mapping needs to be given in the annotation id
and @Result
set in the annotation id = true
so that MyBatis can correctly process the result set mapping. It means that it corresponds to the <id> tag.
For the mapping relationship of the users list attribute, we use
@Result
annotations to define a single mapping relationship, whereproperty
the attribute specifies the name of the list attribute,column
the attribute specifies which column to establish a mapping relationship between the attribute and the query result set, andmany
the attribute indicates that the attribute is related to another The table establishes a one-to-many relationship, andselect
specifies the Mapper interface and method of the SQL statement for querying user information through the attribute.
It should be noted that select
the attribute value here com.example.demo.mapper.UserMapper.selectByGroupId
means that selectByGroupId()
the method named in the UserMapper interface is called to obtain user information.
Therefore, it is also necessary to define a method named in the UserMapper interface selectByGroupId()
to query all user information under this group.
3. Define the UserMapper interface
Then you need to define the UserMapper interface, and define a selectByGroupId() method in it to query all user information belonging to the specified group. For example:
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE group_id = #{groupId}")
List<User> selectByGroupId(@Param("groupId") Long groupId);
}
In the above example, we defined a selectByGroupId()
method called and used @Select
annotations to specify the SQL query statement. At the same time, annotations are used in the parameter list @Param
to specify method parameter names and corresponding query conditions.
At this point, an annotation-based MyBatis collection mapping development process is complete. If you find it useful, remember to like + bookmark it. Don’t get lost next time~