问题:
在项目开发过程中,需要对存在关联的数据库表中的数据以树形结构在页面上进行展示,通过SQL语句难以进行处理,需要在程序的业务层进行处理。
1、数据表
① city_groups表
说明:
city_groups表通过id
和 parent_id
进行自关联,parent_id
为 00000000-0000-0000-0000-000000000000
空UUID时,代表省份。
② persons表
说明:
persons表通过 group_id
和 city_groups表中的id
进行外关联。
③ phones表
说明:
phones表中的 person_id
和 persons表中的id
进行外关联。
2、核心代码
注意
:对于city_groups表查出来的数据需要根据parent_id做升序处理,因为先找到父级,后面的子级才能往父级中添加。
select * from city_groups order by parent_id asc
① TreeNode.java 封装数据,提供递归查找的方法
@Data
public class TreeNode {
private String key;
private String parentKey;
private String name;
private List<TreeNode> children = new ArrayList<>();
public static TreeNode findNode(List<TreeNode> nodes, String key) {
List<TreeNode> finds = new ArrayList<>();
findNode(nodes, key, finds);
return finds.size() != 0 ? finds.get(0) : null;
}
public static void findNode(List<TreeNode> nodes, String key, List<TreeNode> finds) {
for (TreeNode node : nodes) {
if (key.equals(node.getKey()))
finds.add(node);
findNode(node.getChildren(), key, finds);
}
}
}
② CityGroupImpl.java
@Service
public class CityGroupServiceImpl implements CityGroupService {
private CityGroupMapper groupMapper;
public CityGroupServiceImpl(CityGroupMapper groupMapper) {
this.groupMapper = groupMapper;
}
@Override
public List<TreeNode> getNodes() {
List<TreeNode> nodes = new ArrayList<>();
List<CityGroup> groups = groupMapper.getAll();
for(CityGroup group: groups) {
TreeNode node = toTreeNode(group);
TreeNode parent = TreeNode.findNode(nodes, node.getParentKey());
if (parent != null)
parent.getChildren().add(node);
else
nodes.add(node);
}
return nodes;
}
private TreeNode toTreeNode(CityGroup group) {
TreeNode node = new TreeNode();
node.setKey(group.getId());
node.setParentKey(group.getParentId());
node.setName(group.getName());
return node;
}
}
③ PersonServiceImpl.java
@Service
public class PersonServiceImpl implements PersonService {
private PersonMapper personMapper;
public PersonServiceImpl(PersonMapper personMapper) {
this.personMapper = personMapper;
}
@Override
public List<TreeNode> getNodes(List<TreeNode> nodes) {
List<Person> persons = personMapper.selectList(null);
for (Person person : persons) {
TreeNode node = toTreeNode(person);
TreeNode parent = TreeNode.findNode(nodes, node.getParentKey());
if (parent != null)
parent.getChildren().add(node);
else
nodes.add(node);
}
return nodes;
}
private TreeNode toTreeNode(Person person) {
TreeNode node = new TreeNode();
node.setKey(person.getId());
node.setParentKey(person.getGroupId());
node.setName(person.getName());
return node;
}
}
④ PhoneServiceImpl.java
@Service
public class PhoneServiceImpl implements PhoneService {
private PhoneMapper phoneMapper;
public PhoneServiceImpl(PhoneMapper phoneMapper) {
this.phoneMapper = phoneMapper;
}
@Override
public List<TreeNode> getNodes(List<TreeNode> nodes) {
List<Phone> phones = phoneMapper.selectList(null);
for (Phone phone : phones) {
TreeNode node = toTreeNode(phone);
TreeNode parent = TreeNode.findNode(nodes, node.getParentKey());
if (parent != null)
parent.getChildren().add(node);
else
nodes.add(node);
}
return nodes;
}
private TreeNode toTreeNode(Phone phone) {
TreeNode node = new TreeNode();
node.setKey(phone.getId());
node.setParentKey(phone.getPersonId());
node.setName(phone.getName());
return node;
}
}
⑤ PhoneController.java
@RestController
@RequestMapping("/phone")
public class PhoneController {
private PhoneService phoneService;
private PersonService personService;
private CityGroupService groupService;
public PhoneController(PersonService personService, CityGroupService groupService, PhoneService phoneService) {
this.phoneService = phoneService;
this.groupService = groupService;
this.personService = personService;
}
@GetMapping("/getNodes")
public List<TreeNode> getNodes() {
List<TreeNode> nodes = groupService.getNodes();
nodes = personService.getNodes(nodes);
nodes = phoneService.getNodes(nodes);
return nodes;
}
}