zoukankan      html  css  js  c++  java
  • mybatis 一对一 一对多

    一对一的XML配置文件

    <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>

    一对多 两个配置文件之间调用

    一对多中的"一"
    <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>

  • 相关阅读:
    转:测试驱动开发全攻略
    转:如何提高自己的归纳总结能力?
    转:从编译链接过程解析static函数的用法
    C++ 不能在类体外指定关键字static
    转:画图解释 SQL join 语句
    转:[置顶] 从头到尾彻底理解KMP(2014年8月22日版)
    转:The Knuth-Morris-Pratt Algorithm in my own words
    转:数组与指针的区别
    删除单链表中间节点
    如果判断两个单链表有交?第一个交点在哪里?
  • 原文地址:https://www.cnblogs.com/lcuzhanglei/p/2513370.html
Copyright © 2011-2022 走看看