[英]XML ResultMap in MyBatis with association
我覺得這個問題在google或stack上非常奇怪。 讓我解釋。
我的界面方法中的注釋中有結果映射。 只有在這種特殊情況下我需要動態查詢,這就是我決定在xml文件中為接口編寫整個映射器的原因。 下面我粘貼整個文件。 選擇查詢應該沒問題,但我在<resultMap>
遇到了一些困難。
在不同的網站上,我一直在尋找一對一,一對多,多對一的關聯和構造這個結果圖的良好解釋。
我看到有一種可能性將它分成子查詢,subresultmaps。 但我已經完成了myBatis注釋,我想使用它。 你能指點我,應該如何構造resultMap? 我不認為需要構造函數,鑒別器,但它仍然在喊......(這就是我添加<collection>
mark的原因) - IntelliJ強調整個<resultMap>
說: "The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)"
我知道這似乎很明顯,但我完全不知道如何正確地做到這一點。 請幫我。
xml映射文件:
<?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="pl.net.manager.dao.UsageCounterDAO">
<select id="getUsageCounterList" resultType="pl.net.manager.domain.UsageCounter"
resultMap="getUsageCounterListMap">
SELECT * FROM usage_counter WHERE
<if test="apiConsumerIdsList != null">
api_consumer IN
<foreach item="item" index="index" collection="apiConsumerIdsList"
open="(" separator="," close=")">
#{item}
</foreach>
AND
</if>
<if test="serviceConsumerIdsList != null">
service IN
<foreach item="item" index="index" collection="serviceConsumerIdsList"
open="(" separator="," close=")">
#{item}
</foreach>
AND
</if>
<if test="dateFrom != null">
date >= #{dateFrom} AND
</if>
<if test="dateTo != null">
date <= #{dateTo} AND
</if>
<if test="status != null">
status = #{status}
</if>
</select>
<resultMap id="getUsageCounterListMap" type="">
ofType="pl.net.manager.domain.UsageCounter">
<id property="id" column="id"/>
<result property="date" column="date"/>
<result property="apiConsumer" column="api_consumer"
javaType="pl.net.manager.domain.ApiConsumer"/>
<association property="apiConsumer" column="api_consumer"
resultMap="pl.net.manager.dao.ApiConsumerDAO.getApiConsumer"/>
<result property="service" column="service" javaType="pl.net.manager.domain.Service"/>
<association property="service" column="service"
resultMap="pl.net.manager.dao.ServiceDAO.getService"/>
<result property="counterStatus" column="counter_status"/>
<result property="ratingProcessId" column="rating_process_id"/>
<result property="value" column="value"/>
<result property="createdDate" column="created_date"/>
<result property="modifiedDate" column="modified_date"/>
</resultMap>
</mapper>
Mapper XML的XSD需要在<resultMap>
:
<association>
必須在所有<result>
標記之后,這意味着您需要對<result>
標記進行分組,然后在此之后添加<association>
標記。
其次,你不需要getUsageCounterList
中的resultType
和resultMap
。 使用在您的情況下是resultMap
任何一個
第三, getUsageCounterList
WHERE
子句被破壞。 如果只滿足第一個條件,那么在這種情況下你的SELECT將是這樣的:
SELECT * FROM usage_counter WHERE api_consumer IN (1,2,3) AND
正如我在上一個對您的查詢的回答中提到的,您可以使用<where>
標記並按照該答案中提到的語法進行操作。
第四,在resultMap
getUsageCounterListMap
您只需要type
,它將是您嘗試填充的Java類,因此您可以將ofType
移動到type
。
對於One-One映射 ,您可以使用<association>
標記,或者有另一種方法,假設您有一個Java POJO,如下所示:
public class Country{
private String name;
private City capital;
//getters and setters
}
public class City{
private String name;
//getters and setters
}
您可以使用XML編寫映射,如下所示:
<select id="getCountries" resultType="Country">
SELECT c.name, cy.name "capital.name"
FROM country c
JOIN city cy ON cy.name = c.capital
</select>
所以,MyBatis會確保創建的實例City
和值分配capital.name
到該實例的name
屬性。
對於一對多或多對多映射,您可以瀏覽MyBatis結果映射的<collection>
標記。 有關更多信息,請查看提供的鏈接中的Advacned Result Maps部分。
這發生在我身上 -
元素類型“resultMap”的內容必須匹配“(構造函數?,id *,結果*,關聯*,集合*,鑒別器?)”。
當有一個或多個拼寫錯誤
當存在未封閉的頭標記或不匹配的結束標記時
在collection
或association
的時間映射中在<resultMap>
標記內寫入<if></if>
<case><case>
類型的標記時
當你在其他一些標簽中寫下一些並不存在的標簽時
希望這些提示可以幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.