如何将myBatis结果映射到多个对象? [英] How to map a myBatis result to multiple objects?
问题描述
在myBatis 3中是否可以将单个结果映射到多个对象,以确保所有对象都引用同一实例?有没有我可以参考的例子?
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?
已更新以添加更多详细信息:
例如,假设我在数据库中存储有关我的应用程序的Contact
的信息.我想知道是否可以使用myBatis将联系人的同一实例映射到一个Listing
类,该类包含一个Contact
:
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...
}
以及ContactsHolder
类,该类也包含一个Contact
:
as well as to a ContactsHolder
class, which also holds a Contact
:
public class ContactsHolder {
private Contact aContact
//getters & setters...
}
我需要由myBatis映射到Listing
和ContactsHolder
类的对象成为同一实例.这可能吗?
I need the object that is mapped by myBatis to both the Listing
and ContactsHolder
classes to be the same instance. Is this possible?
推荐答案
否,MyBatis无法使用标准结果映射做到这一点. (至少据我所知).您可以选择联系人"对象,然后手动构建一个Listing和ContactsHolder,两者都引用该联系人.
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.
或实现自定义ResultSetHandler.
Or implement a custom ResultSetHandler.
这是一种特殊的请求,我不确定为什么要在两个对象之间共享相同的实例.这可能就是为什么MyBatis 3中不存在类似功能的原因.
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.
这篇关于如何将myBatis结果映射到多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!