mysql联合查询(手把手教学)

以教室对应多个老师对应多个学生为例

 <resultMap id="classesListVo" type="org.springblade.call.vo.CallVO">
        <id property="id" column="id" jdbcType="BIGINT"></id>
        <result property="classId" column="class_id" jdbcType="BIGINT"></result>
        <collection property="teachers" javaType="ArrayList" ofType="com.cgz.web.controller.tool">
            <id property="id" column="tt_id"  jdbcType="BIGINT"/>
            <result property="name" column="tt_name" jdbcType="VARCHAR"/>
             <result property="name" column="tt_code" jdbcType="VARCHAR"/>
        </collection>
        <collection property="studentList" javaType="ArrayList" ofType="package com.cgz.web.controller.tool.Student">
            <id property="id" column="ts_id"  jdbcType="BIGINT"/>
            <result property="name" column="ts_name" jdbcType="VARCHAR"/>
             <result property="code" column="ts_code" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

import java.util.List;

/**
 * @author cgz
 * @version 1.0
 * @date 2023/9/7 9:10
 */
@Data
public class Class {

    private Long id;


    private Long classId;
    
    private List<Student> studentList;

    private List<Teacher> teachers;
}

import lombok.Data;

/**
 * @author cgz
 * @version 1.0
 * @date 2023/9/7 9:10
 */
@Data
public class Student {

    private String name;

    private String code;
}
import lombok.Data;

/**
 * @author cgz
 * @version 1.0
 * @date 2023/9/7 9:09
 */
@Data
public class Teacher {

    private String name;

    private String code;
}


    <select id="getData" resultMap="classesListVo">
        select
       tc.id,
       tc.class_id,
       ts.name as ts_name,
       ts.code as ts_code,
       tt.name as tt_name,
       tt.code as tt_code
        from t_class tc
        left join t_student ts on ts.class_id=tc.class_id  and ts.is_deleted = 0
        left join t_teacher tt on tt.class_id=tc.class_id  and tt.is_deleted = 0

    </select>

	
	

	/**
	 *
	 *
	 * @return
	 */
	List<Class> getData();

你可能感兴趣的:(Mysql,性能优化,mysql,数据库)