亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis一對(duì)多與多對(duì)一查詢處理詳解

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

要點(diǎn)

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

一對(duì)多(association)

數(shù)據(jù)庫(kù)結(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>

多對(duì)一

JavaBean

public class Teacher2 {
  private int id;
  private String name;
  //一個(gè)老師對(duì)應(yīng)多個(gè)學(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é)

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

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

相關(guān)文章

  • mybatis模糊查詢like語(yǔ)句該如何寫

    mybatis模糊查詢like語(yǔ)句該如何寫

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

    基于Spring@Autowired注解與自動(dòng)裝配詳談

    下面小編就為大家?guī)?lái)一篇基于Spring@Autowired注解與自動(dòng)裝配詳談。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • 利用idea生成webservice客戶端超詳解步驟(wsdl文件的使用)

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

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

    gateway基本配置教程

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

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

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

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

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

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

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

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

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

    SpringBoot的@Conditional條件注解詳解

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

    Spring-data-JPA使用時(shí)碰到的問(wèn)題以及解決方案

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

最新評(píng)論