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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章