zoukankan      html  css  js  c++  java
  • mybatis嵌套查询,解决分页查询数量与sql执行器查询数量不一致的问题(本文内容转贴)

    表与表之之间存在关联的时候,就可以使用嵌套查询

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

    /**
     * 公交实体类中包含了司机信息和路线信息
     */
    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;
        //省略封装方法
     }

    嵌套查询

    <?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>

    如果使用多个嵌套需要额外注意,在多对多的情况下,切勿嵌套死循环了,不然就尴尬了~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>
  • 相关阅读:
    03:矩形分割 (二分)
    09:膨胀的木棍 (二分+角度计算)
    A Cubic number and A Cubic Number (二分) HDU
    B
    08:不重复地输出数 (set)
    10:河中跳房子 (二分)
    07:和为给定数 (二分)
    04:网线主管 (二分)
    河中跳房子
    010:输出前k大的数
  • 原文地址:https://www.cnblogs.com/ixixi/p/13491933.html
Copyright © 2011-2022 走看看