需求是要查询课程表、班级表、教师表,查询出所有课程名称以及教师名称(课程表中存放的是班级id和教师id)
主要运用的是两个左连接
select lesson.*,info_class.name AS className,sys_user.realname AS teacherName from lesson
LEFT JOIN info_class on lesson.class_id = info_class.id
LEFT JOIN sys_user on lesson.teacher_id = sys_user.id
where lesson.class_id = 8 and del =0
(课程表)
(班级表)
(教师表)
<resultMap id="BaseResultMapTeacher" type="com.yglh.entity.Lesson">
<id column="id" jdbcType="BIGINT" property="id" />
<id column="name" jdbcType="VARCHAR" property="name" />
<id column="teacher_id" property="teacherId"/>
<id column="class_id" property="classId"/>
<id column="week" property="week"/>
<id column="start_time" property="startTime"/>
<id column="end_time" property="endTime"/>
<id column="create_time" property="createTime"/>
<id column="edit_time" property="editTime"/>
<id column="teacherName" property="teacherName"/>
<id column="className" property="className"/>
</resultMap>
<!-- 模糊查询-->
<select id="findSm" parameterType="com.yglh.vo.LessonVo" resultMap="BaseResultMapTeacher">
SELECT lesson.*,info_class.`name` as className,sys_user.realname AS teacherName from lesson
LEFT JOIN info_class on lesson.class_id = info_class.id
LEFT JOIN sys_user on lesson.teacher_id = sys_user.id
<where>
<if test="teacherId !=null">
and lesson.teacher_id = #{teacherId}
</if>
<if test="classId !=null">
and lesson.class_id = #{classId}
</if>
<if test="week !=null">
and lesson.week = #{week}
</if>
<if test="startTime!=null">
and lesson.start_time <![CDATA[>=]]> #{startTime}
</if>
<if test="endTime!=null">
and lesson.end_time <![CDATA[<=]]> #{endTime}
</if>
AND del=0
</where>
order by id desc
</select>