Java开发笔记15(树的列表查询)
1. Controller:
/**
* 通过id查询树列表
* @param id
*/
@GetMapping("/listById")
private Result listByParentId(String id) {
String stationTelecode = getStation(getUser()).getStationTelecode();
List<StationInfRelaTree> treeList = stationInfRelaService.listByParentId(id, stationTelecode);
HashMap<String, List<StationInfRelaTree>> treeMap = new HashMap<>();
treeMap.put("content",treeList);
return Result.ok(treeList);
}
* 通过id查询树列表
* @param id
*/
@GetMapping("/listById")
private Result listByParentId(String id) {
String stationTelecode = getStation(getUser()).getStationTelecode();
List<StationInfRelaTree> treeList = stationInfRelaService.listByParentId(id, stationTelecode);
HashMap<String, List<StationInfRelaTree>> treeMap = new HashMap<>();
treeMap.put("content",treeList);
return Result.ok(treeList);
}
2. Service:
List<StationInfRelaTree> listByParentId(String id, String stationTelecode);
3. Impl:
@Override
public List<StationInfRelaTree> listByParentId(String id, String stationTelecode) {
List<StationInfRelaTree> resultList = null;
if ("".equals(id) || id == null) {
resultList = stationInfRelaTreeDao.treeAllList(stationTelecode);
if (resultList.size() > 0) {
StationInfRelaTree stationInfRelaTree = resultList.get(0);
Set<StationInfRelaTree> children = stationInfRelaTree.getChildren();
resultList = new ArrayList<>(children);
clearChildren(resultList);
}
return resultList;
}
String val = "-1";
StationInfRelTree findById = InfRelTreeDao.findNoteById(id, stationTelecode);
if (val.equals(findById.getParentId()) || StringUtils.isEmpty(findById.getParentId())) {
resultList = stationInfRelaTreeDao.treeAllList(stationTelecode);
clearChildren(resultList);
} else {
resultList = stationInfRelaTreeDao.treeList(findById.getRelaTreeId(), stationTelecode);
clearChildren(resultList);
}
return resultList;
}
//树的列表展示清除子节点方法
private List<StationInfRelaTree> clearChildren(List<StationInfRelaTree> resultList) {
for (StationInfRelaTree collects : resultList) {
if (collects.getChildren() != null && !collects.getChildren().isEmpty()) {
collects.getChildren().clear();
}
}
return resultList;
}
public List<StationInfRelaTree> listByParentId(String id, String stationTelecode) {
List<StationInfRelaTree> resultList = null;
if ("".equals(id) || id == null) {
resultList = stationInfRelaTreeDao.treeAllList(stationTelecode);
if (resultList.size() > 0) {
StationInfRelaTree stationInfRelaTree = resultList.get(0);
Set<StationInfRelaTree> children = stationInfRelaTree.getChildren();
resultList = new ArrayList<>(children);
clearChildren(resultList);
}
return resultList;
}
String val = "-1";
StationInfRelTree findById = InfRelTreeDao.findNoteById(id, stationTelecode);
if (val.equals(findById.getParentId()) || StringUtils.isEmpty(findById.getParentId())) {
resultList = stationInfRelaTreeDao.treeAllList(stationTelecode);
clearChildren(resultList);
} else {
resultList = stationInfRelaTreeDao.treeList(findById.getRelaTreeId(), stationTelecode);
clearChildren(resultList);
}
return resultList;
}
//树的列表展示清除子节点方法
private List<StationInfRelaTree> clearChildren(List<StationInfRelaTree> resultList) {
for (StationInfRelaTree collects : resultList) {
if (collects.getChildren() != null && !collects.getChildren().isEmpty()) {
collects.getChildren().clear();
}
}
return resultList;
}
4. Dao:
@Query("select t from StationInfRelaTree t where t.parent = '1' and t.stationCode = :stationCode")
List<StationInfRelaTree> treeAllList(@Param("stationCode") String stationTelecode);
List<StationInfRelaTree> treeAllList(@Param("stationCode") String stationTelecode);
@Query("select t from StationInfRelTree t where t.id = :id and t.stationCode = :stationCode")
StationInfRelTree findNoteById(@Param("id") String id, @Param("stationCode") String stationTelecode);
StationInfRelTree findNoteById(@Param("id") String id, @Param("stationCode") String stationTelecode);
@Query("select t from StationInfRelaTree t where t.parent = '1' and t.stationCode = :stationCode")
List<StationInfRelaTree> treeAllList(@Param("stationCode") String stationTelecode);
List<StationInfRelaTree> treeAllList(@Param("stationCode") String stationTelecode);
@Query(value = "select * from b_stationinfrelatree t where t.parent_id = :relaTreeId and t.station_code = :stationCode", nativeQuery = true)
List<StationInfRelaTree> treeList(@Param("relaTreeId") String relaTreeId, @Param("stationCode") String stationTelecode);
List<StationInfRelaTree> treeList(@Param("relaTreeId") String relaTreeId, @Param("stationCode") String stationTelecode);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2022-04-18 idea2022 破解博客
2022-04-18 对数据库中某字段的数据进行截取
2022-04-18 idea总是弹出svn账户密码框 & Server SSL certificate verification failed