这是场景,我有 5 个相互之间有一对多关系的表,我必须在下面给定 Pojos 和 Jooq 中以分层方式映射结果数据。
DB表是a,b,c,d,e
// Here are response Pojo's
Class APojo {
public string name;
public List<BPojo> listOfB;
}
Class BPojo {
public string name;
public List<CPojo> listOfC;
}
Class CPojo {
public string name;
public List<DPojo> listOfD;
}
Class DPojo {
public string name;
public List<EPojo> listOfE;
}
Class EPojo {
public string name;
}
预期的样本响应
{
"name":"A1",
"list_of_b":[
{
"name":"A1B1",
"list_of_c":[
{
"name":"A1B1C1",
"list_of_d":[
{
"name":"A1B1C1D1",
"list_of_e":[
{
"name":"A1B1C1D1E1"
},
{
"name":"A1B1C1D1E2"
}
]
},
{
"name":"A1B1C1D2",
"list_of_e":[
{
"name":"A1B1C1D2E1"
},
{
"name":"A1B1C1D2E2"
}
]
}
]
},
{
"name":"A1B1C2",
"list_of_d":[
{
"name":"A1B1C2D1",
"list_of_e":[
{
"name":"A1B1C2D1E1"
},
{
"name":"A1B1C2D1E2"
}
]
},
{
"name":"A1B1C2D2",
"list_of_e":[
{
"name":"A1B1C2D2E1"
},
{
"name":"A1B1C2D2E2"
}
]
}
]
}
]
},
{
"name":"A1B2",
"list_of_c":[
{
"name":"A1B2C1",
"list_of_d":[
{
"name":"A1B2C1D1",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
},
{
"name":"A1B2C1D2",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
}
]
},
{
"name":"A1B2C2",
"list_of_d":[
{
"name":"A1B2C2D1",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
},
{
"name":"A1B2C2D2",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
}
]
}
]
}
]
}
我首先尝试过这样的事情,但它没有用,因为 fetch groups 只接受 2 个参数
using(configuration()).select(A.fields())
.select(B.fields())
.select(C.fields())
.select(D.fields())
.select(E.fields())
.from(A)
.join(B).on(A.ID.eq(B.A_ID)
.join(C).on(B.ID.eq(C.B_ID)
.join(D).on(C.ID.eq(D.C_ID)
.join(E).on(D.ID.eq(E.D_ID)
.fetchGroups(
r -> r.into(A).into(APojo.class),
r -> r.into(B).into(BPojo.class),
r -> r.into(C).into(CPojo.class),
r -> r.into(D).into(DPojo.class),
r -> r.into(E).into(EPojo.class)
);
然后我得到了这篇文章,并按照下面给出的方法进行了尝试,并尝试了这篇文章中给出的其他 2 种方法,但这也没有奏效,因为Collectors.toMap
只接受 2 个参数,我必须获取 5 级分层数据。
using(configuration()).select(A.fields())
.select(B.fields())
.select(C.fields())
.select(D.fields())
.select(E.fields())
.from(A)
.join(B).on(A.ID.eq(B.A_ID)
.join(C).on(B.ID.eq(C.B_ID)
.join(D).on(C.ID.eq(D.C_ID)
.join(E).on(D.ID.eq(E.D_ID)
.collect(Collectors.groupingBy(
r -> r.into(A).into(APojo.class),
Collectors.toMap(
r -> r.into(B).into(BPojo.class),
r -> r.into(C).into(CPojo.class)
r -> r.into(D).into(DPojo.class)
r -> r.into(E).into(EPojo.class)
)));