[英]MyBatis: How do I store Multiple Values from Query?
在我的AppMapper.xml中,我有一個類似的查詢:
<select id="getCompleteDetails" parameterType="Map" resultType="String">
SELECT * FROM PersonProfile WHERE ID = #{id}
</select>
假設它將返回所有字符串的名稱,地址,年齡,職業。 我用這個正確嗎?
<select id="selectPerson" parameterType="int" resultType="String">
或者因為有多個值要接收,所以它應該類似於
<select id="selectPerson" parameterType="int" resultMap="PersonProfileObj">
如果我使用"resultMap=PersonProfileObj"
,我知道我需要像創建resultMap
<resultMap type="com.test.PersonProfileObj" id="PersonProfileObj">
<result property="Name" column="Name">
<result property="Address" column="Address">
<result property="Age" column="Age">
<result property="Profession" column="Profession">
</resultMap>
//我的resultMap正確嗎?
*我詢問是否使用resultType = String或resultMap“ PersonProfileObj”
我的理解正確嗎?
resultType
指定由給定的映射SQL語句返回的對象的類型(如果返回了多個對象,則為列表的元素)。 如果指定resultType="String"
則查詢應恰好返回字符串類型的一個字段。
如果您需要返回多個字段,則可以:
返回對象時,可以使用默認映射來設置與列名稱相對應的對象屬性,也可以指定resultMap
,以允許以更靈活的方式設置resultMap
column -> property
映射。
還要注意, resultMap
上有一個autoMapping
屬性,該屬性將列隱式地映射到具有相同名稱的屬性,因此在您的示例中,假定列與屬性具有相同的名稱,則足以具有此映射:
<resultMap type="com.test.PersonProfileObj" id="PersonProfileObj" autoMapping="true">
</resultMap>
還要注意,您通常希望為結果映射指定id
,特別是如果查詢在集合/關聯映射中使用或具有聯接時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.