mybaties association使用

两张表:

第一张表so:

第二张表so_record:

这表名及数据只是我随便造的,没有具体意义。有时候会遇到类似这样的需求。比如在查询so全部数据的时候,还需要查询到so中的order_num在so_record中的出现次数这样的需要关联查询的需求。这时可以用到association

<?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.windy.rabbitmq.mapper.SoMapper">
  <resultMap id="BaseResultMap" type="com.windy.rabbitmq.model.So">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_num" jdbcType="VARCHAR" property="orderNum" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <association property="count" select="getCount" column="{so=order_num}"></association>
  </resultMap>

  <select id="getAllSoInfo" resultMap="BaseResultMap">
     select id, order_num, `status`, create_time, update_time
    from so
  </select>

  <select id="getCount" resultType="Integer">
    select count(1) from so_record where so=#{so}
  </select>
</mapper>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值