两张表:
第一张表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>