如何将参数传递给子查询
how to transfer parameter to sub query
我只是使用 findAllregions 参数的 userIdx 来查找 FavoriteStore
这是我的查询
@Select("SELECT * FROM region")
@Results(value = {
@Result(property = "regionIdx", column = "regionIdx"),
@Result(property = "stores", javaType = Store.class, column = "storeIdx",
many = @Many(select = "com.travely.travely.mapper.StoreMapper.findFavoriteStore", fetchType = FetchType.LAZY))
})
List<Region> findAllRegions(@Param("userIdx") final Long useridx);
@Select("SELECT s.* FROM store as s NATURAL JOIN favorite as f WHERE f.userIdx=#{userIdx} AND f.isFavorite = 1")
List<Store> findFavoriteStore(@Param("userIdx") final long userIdx);
适用于 'findAllRegions' 的 select 区域
但不适用于 'findFavoriteStore'
的 select 商店
查询失败,因为 column
属性配置不正确。
这是列属性的相关部分 documentation:
The column name from the database, or the aliased column label that
holds the value that will be passed to the nested statement as an
input parameter.
如您所见,只能使用主查询结果中的列。
这意味着您要么需要将人工列包含到查询中,然后像这样在 @Result.column
中使用它:
@Select("SELECT #{userIdx} userIdx, r.* FROM region r")
@Results(value = {
@Result(property = "regionIdx", column = "regionIdx"),
@Result(
property = "stores", javaType = Store.class, column = "userIdx",
many = @Many(
select = "com.travely.travely.mapper.StoreMapper.findFavoriteStore",
fetchType = FetchType.LAZY))
})
List<Region> findAllRegions(@Param("userIdx") final Long useridx);
或者,如果使用 java 8+,您可以使用默认接口方法来获取 associations/collections,如下所示:
interfact MyMapper {
@Select("SELECT * FROM region")
@Results(value = {
@Result(property = "regionIdx", column = "regionIdx")
})
List<Region> findAllRegions();
default List<Region> findAllRegions(Long userIdx) {
List<Region> regions = findAllRegions();
List<Strore> favoriteStores = findFavoriteStore(userIdx);
for(Region region:regions) {
region.setStores(favoriteStores);
}
return regions;
}
@Select("SELECT s.* FROM store as s NATURAL JOIN favorite as f WHERE f.userIdx=#{userIdx} AND f.isFavorite = 1")
List<Store> findFavoriteStore(@Param("userIdx") final long userIdx);
}
请注意,这不使用延迟获取最喜欢的商店。似乎这不是必需的,因为在不需要最喜欢的商店的情况下,没有 userIdx 的查询可以(并且应该使用。
我只是使用 findAllregions 参数的 userIdx 来查找 FavoriteStore
这是我的查询
@Select("SELECT * FROM region")
@Results(value = {
@Result(property = "regionIdx", column = "regionIdx"),
@Result(property = "stores", javaType = Store.class, column = "storeIdx",
many = @Many(select = "com.travely.travely.mapper.StoreMapper.findFavoriteStore", fetchType = FetchType.LAZY))
})
List<Region> findAllRegions(@Param("userIdx") final Long useridx);
@Select("SELECT s.* FROM store as s NATURAL JOIN favorite as f WHERE f.userIdx=#{userIdx} AND f.isFavorite = 1")
List<Store> findFavoriteStore(@Param("userIdx") final long userIdx);
适用于 'findAllRegions' 的 select 区域 但不适用于 'findFavoriteStore'
的 select 商店查询失败,因为 column
属性配置不正确。
这是列属性的相关部分 documentation:
The column name from the database, or the aliased column label that holds the value that will be passed to the nested statement as an input parameter.
如您所见,只能使用主查询结果中的列。
这意味着您要么需要将人工列包含到查询中,然后像这样在 @Result.column
中使用它:
@Select("SELECT #{userIdx} userIdx, r.* FROM region r")
@Results(value = {
@Result(property = "regionIdx", column = "regionIdx"),
@Result(
property = "stores", javaType = Store.class, column = "userIdx",
many = @Many(
select = "com.travely.travely.mapper.StoreMapper.findFavoriteStore",
fetchType = FetchType.LAZY))
})
List<Region> findAllRegions(@Param("userIdx") final Long useridx);
或者,如果使用 java 8+,您可以使用默认接口方法来获取 associations/collections,如下所示:
interfact MyMapper {
@Select("SELECT * FROM region")
@Results(value = {
@Result(property = "regionIdx", column = "regionIdx")
})
List<Region> findAllRegions();
default List<Region> findAllRegions(Long userIdx) {
List<Region> regions = findAllRegions();
List<Strore> favoriteStores = findFavoriteStore(userIdx);
for(Region region:regions) {
region.setStores(favoriteStores);
}
return regions;
}
@Select("SELECT s.* FROM store as s NATURAL JOIN favorite as f WHERE f.userIdx=#{userIdx} AND f.isFavorite = 1")
List<Store> findFavoriteStore(@Param("userIdx") final long userIdx);
}
请注意,这不使用延迟获取最喜欢的商店。似乎这不是必需的,因为在不需要最喜欢的商店的情况下,没有 userIdx 的查询可以(并且应该使用。