涉及到的数据库表如下图:
SQL语句:
查询课程和课程对应的老师信息:
select cname,ccredit,ctype,tname,tsex,tfaculty
from course_selection.course
LEFT JOIN course_selection.teacher ON course.course_tid = teacher.tid
可以使用List<Map<String, Object>>
接收。
String是key,存放我们需要查询的字段(cname,ccredit,ctype,tname,tsex,tfaculty)
Object是value,存放我们查询的字段所对应的值
例如:cname所对应的值为:计算机网络
则在jsp中可以根据XXX.get("cname")
来获取该key(cname)的值(计算机网络)
示例:
mapper接口:
List<Map<String, Object>> studentQueryAllCourse();
mapper对应的xml文件:
<select id="studentQueryAllCourse" resultType="map">
select cid,cname,ccredit,ctype,tname,tsex,tfaculty
from course_selection.course
LEFT JOIN course_selection.teacher ON course.course_tid = teacher.tid
</select>
Servise层省略。
Controller层:
List<Map<String, Object>> mapList = courseService.studentQueryAllCourse();
model.addAttribute("mapList", mapList);
return "course/pickCourseInfo";
pickCourseInfo.jsp
文件遍历部分代码:
<thead>
<tr>
<th>课程名称</th>
<th>总学分</th>
<th>课程类型</th>
<th>授课教师</th>
<th>教师性别</th>
<th>所属学院</th>
</tr>
</thead>
<tbody>
<c:forEach var="list" items="${mapList}">
<tr>
<td>${list.get("cname")}</td>
<td>${list.get("ccredit")}</td>
<td>${list.get("ctype")}</td>
<td>${list.get("tname")}</td>
<td>${list.get("tsex")}</td>
<td>${list.get("tfaculty")}</td>
<tr/>
</c:forEach>
</tbody>