【Java】Mybatis映射中的resultType和resultMap能同时用吗?

Mybatis映射中的resultType和resultMap能同时用吗?
我的数据库字段和实体类属性名不完全相同,所以要配置resultMap。
我是否可以在resultMap中只指定名字不同的那几个字段,然后在select中共用resultMap和resultType?

【Java】Mybatis映射中的resultType和resultMap能同时用吗?

回答

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>

数据库中没有,实体类中有的属性配置在cityMap中。

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

官方文档上摘下来的
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.

不可以的。不然会报错。

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

以上是 【Java】Mybatis映射中的resultType和resultMap能同时用吗? 的全部内容, 来源链接: utcz.com/a/87185.html

回到顶部