Use la colección en RresultMap para hacer una consulta anidada

1. Use el formato de la siguiente manera:

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

Nota: <collection>la columna en la etiqueta: es el parámetro que se pasará a la instrucción de consulta de selección. Si se pasan varios parámetros, el formato es column= "{nombre de parámetro 1=campo de tabla 1, nombre de parámetro 2=campo de tabla 2}" ;

2. Ejemplos

(2) Clase de entidad

@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) mapeador.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>

Supongo que te gusta

Origin blog.csdn.net/y516369/article/details/127462458
Recomendado
Clasificación