iBatis - 标头详细问题
我正在尝试将一些新功能放入使用IBATIS的现有应用程序中,但我有点受到设计决策的困扰。
有一个现有的类(称其为A类),我想添加一些新字段。这些字段将是B型。
查询将通过外部连接加入B。
因此,这将是
public class A {
//... existing fields
private List<B> bList; // may use a Map rather than a list?
// etc.
}
public class B {
private int id; // primary key
private int type;
private String description;
// etc.
}
我在Web应用程序中使用它。在第一页上,我想返回“ A”的列表,然后在B旁边放置链接。
例如:
LinktoRecordA1 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA2 - LinktoB1 LinktoB3
LinktoRecordA3 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA4
等。
(NB:记录A4与任何b的链接没有链接 - 因此上述外部联接)
在“ A”的初始获取中,我只想知道B记录存在,而它的主要键是通过B详细记录介绍链接。因此,我的问题是,如何在“ A”对象上创建填充的“ B”列表的情况下如何?
I'm trying to put some new functionality into an existing app that uses iBatis but I'm a little stuck with one of the design decisions.
There is an existing class (call it class A) which I want to add some new fields to. These fields will be of type B.
The query will join B via an outer join.
So it will be something like
public class A {
//... existing fields
private List<B> bList; // may use a Map rather than a list?
// etc.
}
public class B {
private int id; // primary key
private int type;
private String description;
// etc.
}
I'm using this in a web app. On the first page I want to return a list of "A's", and then put links beside for the B's.
Eg:
LinktoRecordA1 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA2 - LinktoB1 LinktoB3
LinktoRecordA3 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA4
etc.
(NB: Record A4 has no links to any B's - hence the outer join mentioned above)
In the initial fetch of the "A's", I only want to know that the B record exists, and what it's primary key is to present the link through to the B detail record. So my problem is, how do I do this without creating a fully populated list of "B's" on the "A" object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的评论:
在诸如您描述的情况下(返回列表并且用户可以从中选择某些内容时),我注意到用户通常会选择一个或两个记录以查看更多详细信息。在这种情况下,您最终返回了一个完全填充的对象的列表。
在那种情况下,我要做的就是从选择对象标识符而不是成熟的对象的后备。
因此,您可以仅使用此数据创建一个新类:
然后,您将查询编写您的查询,以返回A和B的标识符,并返回类似的结果:
在此阶段
resultmap
标签派上用场,可以将结果转换为c
对象包含以下方式的列表:这对于避免n+1查询也非常有用。当您返回带有B列表的对象时,您可能已经在执行此操作,但是如果您使用单独的查询来检索列表(即
select
属性 actect> result tag),则比我建议您查看IBATIS数据映射器开发人员指南中的“避免n+1选择”部分,以最大程度地减少返回数据所需的查询数量。底线是...
...如果您处理大量数据,则返回完整加载的对象只是为了显示少量信息,可能是过分的。
..另一方面,如果您处理少量数据,介绍了处理IDS的另一个类可能会使事物复杂化,因此您可能同样返回A和B的完整对象(但是避免n+1查询)。
Regarding your comment:
In situations like the one you describe (when a list is returned and the user can select something from it), I have noticed that the user usually selects one or two records to see more details. In this case you end up returned a list of fully populated objects for nothing.
In that case, what I do, is fallback to just select object identifiers instead of full-fledged objects.
So you could create a new class with only this data:
You then write your query with an outer join to return A's and B's identifiers and return a result similar to this:
At this stage the
groupBy
attribute of theresultMap
tag comes in handy to transform the result in a list ofC
objects containing this:This is also very useful in avoiding N+1 queries. You may already be doing this when you return A objects with list of B’s, but if you are using separate queries to retrieve the list (i.e
select
attribute onresult
tag), than I suggest you take a look at the "Avoiding N+1 Selects" sections in the iBatis Data Mapper developer guide, to minimize the number of queries needed to return the data.Bottom line is...
...if you handle a large volume of data, then returning full loaded objects just to display a small amount of info, is probably overkill.
..on the other hand, if you handle a small amount of data, introducing another class that handles the IDs will probably complicate the things, so you might just as well return full objects of A’s and B’s (but avoiding N+1 queries of course).