使用RresultMap中collection做嵌套查询

怼烎@ 2024-04-01 09:33 137阅读 0赞

1.使用格式如下:

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

注意:<collection>标签中的column:是要传递给select查询语句的参数,如果传递多个参数,格式为column= “ {参数名1=表字段1,参数名2=表字段2}“ ;

2.实例

(2)实体类

  1. @Data
  2. public class SubjectVO implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. private String id;
  5. private String title;
  6. private Integer sort;
  7. private List<SubjectVO> children = new ArrayList<>();
  8. }

(2)mapper.xml

  1. <mapper namespace="com.atguigu.guli.service.edu.mapper.SubjectMapper">
  2. <resultMap id="nestedSubject" type="com.atguigu.guli.service.edu.entity.vo.SubjectVO">
  3. <id property="id" column="id"/>
  4. <result property="title" column="title"/>
  5. <result property="sort" column="sort"/>
  6. <collection column="id"
  7. property="children"
  8. ofType="com.atguigu.guli.service.edu.entity.vo.SubjectVO"
  9. select="selectNestedListByParentId"/>
  10. </resultMap>
  11. <select id="selectNestedListByParentId" resultMap="nestedSubject">
  12. select id, sort, title
  13. from edu_subject
  14. where parent_id = #{parentId}
  15. </select>
  16. </mapper>

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读