最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis關聯(lián)映射舉例詳解

 更新時間:2022年07月30日 09:00:35   作者:·~簡單就好  
關聯(lián)關系是面向對象分析、面向對象設計最終的思想,Mybatis完全可以理解這種關聯(lián)關系,如果關系得當,Mybatis的關聯(lián)映射將可以大大簡化持久層數(shù)據(jù)的訪問

一、關聯(lián)映射

舉例關系說明

數(shù)據(jù)庫創(chuàng)建表,student,teacher

關系說明:

  1. 一個老師可以有多個學生
  2. 一個學生只有一個老師
  3. 一個老師對學生:一對多的關系
  4. 一個學生老師:一對一的關系

二、一對一多對一的關系

查詢學生信息及其對應的教師信息

學生實體:用對象來存儲教師信息,因為一個學生對應一個教師對象

public class Student {
    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;
    //這個是重點
    private Teacher teacher;
}

教師實體:

public class Teacher {
    private Integer id;
    private String Tname;
}

1.第一種形式-連表查詢

數(shù)據(jù)庫查詢sql:

SELECT  student.id,student.name,teacher.name FROM student LEFT JOIN teacher  on student.t_id = teacher.id

mybatis多表聯(lián)查查詢語句:(嵌套其他實體對象)

對于特殊數(shù)據(jù):

  • 如果是對象:用association :< association property=“teacher” javaType=“com.qcby.entity.Teacher”>,特殊數(shù)據(jù)特殊處理
  • < result property=“id” column=“id”/> :所要查詢的字段,property代表java中實體的屬性名稱,column:表示數(shù)據(jù)庫的字段
   <!--    按照結果嵌套處理-->
<select id="getStudent1" resultMap="StudentTeacher1">
   SELECT  student.id,student.Sname,teacher.Tname FROM student  LEFT JOIN teacher  on student.t_id = teacher.id
</select>
   <resultMap id="StudentTeacher1" type="com.qcby.entity.Student">
       <result property="id" column="id"/>
       <result property="Sname" column="Sname"/>
       <result property="sex" column="sex"/>
       <result property="age" column="age"/>
       <result property="t_id" column="t_id"/>
     <!-- 復雜的屬性我們需要單獨去處理 對象:association   集合:collection   -->
   	 <!-- property="teacher" student類當中的關聯(lián)字段 -->
   	 <!-- javaType="com.javen.model.Teacher" 為復雜屬性設置類類型-->	
       <association property="teacher" javaType="com.qcby.entity.Teacher">
           <result property="id" column="id"/>
           <result property="Tname" column="Tname"/>
       </association>
   </resultMap>

2.第二種形式-分步查詢

數(shù)據(jù)庫查詢sql:

SELECT s.id,s.Sname,t.Tname FROM student s,teacher t where s.t_id = t.id

mybatis分布查詢查詢語句:

<select id = "getStudent"  resultMap="StudentTeacher">
    select * from student;
</select>
<!--結果映射集-->
<resultMap id="StudentTeacher"  type="com.qcby.entity.Student">
    <result property="id" column="id"/>
    <result property="Sname" column="Sname"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="t_id" column="t_id"/>
    <!-- select="getTeacher"  :調(diào)用下一個查詢語句       -->
    <!-- column="t_id" 兩個表的關聯(lián)字段-->
    <association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.qcby.entity.Teacher">
    select  * from  teacher where id = #{t_id};    <!-- #{id}; 可以寫任何東西,因為會自動匹配 t_id -->
</select>

三、一對多

查詢教師對應的學生信息

設立教師實體:用集合來存儲對應的學生信息,因為一個教師對應多個學生

public class Teacher {
    private Integer id;
    private String Tname;
    //這個一定要有
    private List<Student> students;
}

第一種形式按照結果嵌套處理

mybatis查詢語句:

<!--按照結果進行查詢-->
<select id="getTeacher" resultMap="TeacherStudent">
    SELECT  teacher.id,teacher.Tname,student.Sname FROM teacher
        LEFT JOIN student  on student.t_id = teacher.id
 </select>
<resultMap id="TeacherStudent" type="com.qcby.entity.Teacher">
    <result property="id" column="id"/>
    <result property="Tname" column="Tname"/>
    <!-- 復雜的屬性我么需要單獨去處理 對象:association   集合:collection
      在集合中的泛型信息,我們使用ofType獲取
      -->
    <collection property="students" ofType="com.qcby.entity.Student">
    	<!-- 查詢什么寫什么 -->
        <result property="Sname" column="Sname"/>
    </collection>
</resultMap>

第二種形式按照查詢嵌套處理

mybatis查詢語句: 對于特殊字段集合采用分布查詢的方式,特殊字段特殊處理:< collection property=“students” column=“t_id”

ofType=“com.qcby.entity.Student” select=“getStudentByTeacherId” />,getStudentByTeacherId一個新的查詢語句

<!--按照查詢嵌套處理:分布查詢-->
<select id="getTeacher" resultMap="TeacherStudent2">
    select * from teacher
</select>
<resultMap id="TeacherStudent2" type="com.qcby.entity.Teacher">
    	<!--column="t_id"  傳值-->
    <collection property="students" column="t_id"  
                ofType="com.qcby.entity.Student" select="getStudentByTeacherId" />  <!--實現(xiàn)分布查詢-->
</resultMap>
<select id="getStudentByTeacherId" resultType="com.qcby.entity.Student">
    select * from student where id = #{t_id}
</select

到此這篇關于Mybatis關聯(lián)映射舉例詳解的文章就介紹到這了,更多相關Mybatis關聯(lián)映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

东乡县| 义乌市| 尼玛县| 青神县| 怀柔区| 丹阳市| 南充市| 洱源县| 壶关县| 莫力| 商河县| 宿松县| 福建省| 沁源县| 轮台县| 门源| 西青区| 留坝县| 云林县| 通化县| 北京市| 贞丰县| 天台县| 尼勒克县| 广饶县| 宁河县| 扶沟县| 图木舒克市| 忻城县| 平南县| 巨鹿县| 甘谷县| 商城县| 延寿县| 平顶山市| 庆安县| 宣威市| 门源| 武夷山市| 新宾| 甘南县|