我有下面的表格:
IDofDog, IDOfOwner
1 4
2 4
3 3
4 3如你所见,一个人可以养多只狗。我还有一个类:
class A{
int ownerID;
List <Integer> dogs;
}有没有可能让选定的主人(例如4)得到他的狗?更确切地说,我希望(使用mybatis)获得这样的A a对象:
a.ownerID = 4
a.dogs = [1,2]发布于 2016-12-03 01:08:06
我认为您可以使用@Result注释来做到这一点。就像..。
interface DogRepo {
@Select("Select distinct IdOfOwner as ownerId from DogOwnership")
@Results(@Result(column = "ownerId", property = "dogs", many = @Many(select = "getDogIdsForOwner")))
A getDogsByOwner();
@Select("Select IdofDog from DogOwnership where IdOfOwner=#{ownerId}")
List<Integer> getDogIdsForOwner(@Param("ownerId") int ownerId);
}这可能不是最有效的方法,只读取行并手动构建对象可能更有效。
https://stackoverflow.com/questions/40937324
复制相似问题