1.oneToOne
<
select
id
=
"getClass"
parameterType
=
"int"
resultMap
=
"ClassResultMap"
>
select *
from
class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</
select
>
<
resultMap
type
=
"common.pojo.Classes"
id
=
"
ClassResultMap
"
>
<
id
property
=
"id"
column
=
"c_id"
/>
<
result
property
=
"name"
column
=
"c_name"
/>
<
association
property
=
"teacher"
javaType
=
"common.pojo.Teacher"
>
<
id
property
=
"id"
column
=
"t_id"
/>
<
result
property
=
"name"
column
=
"t_name"
/>
</
association
>
</
resultMap
>
<!-- 返回方式,子查询 -->
<
select
id
=
"getClass2"
parameterType
=
"int"
resultMap
=
"
ClassResultMap2
"
>
select *
from
class where c_id=#{id}
</
select
>
<
resultMap
type
=
"common.pojo.Classes"
id
=
"ClassResultMap2"
>
<
id
property
=
"id"
column
=
"c_id"
/>
<
result
property
=
"name"
column
=
"c_name"
/>
<
association
property
=
"teacher"
column
=
"teacher_id"
select
=
"getTeacher"
>
</
association
>
</
resultMap
>
<
select
id
=
"getTeacher"
parameterType
=
"int"
resultMap
=
"teacherResultMap"
>
select *
from teacher where t_id=#{id}
</
select
>
<
resultMap
type
=
"common.pojo.Teacher"
id
=
"teacherResultMap"
>
<
id
property
=
"id"
column
=
"t_id"
/>
<
result
property
=
"name"
column
=
"t_name"
/>
</
resultMap
>
|
2.oneToMany
<
mapper
namespace
=
"mapping.classesMapper"
>
<!-- 返回方式,resultMap -->
<
select
id
=
"getClass"
parameterType
=
"int"
resultMap
=
"ClassResultMap"
>
select *
from
class c,teacher t,student s where c.teacher_id=t.t_id and
s.class_id=c.c_id and c.c_id=#{id}
</
select
>
<
resultMap
type
=
"common.pojo.Classes"
id
=
"ClassResultMap"
>
<
id
property
=
"id"
column
=
"c_id"
/>
<
result
property
=
"name"
column
=
"c_name"
/>
<
association
property
=
"teacher"
javaType
=
"common.pojo.Teacher"
>
<
id
property
=
"id"
column
=
"t_id"
/>
<
result
property
=
"name"
column
=
"t_name"
/>
</
association
>
<
collection
property
=
"students"
ofType
=
"common.pojo.Student"
>
<
id
property
=
"id"
column
=
"s_id"
/>
<
result
property
=
"name"
column
=
"s_name"
/>
</
collection
>
</
resultMap
>
----------------------------------------------------------------------------------
<!-- 返回方式,子查询 -->
<
select
id
=
"getClass2"
parameterType
=
"int"
resultMap
=
"ClassResultMap2"
>
select *
from
class where c_id=#{id}
</
select
>
<
resultMap
type
=
"common.pojo.Classes"
id
=
"ClassResultMap2"
>
<
id
property
=
"id"
column
=
"c_id"
/>
<
result
property
=
"name"
column
=
"c_name"
/>
<
association
property
=
"teacher"
column
=
"teacher_id"
select
=
"getTeacher"
>
</
association
>
<
collection
property
=
"students"
ofType
=
"common.pojo.Student"
column
=
"c_id"
select
=
"getStudent"
></
collection
>
这里的column是说明传哪个值给子查询
</
resultMap
>
<
select
id
=
"getTeacher"
parameterType
=
"int"
resultType
=
"common.pojo.Teacher"
>
select
t_id id,t_name
name
from teacher where t_id=#{id}
</
select
>
<
select
id
=
"getStudent"
parameterType
=
"int"
resultType
=
"common.pojo.Student"
>
select
s_id id,s_name
name
from student where class_id=#{id}
</
select
>
</
mapper
>
|
MyBatis中使用collection标签来解决一对多的关联查询,ofType属性指定集合中元素的对象类型。
|