今天在寫一個關聯表查詢的時候出現的幾個問題。
需要查出這個訂單的所有狀態,屬於一對多的關係
//這個爲查詢的語句
<select id="selectOrderByMap" parameterType="java.lang.String" resultMap="OrderLinkAudit">
select
ord.id,ord.ser_title,ord.org_name,ord.status,audit.content as audit_content, audit.create_time as audit_create_time,audit.order_id as audit_order_id
from ser_order ord
left join ser_order_audit audit on ord.id = audit.order_id
<where>
<if test="_parameter!= null">
and ord.id = #{_parameter, jdbcType=VARCHAR}
</if>
</where>
order by audit.create_time desc
</select>
// 訂單關聯審覈記錄
<resultMap id="OrderLinkAudit" type="com.xx.xx.xx.xx.Order" extends="BaseResultMap">
<collection property="auditList" columnPrefix="audit_"
ofType="com.xx.xx.xx.xx.OrderAudit">
<id column="id" jdbcType="VARCHAR" property="id"/>
<result column="order_id" jdbcType="VARCHAR" property="orderId"/>
<result column="status" jdbcType="INTEGER" property="status"/>
<result column="user_id" jdbcType="VARCHAR" property="userId"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
<result column="content" jdbcType="VARCHAR" property="content"/>
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
</collection>
</resultMap>
不瞭解resultMap,所以在使用的時候出了差錯。
resultMap:適合使用返回值是自定義實體類的情況。
resultType:適合使用返回值得數據類型是非自定義的,即jdk的提供的類型。
在想返回有相同字段content,create_time ,一開始沒有用resultMap中定義的別名columnPrefix=”audit_”,導致直接覆蓋了,前一個表的相同字段值。
columnPrefix:定義的別名用來區分是哪個類的字段,映射對應的屬性,區分他們分別屬於哪些類。