zoukankan      html  css  js  c++  java
  • Mybatis一对一,一对多

    一对一

    假如有student表(学生表)和student_card表(学生证表)。

    student表中有一个字段self_card用来查student_card,student_card表中有一个student_id用来查student。

    在Student的pojo类中,成员self_card被替换成StudentCard的pojo类,通过查询student_id将Student中的StudentCard补全。

    student表如果没有self_card这个字段对于pojo类来说是无关紧要的,因为pojo类可以重新增加其对应的成员再对其赋值。

    这种情况在Mybatis中可以在StudentMapper的<resultMap>中添加

    <association property="self_card" column="id" 
        	select="mytatis.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId" />
    

    就可以获取Student的所有信息。

    一对多

    如果一个学生对应多门课程,这时候称为一对多,而每门课程对其具体的课程信息就是一对一。
    一对多的级联就需要用<collection>

    <collection property="studentLecturesList" column="id" 
      	    select="mytatis.mapper.StudentLectureMapper.findStudentLectureByStuId" />
    

    一对一还是用上面的<association>
    具体代码请点击源码

  • 相关阅读:
    IHttpHandler and HttpModule
    sql page reship
    prototype
    google search engine theory
    ghost
    sql split
    Missing you is a kind of my deeppain in my life
    file control
    a story of c#(reship)
    teacher and student(reship)
  • 原文地址:https://www.cnblogs.com/bihanghang/p/9989036.html
Copyright © 2011-2022 走看看