多表查询
查询多张表,每张表取一个或多个字段,返回相关数据。先根据查询的字段创建vo类,再编写sql语句,使用mybatis查询数据库数据
@Data public class CoursePublishVo { private String id; private String title; private String cover; private Integer lessonNum; private String subjectLevelOne; private String subjectLevelTwo; private String teacherName; private String price;//只用于显示 }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.swimming.serviceedu.mapper.EduCourseMapper"> <select id="getCoursePublishInfo" resultType="com.swimming.serviceedu.entity.vo.CoursePublishVo"> SELECT ec.id, ec.title, ec.price, ec.cover, ec.lesson_num as lessonNum, et.`name` as teacherName, es1.title as subjectLevelOne, es2.title as subjectLevelTwo from edu_course ec LEFT JOIN edu_teacher et on ec.teacher_id = et.id LEFT JOIN edu_subject es1 on ec.subject_parent_id = es1.id LEFT JOIN edu_subject es2 on ec.subject_id = es2.id where ec.id = #{courseId} </select> </mapper>