如何将 myBatis 结果映射到多个对象?
myBatis 3 是否可以将单个结果映射到多个对象,确保所有对象都引用同一个实例?有没有这样的例子我可以参考?
更新以添加更多详细信息:
例如,假设我在数据库中存储有关我的应用程序的联系人
的信息。我想知道是否可以使用 myBatis 将联系人的同一个实例映射到一个 Listing
类,该类包含一个 Contact
:
public class Listing {
private Contact myContact;
//getters & setters...
}
以及一个ContactsHolder
类,它还包含一个 Contact
:
public class ContactsHolder {
private Contact aContact
//getters & setters...
}
我需要 myBatis 映射到 Listing
和ContactsHolder
类是同一个实例。这可能吗?
Is it possible in myBatis 3 to map a single result to multiple objects, ensuring that the objects all reference the same instance? Is there an example of this I could reference?
Updated to add more detail:
For instance, let's say I store information regarding Contact
s for my application in my DB. I want to know if it's possible to use myBatis to map the same instance of a contact to, say, a Listing
class, which holds a Contact
:
public class Listing {
private Contact myContact;
//getters & setters...
}
as well as to a ContactsHolder
class, which also holds a Contact
:
public class ContactsHolder {
private Contact aContact
//getters & setters...
}
I need the object that is mapped by myBatis to both the Listing
and ContactsHolder
classes to be the same instance. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,MyBatis 无法通过标准结果映射来做到这一点。 (至少据我所知)。您可以选择“联系人”对象,然后手动构建 Listing 和 ContactsHolder,两者都引用该联系人。
或者实现自定义 ResultSetHandler。
这是一个特殊的请求,我不确定为什么你想要在两个对象之间共享相同的实例。这可能就是为什么 MyBatis 3 中不存在这样的功能。
No, MyBatis isn't able to do that with standard result mapping. (at least to my knowledge). You could select the "Contact" object, then build a Listing and ContactsHolder manually with both of them referencing the Contact.
Or implement a custom ResultSetHandler.
It's kind of a peculiar request, I'm not sure why you want the same instances shared across two objects like that. That's probably why no feature like this exists in MyBatis 3.
如果您使用 select 来获取关联的联系人并且在同一会话中获取两个对象,则可以使用标准结果映射来完成此操作。
修改
Listing
和ContactsHolder
的结果映射:现在创建一个查询
selectContact
:现在,如果您创建一些同时使用
listingMap
的选择code> 和contactsHolderMap
来映射引用同一联系人的Listing
和ContactsHolder
记录,它们都将使用相同的 ID 查询联系人。Mybatis 对会话中读取的所有对象使用本地缓存,因此在获取第二个关联联系人期间,将命中缓存并重用同一对象。
即使您手动执行两次查询以在同一事务中获取
Listing
和ContactsHolder
,也会使用相同的Contact
在两个返回的对象中。This can be done with standard result mapping if you use select to fetch the associated contact and both objects are fetched in the same session.
Modify result maps for
Listing
andContactsHolder
:Now create a query
selectContact
:Now if you create some select that uses both
listingMap
andcontactsHolderMap
to mapListing
andContactsHolder
records that reference the same contact they will both query for the contact using the same id.Mybatis uses local cache for all objects read in a session so during fetching of the second associated contact the cache will be hit and the same object will be reused.
Even if you do two queries manually to get
Listing
andContactsHolder
in the same transaction the sameContact
will be used in both returned objects.