spring-JDBC-Templent 处理1对多结果集
spring-JDBC-Templent 处理1对多结果集,返回对象为listA,其中A对象中包含listB,B为子对象
- 如何处理结果集?
spring-JDBC-Templent 处理1对多结果集,返回对象为listA,其中A对象中包含listB,B为子对象
1.写一个方法,封装返回参数,封装A对象,封装B对象,A对象中有一个属性叫
```java
```List<B> children
2.查询出来的结果集合一定是一个全量数据,首先区分,可以使用Stream流,根据筛选条件,区分父子,父集合,子集合
3.创建一个集合,用来存放符合条件的子集,遍历父集合,再父集合中 再遍历子集合,根据筛选条件,如果符合条件将其添加到刚才创建的集合中,这样childRen就好了 ,然后创建A对象 把属性填上,把children进行赋值,封装返回的ListA 遍历完的结果 添加到集合中返回即可。
4.思路不用代码体现可能会有点乱,仅供参考
5.提供之前封装的代码,仅供参考
```java
/**
* 封装返回 父节点 和父节点下的子节点 主题域
* @param parent
* @param childList
* @return
*/
public ThemeFieldTempBO getTreeData(QuotaThemeFiled parent,List<QuotaThemeFiled> childList){
List<ThemeFieldBO> rspList=new ArrayList<>();
ThemeFieldTempBO par=new ThemeFieldTempBO();
par.setKey(parent.getThemeId().toString());
par.setValue(parent.getThemeId().toString());
par.setTitle(parent.getThemeName());
if (parent!=null&&childList.size()>0){
for (QuotaThemeFiled bo:childList){
if (parent.getThemeId()==bo.getpId()){
ThemeFieldBO chi=new ThemeFieldBO();
chi.setKey(bo.getThemeId().toString());
chi.setValue(bo.getThemeId().toString());
chi.setTitle(bo.getThemeName());
rspList.add(chi);
}
}
}
par.setChildren(rspList);
return par;
}