在Java中, 使用Mybatis Plus查询包含'test'的所有数据的所有父数据和子数据并存入数组中, 可以使用递归查询实现。
首先, 使用Mybatis Plus查询包含'test'的所有数据。
List<Public> list = publicMapper.selectList(new QueryWrapper<Public>().like("name", "test"));
然后遍历查询出来的数据, 对于每个数据, 递归查询其父节点和子节点, 并将结果存入数组中。
List<Public> result =new ArrayList<>();
for (Public p : list) {
result.add(p);
getParent(p, result);
getChild(p, result);
}
private void getParent(Public p, List<Public> result) {
if (p.getParentid() != null) {
Public parent = publicMapper.selectById(p.getParentid());
result.add(parent);
getParent(parent, result);
}
}
private void getChild(Public p, List<Public> result) {
List<Public> childList = publicMapper.selectList(new QueryWrapper<Public>().eq("parentid", p.getId()));
if (!childList.isEmpty()) {
result.addAll(childList);
for (Public child : childList) {
getChild(child, result);
}
}
}
最后得到的result就是所有符合条件的父子节点. 需要注意的是,这个例子中的父子关系是单向的,如果需要双向查询还需要在getParent函数中加上对子节点的查询。