gpt4 book ai didi

java - 从单个 ibatis 查询返回多个类型

转载 作者:行者123 更新时间:2023-11-29 00:18:45 25 4
gpt4 key购买 nike

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

我的搜索查询需要对加油站和杂货店进行排序和显示。我正在考虑使用 SQL 联合并将不适用的字段设置为 0 或 null。但是,我真的对如何使用 ibatis 执行此操作感到困惑(因为这两个对象属于不同类型):

<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>

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

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

谢谢

最佳答案

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

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

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

然后我会编辑我的查询以在选择字段中添加 storeType 并为 groceryStore 和 gasStation 创建一个 resultMap。

注意 - 为了解决这个问题,我阅读了 this stackoverflow question .

关于java - 从单个 ibatis 查询返回多个类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com