分享

Mybatis一对多与一对一

 dabinglibrary 2014-12-04

[1].[代码] 一对一的XML配置文件 跳至 [1] [2]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<mapper namespace="dao.mapper.ClassMapper">
 
    <resultMap id="classResultMap" type="Classes">
        <id property="classid" column="classid1" />
        <result property="classname" column="classname" />
        <result property="teacherid" column="teacherid2" />
        <association property="teacher" column="teacherid" javaType="Teacher" select="getTeacher" />
<!--         <association property="teacher" column="teacherid" javaType="Teacher" select="dao.mapper.TeacherMapper.getTeacher" />  两个XML文件之间调用 -->
    </resultMap>
     
    <select id="selectAllByClassId" parameterType="int" resultMap="classResultMap">
        select * from class c where c.classid = #{classid};
    </select>
     
    <select id="getTeacher" parameterType="int" resultType="teacher">
        select * from teacher tt where tt.teacherid = #{teacherid2}
    </select>
     
     
</mapper>

[2].[代码] 一对多 两个配置文件之间调用 跳至 [1] [2]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
一对多中的"一"
<mapper namespace="dao.mapper.ClassMapper">
 
    <resultMap id="classResultMap" type="Classes">
        <id property="classid" column="classid1" />
        <result property="classname" column="classname" />
        <result property="teacherid" column="teacherid2" />
        <collection property="studentList" column="classid" javaType="ArrayList" ofType="Student" select="StudentDao.getStudentByClassID" />
    </resultMap>
     
    <select id="selectAllByClassId" parameterType="int" resultMap="classResultMap">
        select * from class c where c.classid = #{classid};
    </select>
</mapper>
 
 
一对多中的"多"
 
<mapper namespace="StudentDao">
 
    <resultMap type="Student" id="studentResultMap">
        <id property="studentid" column="studentid" />
        <result property="studentname" column="studentname" />
    </resultMap>
     
    <!-- 查询学生list,根据班级id -->
    <select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap">
        select *from student st WHERE st.classid = #{classid1}
    </select>
</mapper>





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多