Fork me on GitHub

MyBatis中一对多和多对一

连表查询
多对一查询

<select id="findAll2" resultMap="a1">
SELECT s.`id` sid ,s.`name` sname ,t.`name` tname
FROM `student` s ,`teacher` t
WHERE s.`tid`=t.`id`
</select>
<resultMap id="a1" type="pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="pojo.Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>


一对多查询

<select id="getTeacher" resultMap="a2">
SELECT t.name tname,s.name sname FROM teacher t,student s WHERE s.tid=t.id

</select>
<resultMap id="a2" type="pojo.Teacher2">
<result property="name" column="tname"/>
<collection property="student2" ofType="pojo.Student2">
<result property="name" column="sname"/>
</collection>
</resultMap>

posted @ 2024-03-16 22:40  一名狗书匠&  阅读(2)  评论(0编辑  收藏  举报

asd