如果我在 MyBatis 中定义一个 <association> in a <resultMap> ,即使属性匹配列名,我是否也必须定义所有字段?
If I define an <association> in a <resultMap> in MyBatis, do I have to define all fields even if the properties match the column names?
我正在一个项目中试用 MyBatis,到目前为止给我留下了深刻的印象。我正在使用 Spring Boot starter,理想情况下希望在注释中有尽可能多的配置。
一切顺利,直到我尝试在我的映射器中实现 'has-a' 连接。我有一个客户实体,其地址为:
Customer (id, name, email, ...)
Address (id, customerId, street, city, ...)
也许我错了(我希望如此),但似乎不可能使用注释来映射它(如果我想避免 N+1 问题,我这样做了),所以我不情愿地创建了一个 customerMapper.xml 文件,其中包含:
<mapper namespace="foo.domain.mapper.CustomerMapper">
<resultMap id="customer" type="foo.domain.Customer">
<id property="id" column="customerId"/>
<association property="address" resultMap="address"/>
</resultMap>
<resultMap id="address" type="foo.domain.Address">
<id property="id" column="addressId"/>
</resultMap>
</mapper>
在我的映射器中我有:
@Select("SELECT * FROM Customer cus LEFT OUTER JOIN Address adr ON adr.customerId = cus.customerId")
@ResultMap("customer")
List<Customer> findAll();
然而,当我调用 findAll()
时,我发现只有 customer
和 address
的 id
字段被填充,尽管 [=45] =] 名称与 Customer 和 Address 的列名称匹配,所以我希望 MyBatis 自动映射它们。
如果我取出 <association>
,我会得到一个完全填充的 customer
。同样,如果我开始在每个 <resultMap>
中添加 <result>
元素,我为其添加元素的字段将被正确填充。
所以 - 我的问题是 - 如果我指定 <association>
,我的其余字段将不会自动映射是否正确,所以我必须明确指定 [=44] 中的每个字段=]?如果是这样,并且考虑到我拥有的数据结构(无法更改)需要对大多数查询进行连接,我想如果我使用 MyBatis,我将最终编写 more 代码而不是普通 JDBC!
我想我已经找到了我自己的问题的答案 here。
There are three auto-mapping levels:
NONE - disables auto-mapping. Only manually mapped properties will be set.
PARTIAL - will auto-map results except those that have nested result mappings defined inside (joins).
FULL - auto-maps everything.
The default value is PARTIAL, and it is so for a reason. When FULL is used auto-mapping will be performed when processing join results and joins retrieve data of several different entities in the same row hence this may result in undesired mappings.
所以默认情况下,就我而言,自动映射不会发生。我需要决定是否打开它。
对于关联,每个列都应映射到相应的对象变量
我正在一个项目中试用 MyBatis,到目前为止给我留下了深刻的印象。我正在使用 Spring Boot starter,理想情况下希望在注释中有尽可能多的配置。
一切顺利,直到我尝试在我的映射器中实现 'has-a' 连接。我有一个客户实体,其地址为:
Customer (id, name, email, ...)
Address (id, customerId, street, city, ...)
也许我错了(我希望如此),但似乎不可能使用注释来映射它(如果我想避免 N+1 问题,我这样做了),所以我不情愿地创建了一个 customerMapper.xml 文件,其中包含:
<mapper namespace="foo.domain.mapper.CustomerMapper">
<resultMap id="customer" type="foo.domain.Customer">
<id property="id" column="customerId"/>
<association property="address" resultMap="address"/>
</resultMap>
<resultMap id="address" type="foo.domain.Address">
<id property="id" column="addressId"/>
</resultMap>
</mapper>
在我的映射器中我有:
@Select("SELECT * FROM Customer cus LEFT OUTER JOIN Address adr ON adr.customerId = cus.customerId")
@ResultMap("customer")
List<Customer> findAll();
然而,当我调用 findAll()
时,我发现只有 customer
和 address
的 id
字段被填充,尽管 [=45] =] 名称与 Customer 和 Address 的列名称匹配,所以我希望 MyBatis 自动映射它们。
如果我取出 <association>
,我会得到一个完全填充的 customer
。同样,如果我开始在每个 <resultMap>
中添加 <result>
元素,我为其添加元素的字段将被正确填充。
所以 - 我的问题是 - 如果我指定 <association>
,我的其余字段将不会自动映射是否正确,所以我必须明确指定 [=44] 中的每个字段=]?如果是这样,并且考虑到我拥有的数据结构(无法更改)需要对大多数查询进行连接,我想如果我使用 MyBatis,我将最终编写 more 代码而不是普通 JDBC!
我想我已经找到了我自己的问题的答案 here。
There are three auto-mapping levels:
NONE - disables auto-mapping. Only manually mapped properties will be set.
PARTIAL - will auto-map results except those that have nested result mappings defined inside (joins).
FULL - auto-maps everything.The default value is PARTIAL, and it is so for a reason. When FULL is used auto-mapping will be performed when processing join results and joins retrieve data of several different entities in the same row hence this may result in undesired mappings.
所以默认情况下,就我而言,自动映射不会发生。我需要决定是否打开它。
对于关联,每个列都应映射到相应的对象变量