[英]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.