从单个ibatis查询返回多个类型 [英] Return Multiple Types from a Single ibatis Query

查看:154
本文介绍了从单个ibatis查询返回多个类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索表单,需要包含两个不同表格的结果。这些表彼此没有关系,我们是分开的。在我的示例场景中,我们有加油站和杂货店。杂货店表可能具有freezerSize,produceStorage,numberOfCarts等属性。加油站表可能有gasTankSizeInGallons,windowCleanerInGallons等....两个表之间有一些共享字段(即 - numberOfEmployees,squareFeetOfStoreSpace,numberOfShelves等...)。

I have a search form that needs to include results from two different tables. The tables have no relationship to each other and our separate. In my example scenario, we have gas stations and grocery stores. The grocery store table might have attributes like freezerSize, produceStorage, numberOfCarts. The gas stations table might have gasTankSizeInGallons, windowCleanerInGallons, etc.... There are some shared fields between the two tables (i.e. - numberOfEmployees, squareFeetOfStoreSpace, numberOfShelves, etc...).

我的搜索查询需要一起排序和显示加油站和杂货店。我正在考虑使用SQL联合并将不适用的字段设置为0或null。但是,我真的很难知道如何使用ibatis(因为两个对象的类型不同):

My search query needs to sort and display the gas stations and grocery stores together. I was thinking of using a SQL union and setting the non-applicable fields to 0 or null. However, I'm really stumped about how to do this with ibatis (since both objects are of a different type):

<select id="searchQuery" parameterClass="java.util.Map" resultClass="????????????????">
    SELECT
        storeName, storeCity, storeState, numberOfCarts, freezerSize, 0 gasTankSizeInGallons, 0 windowCleanerInGallons
    FROM
        grocery_stores
    UNION
    SELECT
        storeName, storeCity, storeState, 0 numberOfCarts, 0 freezerSize, gasTankSizeInGallons, windowCleanerInGallons
    FROM
        gas_stations
    ORDER BY storeState, storeCity, storeName
</select>

注意 - 实际查询在订单中有更多的东西,它是分页的,并且有select中的更多字段(加上select字段中每个适用字段的where子句)。

Note - the actual query has many more things in the order by, it is paginated, and there are many more fields in the select (plus a where clause for each applicable field in the select field).

上述查询的resultClass应该是什么?我有一个GroceryStore和GasStation类,它们都来自Store。但是,Store没有很多GroceryStore和GasStation特定字段。我可以做两个单独的查询,但结果的排序必须在java中完成,因为它需要先加载大量数据,所以效率很低。

What should be the resultClass for the above query? I have a GroceryStore and GasStation class both of which extend from Store. However, Store does not have many of the GroceryStore and GasStation specific fields. I could do two separate queries, but the ordering of the results would have to be done in java and it would be inefficient since it would need to load large amounts of data first.

谢谢

推荐答案

经过大量的谷歌搜索,我找到了自己问题的答案。

After much googling, I figured out the answer to my own question.

ibatis鉴别器将在gasStation和groceryStore类之间进行选择。

The ibatis discriminator will pick between the gasStation and groceryStore classes.

<resultMap id="searchResultMap" class="Store">
     <discriminator column="storeType" javaType="java.lang.String">
           <subMap value="grocery" resultMap="groceryStoreMap"/>
           <subMap value="gasStation" resultMap="gasStationMap"/>
     </discriminator>
</resultMap>

然后,我会编辑我的查询,在select字段中添加storeType,并为groceryStore创建resultMap, gasStation。

I would then edit my query to add storeType in the select field and create a resultMap for the groceryStore and gasStation.

注意 - 为了解决这个问题,我读了这个stackoverflow问题

Note - to figure this out, I read this stackoverflow question.

这篇关于从单个ibatis查询返回多个类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆