欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis一對多與多對一查詢處理詳解

 更新時間:2021年03月04日 11:01:27   作者:KittyGuy  
這篇文章主要給大家介紹了關(guān)于Mybatis一對多與多對一查詢處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

要點

  • 主要還是結(jié)果集映射(resultMap)
  • association標(biāo)簽: 一個復(fù)雜類型的關(guān)聯(lián);許多結(jié)果將包裝成這種類型(JavaBean)嵌套結(jié)果映射,關(guān)聯(lián)可以是 resultMap 元素,或是對其它結(jié)果映射的引用
  • collection標(biāo)簽: 一個復(fù)雜類型的集合(List)嵌套結(jié)果映射,集合可以是resultMap元素,或是對其它結(jié)果映射的引用

一對多(association)

數(shù)據(jù)庫結(jié)構(gòu)

tid是student的外鍵,是teacher表的id

JavaBean

public class Student {
 private int id;
 private String name;
 private Teacher teacher;
}
public class Teacher {
 private int id;
 private String name;
}

mapper.java

public interface StudentMapper {
 List<Student> getStudent();
 List<Student> getStudent2();
}

Student類里面有teacher,要想查出Student中的Teacher就需要映射。

方法有二

(1)

<select id="getStudent" resultMap="StudentTeacher">
    select * from mybatis.student
  </select>
  <resultMap id="StudentTeacher" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
  </resultMap>
        <!--子查詢-->
  <select id="getTeacher" resultType="Teacher">
     select * from mybatis.teacher where id=#{Anything}
  </select>

(2)

<select id="getStudent2" resultMap="StudentTeacher2">
    select * from mybatis.student as s ,mybatis.teacher as t where s.tid=t.id
  </select>
  <resultMap id="StudentTeacher2" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" javaType="Teacher">
      <result property="id" column="tid"/>
      <result property="name" column="name"/>
    </association>
  </resultMap>

多對一

JavaBean

public class Teacher2 {
  private int id;
  private String name;
  //一個老師對應(yīng)多個學(xué)生
  private List<Student2> students;
}
public class Student2 {
  private int id;
  private String name;
  private int tid;
}

mapper.java

public interface TeacherMapper2 {
  List<Teacher2> getTeacher(@Param("id") int id);
}

mapper.xml

<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mybatis.DAO.TeacherMapper2">
  <select id="getTeacher" parameterType="int" resultMap="teacherS">
    select s.id, s.name, s.tid,t.id as tid, t.name as tname from mybatis.student as s ,mybatis.teacher as t where s.tid=t.id and t.id=#{id}
  </select>
  <resultMap id="teacherS" type="teacher2">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="student2">
      <result property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="tid" column="tid"/>
    </collection>
  </resultMap>
</mapper>

小結(jié)

  • 一對多和多對一區(qū)別不大
  • 其實就是association(類)和collection(集合)的區(qū)別
  • 還有ofType和javaType的區(qū)別
  • 如果查詢結(jié)果不符合預(yù)期,請設(shè)置別名試一試

到此這篇關(guān)于Mybatis一對多與多對一查詢處理的文章就介紹到這了,更多相關(guān)Mybatis一對多與多對一查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis模糊查詢like語句該如何寫

    mybatis模糊查詢like語句該如何寫

    MyBatis模糊查詢通常使用LIKE關(guān)鍵字,結(jié)合concat函數(shù)拼接通配符%實現(xiàn),在MyBatis配置文件中,通過#{keyword}傳遞參數(shù),生成帶有通配符的查詢語句,MyBatis-Plus中,通過LambdaQueryWrapper類和like方法構(gòu)建模糊查詢條件,簡化查詢操作
    2024-09-09
  • 基于Spring@Autowired注解與自動裝配詳談

    基于Spring@Autowired注解與自動裝配詳談

    下面小編就為大家?guī)硪黄赟pring@Autowired注解與自動裝配詳談。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 利用idea生成webservice客戶端超詳解步驟(wsdl文件的使用)

    利用idea生成webservice客戶端超詳解步驟(wsdl文件的使用)

    這篇文章主要給大家介紹了關(guān)于利用idea生成webservice客戶端超詳解步驟,第一次接觸webservice,從采坑到采坑,算是了解了一些,明白了一些,文中通過代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • gateway基本配置教程

    gateway基本配置教程

    路由(Route)由一個ID,一個目標(biāo)URI(最終路由到的url地址),一組斷言(匹配條件判斷)和一組過濾器定義,這篇文章主要介紹了gateway基本配置,需要的朋友可以參考下
    2023-05-05
  • Java中==與equals()及hashcode()三者之間的關(guān)系詳解

    Java中==與equals()及hashcode()三者之間的關(guān)系詳解

    最近也是在讀Hollis的《深入理解Java核心技術(shù)》里面一節(jié)講到了equals()和hashcode()的關(guān)系,對于這個高頻面試點,咱們需要認(rèn)真理清一下幾者之間的關(guān)系
    2022-10-10
  • Springboot中的異步任務(wù)執(zhí)行及監(jiān)控詳解

    Springboot中的異步任務(wù)執(zhí)行及監(jiān)控詳解

    這篇文章主要介紹了Springboot中的異步任務(wù)執(zhí)行及監(jiān)控詳解,除了自己實現(xiàn)線程外,springboot本身就提供了通過注解的方式,進(jìn)行異步任務(wù)的執(zhí)行,下面主要記錄一下,在Springboot項目中實現(xiàn)異步任務(wù),以及對異步任務(wù)進(jìn)行封裝監(jiān)控,需要的朋友可以參考下
    2023-10-10
  • JAVA內(nèi)存模型和Happens-Before規(guī)則知識點講解

    JAVA內(nèi)存模型和Happens-Before規(guī)則知識點講解

    在本篇文章里小編給大家整理的是一篇關(guān)于JAVA內(nèi)存模型和Happens-Before規(guī)則知識點內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2020-11-11
  • Java8中List轉(zhuǎn)換String字符串幾種方式

    Java8中List轉(zhuǎn)換String字符串幾種方式

    這篇文章主要給大家介紹了關(guān)于Java8中List轉(zhuǎn)換String字符串的幾種方式,在實際開發(fā)中經(jīng)常遇到List轉(zhuǎn)為String字符串的情況,文中給出了幾種方法的示例代碼,需要的朋友可以參考下
    2023-07-07
  • SpringBoot的@Conditional條件注解詳解

    SpringBoot的@Conditional條件注解詳解

    這篇文章主要介紹了SpringBoot的@Conditional條件注解詳解,打開每個自動配置類,都會看到@Conditional或其衍生的條件注解,本節(jié)我們來認(rèn)識下@Conditional注解,需要的朋友可以參考下
    2023-12-12
  • Spring-data-JPA使用時碰到的問題以及解決方案

    Spring-data-JPA使用時碰到的問題以及解決方案

    這篇文章主要介紹了Spring-data-JPA使用時碰到的問題以及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評論