Mybatis 实现一对多关系


//teacher 类
    private int id;
   private String name;
    // 一个老师有多个学生 集合
   private List<Student> students;

// student 类
    private int id;
   private String name;
   private int tid;


  <select id="getTeacher" resultMap="TeacherMap">
        select sid, sname, tname, tid
        from student s,teacher t
        where = s.tid
        and = #{tid};

  <resultMap id="TeacherMap" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--javaType 指定属性的类型; 集合中的泛型,使用oftype获取 集合使用 collection 处理-->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>


    <select id="getTeacher2" resultMap="TeacherMap2">
        select * from teacher where id = #{tid}

   <resultMap id="TeacherMap2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherID"/>

   <select id="getStudentByTeacherID" resultType="Student">
        select * from student where tid = #{tid}

《Mybatis 实现一对多关系》有5个想法

  1. There are a few things us guys would rather do, like sorting a box of mixed nails, screws, bolts, washers and nuts into little sliding drawers of a box bought at Home Depot! Keelia Forster Tavis

  2. Nice post. I learn something new and challenging on blogs I stumbleupon every day. It will always be exciting to read through content from other writers and use something from other websites. Faustine Nikolaus Ming

  3. Someone necessarily help to make severely articles I might state. This is the first time I frequented your website page and to this point? I amazed with the analysis you made to make this actual post amazing. Excellent activity! Analise Arnoldo Zumwalt


邮箱地址不会被公开。 必填项已用*标注