RresultMap のコレクションを使用してネストされたクエリを実行する

1. 次の形式を使用します。

<collection column="传递给嵌套查询语句的字段参数"
			property="pojo对象中的集合属性" 
			ofType="集合属性中的pojo对象"
			select="嵌套的查询语句" > 
 </collection>

注:<collection>ラベルの列: は、select クエリ ステートメントに渡されるパラメーターです。複数のパラメーターが渡される場合、形式は column= "{パラメーター名 1=テーブル フィールド 1, パラメーター名 2=テーブル フィールド 2}" です。 ;

2. 例

(2)エンティティクラス

@Data
public class SubjectVO implements Serializable {
    
    
    private static final long serialVersionUID = 1L;
    private String id;
    private String title;
    private Integer sort;
    private List<SubjectVO> children = new ArrayList<>();
}

(2)mapper.xml

<mapper namespace="com.atguigu.guli.service.edu.mapper.SubjectMapper">
    <resultMap id="nestedSubject" type="com.atguigu.guli.service.edu.entity.vo.SubjectVO">
        <id property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="sort" column="sort"/>
        <collection column="id"
					property="children"   
					ofType="com.atguigu.guli.service.edu.entity.vo.SubjectVO"     
                    select="selectNestedListByParentId"/>
    </resultMap>

    <select id="selectNestedListByParentId" resultMap="nestedSubject">
        select id, sort, title
        from edu_subject
        where parent_id = #{parentId}
    </select>
</mapper>

おすすめ

転載: blog.csdn.net/y516369/article/details/127462458