在MyBatis中使用Result注解進行多表關聯查詢的配置,可以通過以下步驟實現:
<select id="selectUserAndRole" resultType="User">
SELECT u.id, u.username, r.role_name
FROM user u
JOIN user_role ur ON u.id = ur.user_id
JOIN role r ON ur.role_id = r.id
</select>
public class User {
private Long id;
private String username;
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "role.roleName", column = "role_name")
})
private Role role;
// getters and setters
}
public interface UserMapper {
@Select("selectUserAndRole")
@ResultMap("UserResultMap")
User selectUserAndRole();
}
<resultMap id="UserResultMap" type="User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<association property="role" javaType="Role">
<result property="roleName" column="role_name"/>
</association>
</resultMap>
通過以上步驟,就可以使用Result注解進行多表關聯查詢的配置,并實現多表關聯查詢的功能。