mybatis框架實現一對多多對一查詢


1.多對一:

多個學生,對應一個老師
對於學生這邊而言,關聯...多個學生,關聯一個老師【多對一】
對於老師而言,集合,一個老師又很多學生【一對多】

實體類:
//使用了lombok
@Data
public class Teacher {
    private int id;
    private String name;
}

@Data
public class Student {
    private int id;
    private String name;

    //學生需要關聯一個老師!
    private Teacher teacher;

}

按照查詢嵌套處理:(子查詢)

<!--
思路:
    1、查詢所有的學生信息
    2、根據查詢出來的學生的id的tid,尋找對應的老師! -子查詢
-->
<!--此處的resultMap要寫下文reaultMap標簽中的id-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
​
<resultMap id="StudentTeacher" type="com.rui.pojo.Student">
    <!--復雜的屬性,我們需要單獨處理  對象:association  集合:collection-->
    <!--column表示你數據庫表中的外鍵,也就是學生表的tid(對應老師的id)-->
    <!--select表示將teacher類轉入的方法,使用下文的select標簽-->
    <association property="teacher" column="tid" javaType="com.rui.pojo.Teacher" select="getTeacher"/>
</resultMap>
​
<select id="getTeacher" resultType="com.rui.pojo.Teacher">
    select * from teacher where id = #{id}
</select>

按照結果嵌套處理:(連表查詢)

<!--按照結果嵌套處理-->
<select id="getStudent2" resultMap="StudentTeacher2">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid=t.id;
</select>
​
<resultMap id="StudentTeacher2" type="com.rui.pojo.Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="com.rui.pojo.Teacher">
        <result property="id" column="tid"></result>
        <result property="name" column="tname"></result>
    </association>
​
</resultMap>

2.一對多

比如:一個老師擁有多個學生!
對於老師而言,就是一對多的關系

@Data
public class Teacher {
    private int id;
    private String name;
​
    //一個老師擁有多個學生
    private List<Student> students;
}
@Data
public class Student {
    private int id;
    private String name;
    private int tid;
​
}

按照結果嵌套處理:

<!--按結果嵌套查詢-->
<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid=t.id and t.id = #{tid}
</select>
<resultMap id="TeacherStudent" type="com.rui.pojo.Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <!--復雜的屬性,我們需要單獨處理  對象:association  集合:collection
        javaType="" 指定屬性的類型
        集合中的泛型信息,我們使用ofType獲取
    -->
    <collection property="students" ofType="com.rui.pojo.Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>

按照查詢嵌套處理

<select id="getTeacher2" resultMap="TeacherStudent2">
    select * from mybatis.teacher where id = #{tid}
</select>
<resultMap id="TeacherStudent2" type="com.rui.pojo.Teacher">
    <collection property="students" javaType="ArrayList" ofType="com.rui.pojo.Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
​
<select id="getStudentByTeacherId" resultType="com.rui.pojo.Student">
    select * from mybatis.student where tid = #{tid}
</select>

小結

1.關聯-association【多對一】
2.集合-collection 【一對多】
3.javaType & ofType
 JavaType用來指定實體類中屬性的類型
 ofType用來指定映射到List或者集合中的pojo類型,泛型中的約束類型!

注意點:

 保證SQL的可讀性,盡量保證通俗易懂
 注意一對多和多對一中,屬性名和字段的問題!
 如果問題不好排查錯誤,可以使用日志,建議使用Log4j


以上內容為學習b站狂神老師mybatis筆記,有錯誤歡迎指出,大佬勿噴.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM