我只是使用 findAllregions 参数的 userIdx 来 findFavouriteStore
这是我的查询
@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”的区域
但无法选择“findFavoriteStore”的商店
该查询不起作用,因为column
属性配置不正确。
这是列属性的相关部分文档:
数据库中的列名,或别名列标签
保存将作为嵌套语句传递到的值
输入参数。
正如您所看到的,只能使用主查询结果中的列。
这意味着您需要将人工列包含到查询中并在查询中使用它@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+,您可以使用默认接口方法来获取关联/集合,如下所示:
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 的查询。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)