关于Mybatis中两表关联查询利用association进行连接

首先我们查询的是隧道的所有内容:

Controller层:

tunnelService.selectTunnelWithSiteByExample(example);

Service层:因为查询所有,所以返回的是一个list

List<Tunnel> vehSailInfoList = tunnelMapper.selectTunnelWithSiteByExample(example);

Mapper.xml层:

到这层我们要分步来写:

1.首先我们是查隧道关联站点,所以我们要在隧道Tunnel的实体里面,把站点的实体私有化放到里面去。
代码(这里最好用下private):

public HighwaySite site;//站点

2.编写查询语句首先查询隧道的所有属性:

<!-- 查询带站点信息的车道数据 -->
  <select id="selectTunnelWithSiteByExample" parameterType="com.vrview.ssm.model.example.TunnelExample" resultMap="WithSiteMap">
    <include refid="OracleDialectPrefix" />
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from HIS_VEH_SAIL_INFO
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
    <include refid="OracleDialectSuffix" />
  </select>

这里我们要注意返回的resultMap:

3.这里综合结果集:

<!-- 隧道结果集:带站点 (site要在model中定义)-->
  <resultMap extends="BaseResultMap" id="WithSiteMap" type="com.vrview.ssm.model.Tunnel">
  	<association column="PASSSITE_ID" property="site" select="selectLaneSite" />
  </resultMap>

4.最后在由根据从查询出来的隧道实体里面的字段PASSSITE_ID作为查询站点的id值用来条件查询站点

<!-- 查询站点 -->
  <select id="selectLaneSite" parameterType="java.lang.Long" resultMap="com.vrview.ssm.mapper.HighwaySiteMapper.BaseResultMap">
  		SELECT * FROM CFG_HIGHWAY_SITE WHERE ID = #{passsiteId}
  </select>

最后看下总的sql语句:

 <!-- 隧道结果集:带站点 (site要在model中定义)-->
  <resultMap extends="BaseResultMap" id="WithSiteMap" type="com.vrview.ssm.model.Tunnel">
  	<association column="PASSSITE_ID" property="site" select="selectLaneSite" />
  </resultMap>
  <!-- 查询站点 -->
  <select id="selectLaneSite" parameterType="java.lang.Long" resultMap="com.vrview.ssm.mapper.HighwaySiteMapper.BaseResultMap">
  		SELECT * FROM CFG_HIGHWAY_SITE WHERE ID = #{passsiteId}
  </select>
  <!-- 查询带站点信息的车道数据 -->
  <select id="selectTunnelWithSiteByExample" parameterType="com.vrview.ssm.model.example.TunnelExample" resultMap="WithSiteMap">
    <include refid="OracleDialectPrefix" />
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from HIS_VEH_SAIL_INFO
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
    <include refid="OracleDialectSuffix" />
  </select>
最后将查询到的结果是list层层返回。







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值