溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis中的嵌套查詢?nèi)绾问褂?/h1>
發(fā)布時(shí)間:2023-03-15 11:36:15 來(lái)源:億速云 閱讀:135 作者:iii 欄目:開(kāi)發(fā)技術(shù)

今天小編給大家分享一下mybatis中的嵌套查詢?nèi)绾问褂玫南嚓P(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

mybatis嵌套查詢的使用

在使用mybatis時(shí),當(dāng)我們遇到表與表之之間存在關(guān)聯(lián)的時(shí)候,就可以使用嵌套查詢

比如說(shuō)

當(dāng)一個(gè)對(duì)象包含了另一個(gè)對(duì)象

/**
 * 公交實(shí)體類中包含了司機(jī)信息和路線信息
 */
public class Bus implements Serializable {
    private Integer id;

    private String card;

    private Integer driverid;

    private Integer wayid;

    private Double price;

    private Date topen;

    private Date tclose;

    private Driver driver;//司機(jī)

    private Way way;//路線
    //省略封裝方法
 }

當(dāng)一個(gè)對(duì)象中包含另一個(gè)對(duì)象的泛型集合

public class Way implements Serializable {
    private Integer id;

    private String name;

    private Date topen;

    private Date tclose;

    private List<Station> stations;

    private String topenString;

    private String tcloseString;
    //省略封裝方法
 }

當(dāng)一個(gè)對(duì)象中包含了另外一個(gè)對(duì)象時(shí),在resultMap中就可以使用嵌套查詢

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.whx.bus.mapper.BusMapper" >
  <resultMap id="BaseResultMap" type="com.whx.bus.entity.Bus" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="card" property="card" jdbcType="VARCHAR" />
    <result column="driverId" property="driverid" jdbcType="INTEGER" />
    <result column="wayId" property="wayid" jdbcType="INTEGER" />
    <result column="price" property="price" jdbcType="DOUBLE" />
    <result column="topen" property="topen" jdbcType="TIMESTAMP" />
    <result column="tclose" property="tclose" jdbcType="TIMESTAMP" />
    <!-- 在select屬性中指向需要調(diào)用哪個(gè)sql執(zhí)行(可以指向其它命名空間的,比如:com.whx.bus.mapper.WayMapper.selectByPrimaryKey)-->
    <!-- 在column屬性中指定需傳遞給子查詢的參數(shù) -->
    <!-- 在property屬性中指定Java對(duì)象中的變量名 -->
    <!-- 在javaType屬性中指定當(dāng)前對(duì)象的限定名(如果是集合的話就是ofType) -->
    <association property="driver" column="driverId" javaType="com.whx.bus.entity.Driver" select="selectDriverById">
    </association>
    <!-- 在select屬性中指向需要調(diào)用哪個(gè)sql執(zhí)行(可以指向其它命名空間的,比如:com.whx.bus.mapper.WayMapper.selectByPrimaryKey)-->
    <!-- 在column屬性中指定需傳遞給子查詢的參數(shù) -->
    <!-- 在property屬性中指定Java對(duì)象中的變量名 -->
    <!-- 在javaType屬性中指定當(dāng)前對(duì)象的限定名(如果是集合的話就是ofType) -->
    <association property="way" column="wayId" javaType="com.whx.bus.entity.Way" select="selectWayById">
    </association>
  </resultMap>
  <!-- 司機(jī)結(jié)果集 -->
  <resultMap id="DriverResultMap" type="com.whx.bus.entity.Driver">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="driver_card" property="driverCard" jdbcType="VARCHAR" />
    <result column="mobile" property="mobile" jdbcType="VARCHAR" />
  </resultMap>
  <!-- 路線結(jié)果集 -->
  <resultMap id="WayResultMap" type="com.whx.bus.entity.Way">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="topen" property="topen" jdbcType="TIMESTAMP" />
    <result column="tclose" property="tclose" jdbcType="TIMESTAMP" />
    <result column="topenString" property="topenString" jdbcType="VARCHAR" />
    <result column="tcloseString" property="tcloseString" jdbcType="VARCHAR" />
    <!-- 在select屬性中指向需要調(diào)用哪個(gè)sql執(zhí)行(可以指向其它命名空間的,比如:com.whx.bus.mapper.WayMapper.selectByPrimaryKey)-->
    <!-- 在column屬性中指定需傳遞給子查詢的參數(shù) -->
    <!-- 在property屬性中指定Java對(duì)象中的變量名 -->
    <!-- 在ofType屬性中指定泛型集合對(duì)應(yīng)的對(duì)象的限定名(如果是單個(gè)對(duì)象的話就是javaType) -->
    <collection property="stations" ofType="com.whx.bus.entity.Station" column="id" javaType="java.util.ArrayList" select="selectStationsByWay">
    </collection>
  </resultMap>
  <!-- 站點(diǎn)結(jié)果集 -->
  <resultMap id="StationResultMap" type="com.whx.bus.entity.Station" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
  </resultMap>
  <!-- 通過(guò)主鍵獲取公交信息 -->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from bus
    where id = #{id,jdbcType=INTEGER}
  </select>

  <!-- 通過(guò)路線獲取站點(diǎn)信息 -->
  <select id="selectStationsByWay" parameterType="java.lang.Integer" resultMap="StationResultMap">
    select station.* from station inner join way_station on station.id = way_station.stationId where way_station.wayId = #{value}
  </select>
  <!-- 通過(guò)司機(jī)id查詢司機(jī)信息 -->
  <select id="selectDriverById" parameterType="java.lang.Integer" resultMap="DriverResultMap">
    select driver.* from driver where id = #{value}
  </select>
  <!-- 通過(guò)路線id查詢路線信息 -->
  <select id="selectWayById" parameterType="java.lang.Integer" resultMap="WayResultMap">
    select way.* from way where id = #{value}
  </select>
</mapper>

配置了resultMap的嵌套查詢之后,調(diào)用自己的查詢只要調(diào)用相應(yīng)的resultMap之后就可以了,執(zhí)行查詢之后就會(huì)自己會(huì)調(diào)用子查詢(注意:子查詢其實(shí)也是對(duì)應(yīng)一個(gè)查詢語(yǔ)句,也要有相應(yīng)的結(jié)果集)。

附上一個(gè)查詢結(jié)果的debug

mybatis中的嵌套查詢?nèi)绾问褂?></p><p>從圖中也是可以看出Bus中的Way對(duì)象是有數(shù)據(jù)的,并且Way中的泛型集合stations也是有數(shù)據(jù)的,這是因?yàn)樽硬樵冎械慕Y(jié)果集也配置了嵌套查詢,所以相對(duì)于嵌套了兩次~</p><p>如果使用多個(gè)嵌套需要額外注意,在多對(duì)多的情況下,切勿嵌套死循環(huán)了,不然就尷尬了~233</p><p>需要嵌套對(duì)象還是集合就根據(jù)自己的需求來(lái)了,注意單個(gè)對(duì)象是association、集合是collection(屬性在代碼中有說(shuō)明)</p><p>還有一個(gè)點(diǎn)需要注意的就是:如果配置了嵌套了,在原查詢語(yǔ)句中就不要查嵌套的表了,只查原表中的就行~不然就會(huì)出錯(cuò)切記切記</p><h3>傳遞多個(gè)參數(shù)</h3><p>如果嵌套查詢需傳遞多個(gè)參數(shù)</p><pre class=<resultMap id="BaseResultMap" type="com.whx.bus.entity.Bus" >     <id column="id" property="id" jdbcType="INTEGER" />     <result column="card" property="card" jdbcType="VARCHAR" />     <result column="driverId" property="driverid" jdbcType="INTEGER" />     <result column="wayId" property="wayid" jdbcType="INTEGER" />     <result column="price" property="price" jdbcType="DOUBLE" />     <result column="topen" property="topen" jdbcType="TIMESTAMP" />     <result column="tclose" property="tclose" jdbcType="TIMESTAMP" />     <!-- 如果這里需要傳遞一個(gè)card和一個(gè)driverId到子查詢中 -->     <!-- cardParam表示自查詢中用到的鍵(鍵可自己定義)、card表示當(dāng)前結(jié)果集的card列的值(列根據(jù)上面的結(jié)果集來(lái)) -->     <!-- driverIdParam表示自查詢中用到的鍵(鍵可自己定義)、driverId表示當(dāng)前結(jié)果集的driverId列的值(列根據(jù)上面的結(jié)果集來(lái)) -->     <association property="driver" column="{cardParam=card,driverIdParam=driverId}" javaType="com.whx.bus.entity.Driver" select="selectDriverById">     </association> </resultMap>

以上就是“mybatis中的嵌套查詢?nèi)绾问褂谩边@篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI