<!--任务7:要求完成MyClassMapper的映射 班级和学生是一对多关系【15】-->
时间: 2024-03-10 19:44:47 浏览: 35
任务7要求完成MyClassMapper的映射,班级和学生是一对多关系。在Mapper XML文件中,可以使用以下语句来实现对班级和学生表的操作:
1. 查询班级信息及其对应的学生列表
```
<select id="getClassById" parameterType="int" resultMap="classResultMap">
SELECT c.*, s.*
FROM class c
LEFT JOIN student s ON c.id = s.class_id
WHERE c.id = #{id}
</select>
<resultMap id="classResultMap" type="Class">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" ofType="Student">
<id property="id" column="s.id"/>
<result property="name" column="s.name"/>
<result property="age" column="s.age"/>
</collection>
</resultMap>
```
2. 查询班级信息及其对应的学生列表(使用嵌套查询方式)
```
<select id="getClassById" parameterType="int" resultMap="classResultMap">
SELECT *
FROM class
WHERE id = #{id}
</select>
<resultMap id="classResultMap" type="Class">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" ofType="Student" select="getStudentsByClassId"/>
</resultMap>
<select id="getStudentsByClassId" parameterType="int" resultMap="studentResultMap">
SELECT *
FROM student
WHERE class_id = #{id}
</select>
<resultMap id="studentResultMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
```
以上两种方式均可以实现对班级和学生表的查询操作,具体实现方式可以根据实际需求进行选择。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.atguigu.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
admin页面
root
<form action ="root.html" method="post">
username:<input type="text" name="username"/>
<input type="submit" value="提交"/>
</form>
访问HelloServlet
</body>
</html> 为什么点击hello的超链接会显示404
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)