踩坑了,做一下笔记,长长记性。
多表关联查询。
collecton 标签
属性:
property : 对应实体类之中的属性名。
column:子查询时,映射时的参数;
column="{a = a1, b=b1}",多参数为例。
a代表子查询中的入参。name = #{aname},那么a就是aname。
b代表SQL主查询语句中的查出来的字段名,也就是select 之后,from之前的字段名。如果有as,就用as之后的。比如 select name as t_name... 此时,b 就是 t_name;如果没有as,b就是 name。
javaType:一般用到的时候就是List,毕竟子查询一般查出来的就是集合。如果是一对一,直接查就好了,还用什么子查询。非要用的话,就不要用这个标签,有type 和 ofType 给你用。
select:对应select标签的id,也就是子查询的语句。
子查询要求:
要有resultMap标签。
子查询语句:返回对应的。
重点:主查询语句和子查询语句并不需要有什么关系,你只是用主查询语句中查出来的数据去调用子查询!!!
<resultMap id="allMsg" type="teacher">
<id column="name" property="tName"></id>
<id column="id_no" property="tIdNo"></id>
<result column="age" property="tAge" />
<collection property="studentList" select="selectStudentByTeacher" column="{name=t_name,idNo=t_id_no}" javaType="List" />
</resultMap>
<resultMap id="student" >
<id column="sno" property="sno"></id>
<result column="t_name" property="tName" />
<result column="t_id_no" property="tIdNo" />
<result column="age" property="sAge" />
<result column="name" property="sName" />
</resultMap>
<select id="selectStudentByTeacher" resultMap="student">
select
sno,
t_name,
t_id_no,
age,
name
from
s_student
where
t_name = #{name} AND
t_id_no = #{idNo}
</select>
<select id="selectTeacherByID" resultMap="allMsg" parameterType="Map">
select
t_name,
t_id_no,
age
from
s_teacher
where
id_no = #{idNo} AND
name = #{name}
</select>
差不多就是这个样子。