鍍金池/ 問答/Java  數(shù)據(jù)庫/ Mybatis映射中的resultType和resultMap能同時用嗎?

Mybatis映射中的resultType和resultMap能同時用嗎?

Mybatis映射中的resultType和resultMap能同時用嗎?
我的數(shù)據(jù)庫字段和實體類屬性名不完全相同,所以要配置resultMap。
我是否可以在resultMap中只指定名字不同的那幾個字段,然后在select中共用resultMap和resultType?

clipboard.png

回答
編輯回答
魚梓

resultMap和resultType不能同時使用。

不過你可以使用繼承的方式擴展字段

<resultMap id="cityResultMap" type="city">
        <result column="id" property="id" />
        ...
    </resultMap>

    <resultMap id="cityMap" extends="cityResultMap" type="city">
        <result column="name" property="provinceName" />
        ...
    </resultMap>

數(shù)據(jù)庫中沒有,實體類中有的屬性配置在cityMap中。

2018年2月9日 19:26
編輯回答
硬扛

不可以的。不然會報錯。

2017年1月2日 08:40
編輯回答
笨笨噠

你運行一下就知道了吧,你可以在resultMap把所有列都配置了

2017年10月6日 05:59
編輯回答
話寡

可以直接用resultType.
查詢的時候 xxx as yyy就行了

2018年5月2日 07:32
編輯回答
忠妾

官方文檔上摘下來的
resultType The fully qualified class name or alias for the expected type that will be returned from this statement. Note that in the case of collections, this should be the type that the collection contains, not the type of the collection itself. Use resultType OR resultMap, not both.

resultMap A named reference to an external resultMap. Result maps are the most powerful feature of MyBatis, and with a good understanding of them, many difficult mapping cases can be solved. Use resultMap OR resultType, not both.

2018年2月23日 11:27