广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis中嵌套查询的使用解读
  • 769
分享到

mybatis中嵌套查询的使用解读

mybatis嵌套查询嵌套查询使用mybatis查询 2023-03-15 11:03:05 769人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis嵌套查询的使用传递多个参数总结mybatis嵌套查询的使用 在使用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

从图中也是可以看出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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: mybatis中嵌套查询的使用解读

本文链接: https://www.lsjlt.com/news/199911.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作