0%

MYSQL8—关联关系 一对多 多对一 多对多

1.一对多,一的用集合set<student>
比如教师的id,和学生的tearcherId
1.1先查询student

1
2
3
4
5
<select id=“selectUsers” resultType=“User”>
select id, username, tearcherId
from student
where id = #{id}
</select>

1.2在查询tearcher表

1
2
3
4
5
6
7
<resultMap id=“detailedBlogResultMap” type=“Blog”>
<id property=“id” column=“comment_id”/>
<result property=“username” column=“user_name”/>
<collection property=“comments” ofType=“student”>
<id property=“id” column=“comment_id”/>
</collection>
</resultMap>

改进版:

2.多对一 对的是一个对象

3.多对多 本质:基本还是一对也多,或者是多对一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<resultMap id=“detailedBlogResultMap” type=“Blog”>
<constructor>
<idArg column=“blog_id” javaType=“int”/>
</constructor>
<result property=“title” column=“blog_title”/>
<association property=“author” javaType=“Author”>
<id property=“id” column=“author_id”/>
<result property=“username” column=“author_username”/>
<result property=“password” column=“author_password”/>
<result property=“email” column=“author_email”/>
<result property=“bio” column=“author_bio”/>
<result property=“favouriteSection” column=“author_favourite_section”/>
</association>
<collection property=“posts” ofType=“Post”>
<id property=“id” column=“post_id”/>
<result property=“subject” column=“post_subject”/>
<association property=“author” javaType=“Author”/>
<collection property=“comments” ofType=“Comment”>
<id property=“id” column=“comment_id”/>
</collection>
</resultMap>