温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis中的嵌套查询如何使用

发布时间:2023-03-15 11:36:15 来源:亿速云 阅读:123 作者:iii 栏目:开发技术

今天小编给大家分享一下mybatis中的嵌套查询如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

mybatis嵌套查询的使用

在使用mybatis时,当我们遇到表与表之之间存在关联的时候,就可以使用嵌套查询

比如说

当一个对象包含了另一个对象

/**
 * 公交实体类中包含了司机信息和路线信息
 */
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;//司机

    private Way way;//路线
    //省略封装方法
 }

当一个对象中包含另一个对象的泛型集合

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;
    //省略封装方法
 }

当一个对象中包含了另外一个对象时,在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属性中指向需要调用哪个sql执行(可以指向其它命名空间的,比如:com.whx.bus.mapper.WayMapper.selectByPrimaryKey)-->
    <!-- 在column属性中指定需传递给子查询的参数 -->
    <!-- 在property属性中指定Java对象中的变量名 -->
    <!-- 在javaType属性中指定当前对象的限定名(如果是集合的话就是ofType) -->
    <association property="driver" column="driverId" javaType="com.whx.bus.entity.Driver" select="selectDriverById">
    </association>
    <!-- 在select属性中指向需要调用哪个sql执行(可以指向其它命名空间的,比如:com.whx.bus.mapper.WayMapper.selectByPrimaryKey)-->
    <!-- 在column属性中指定需传递给子查询的参数 -->
    <!-- 在property属性中指定Java对象中的变量名 -->
    <!-- 在javaType属性中指定当前对象的限定名(如果是集合的话就是ofType) -->
    <association property="way" column="wayId" javaType="com.whx.bus.entity.Way" select="selectWayById">
    </association>
  </resultMap>
  <!-- 司机结果集 -->
  <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>
  <!-- 路线结果集 -->
  <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属性中指向需要调用哪个sql执行(可以指向其它命名空间的,比如:com.whx.bus.mapper.WayMapper.selectByPrimaryKey)-->
    <!-- 在column属性中指定需传递给子查询的参数 -->
    <!-- 在property属性中指定Java对象中的变量名 -->
    <!-- 在ofType属性中指定泛型集合对应的对象的限定名(如果是单个对象的话就是javaType) -->
    <collection property="stations" ofType="com.whx.bus.entity.Station" column="id" javaType="java.util.ArrayList" select="selectStationsByWay">
    </collection>
  </resultMap>
  <!-- 站点结果集 -->
  <resultMap id="StationResultMap" type="com.whx.bus.entity.Station" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
  </resultMap>
  <!-- 通过主键获取公交信息 -->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from bus
    where id = #{id,jdbcType=INTEGER}
  </select>

  <!-- 通过路线获取站点信息 -->
  <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>
  <!-- 通过司机id查询司机信息 -->
  <select id="selectDriverById" parameterType="java.lang.Integer" resultMap="DriverResultMap">
    select driver.* from driver where id = #{value}
  </select>
  <!-- 通过路线id查询路线信息 -->
  <select id="selectWayById" parameterType="java.lang.Integer" resultMap="WayResultMap">
    select way.* from way where id = #{value}
  </select>
</mapper>

配置了resultMap的嵌套查询之后,调用自己的查询只要调用相应的resultMap之后就可以了,执行查询之后就会自己会调用子查询(注意:子查询其实也是对应一个查询语句,也要有相应的结果集)。

附上一个查询结果的debug

mybatis中的嵌套查询如何使用

从图中也是可以看出Bus中的Way对象是有数据的,并且Way中的泛型集合stations也是有数据的,这是因为子查询中的结果集也配置了嵌套查询,所以相对于嵌套了两次~

如果使用多个嵌套需要额外注意,在多对多的情况下,切勿嵌套死循环了,不然就尴尬了~233

需要嵌套对象还是集合就根据自己的需求来了,注意单个对象是association、集合是collection(属性在代码中有说明)

还有一个点需要注意的就是:如果配置了嵌套了,在原查询语句中就不要查嵌套的表了,只查原表中的就行~不然就会出错切记切记

传递多个参数

如果嵌套查询需传递多个参数

<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" />
    <!-- 如果这里需要传递一个card和一个driverId到子查询中 -->
    <!-- cardParam表示自查询中用到的键(键可自己定义)、card表示当前结果集的card列的值(列根据上面的结果集来) -->
    <!-- driverIdParam表示自查询中用到的键(键可自己定义)、driverId表示当前结果集的driverId列的值(列根据上面的结果集来) -->
    <association property="driver" column="{cardParam=card,driverIdParam=driverId}" javaType="com.whx.bus.entity.Driver" select="selectDriverById">
    </association>
</resultMap>

以上就是“mybatis中的嵌套查询如何使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI