Mybatis多表關聯查詢之三表查詢
阿新 • • 發佈:2018-12-25
在開發中經常用到多表查詢,這裡演示三表查詢的情況。
業務邏輯:購物車訂單的邏輯
該邏輯共涉及到三張表
1. order表 就是訂單表 記錄 訂單人的資訊、訂單地址、訂單號、訂單的總價, 一條訂單記錄可能會有多個商品所以 有建立了一個訂單詳情表
2. 訂單詳情表記錄、訂單中一種商品的總價,數量
3. 商品表、記錄商品的詳情
要求展示的是訂單記錄:
那麼就用到了多表查詢,同時在這三個表中查詢
訂單與訂單詳情是1對多的關係 訂單詳情與商品是1對1關係,在resultMap中要這樣寫
<resultMap id ="orderResultMap" type="com.hd.entity.Order">
<id column="orderId" jdbcType="INTEGER" property="id" />
<result column="loginName" jdbcType="VARCHAR" property="loginName" />
<result column="userAddress" jdbcType="VARCHAR" property="userAddress" />
<result column="allCost" jdbcType="REAL" property="cost" />
<result column="serialnumber" jdbcType="VARCHAR" property="serialnumber" />
<collection property="orderDetailList" ofType="com.hd.entity.OrderDetail" >
<result column="orderDetailId" property="id"></result>
<result column="quantity" property="quantity"></result>
<result column="cost" property="cost"></result>
<association property="product" javaType="com.hd.entity.Product">
<id column="productId" property="id"></id>
<id column="fileName" property="fileName"></id>
</association>
</collection>
</resultMap>