递归、嵌套for循环、map集合⽅式实现树形结构菜单列表查询有时候,我们需要⽤到菜单列表,但是怎么样去实现⼀个菜单列表的编写呢,这是⼀重要的问题。
⽐如我们需要编写⼀个树形结构的菜单,那么我们可以使⽤JQuery的zTree插件:
例如现在需要编写⼀个这样的菜单列表。那么就可以使⽤JQuery的zTree插件。
先看⼀下数据库表结构。
CREATE TABLE `permission` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`pid` int(11) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
1、前端页⾯。
<div class="panel-body">
<ul id="permissionTree" class="ztree"></ul>
</div>
<script>
ssm框架主要代码在哪里$(function () {
var setting = {
async: {
enable: true,
url:"${APP_PATH}/permission/loadData",
autoParam:["id", "name=n", "level=lv"],
otherParam:{"otherParam":"zTreeAsyncTest"},
}
};
//树形结构
$.fn.zTree.init($("#permissionTree"), setting);
});
</script>
2、在这⾥呢,我使⽤的是ssm框架。所以就提交到controller层来进⾏操作。Permission类的代码
1import java.util.ArrayList;
2import java.util.List;
3
4public class Permission {
5
6private Integer id;
7private String name;
8private String url;
9private Integer pid;
10private boolean open = true;
11private boolean checked = false;
12private String icon;
13private List<Permission> children = new ArrayList<Permission>();
14
15public boolean isChecked() {
16return checked;
17 }
18public void setChecked(boolean checked) {
19this.checked = checked;
20 }
21public String getIcon() {
22return icon;
23 }
24public void setIcon(String icon) {
25this.icon = icon;
26 }
27public Integer getId() {
28return id;
29 }
30public void setId(Integer id) {
31this.id = id;
32 }
33public String getName() {
34return name;
35 }
36public void setName(String name) {
37this.name = name;
38 }
39public String getUrl() {
40return url;
41 }
42public void setUrl(String url) {
43this.url = url;
44 }
45public Integer getPid() {
46return pid;
47 }
48public void setPid(Integer pid) {
49this.pid = pid;
50 }
51public boolean isOpen() {
52return open;
53 }
54public void setOpen(boolean open) {
55this.open = open;
56 }
57public List<Permission> getChildren() {
58return children;
59 }
60public void setChildren(List<Permission> children) {
61this.children = children;
62 }
63
64 }
使⽤controller这⾥我使⽤三种⽅式来进⾏查。
(1)递归⽅式读取各节点
controller层
* 异步加载树结点
* @return
*/
@RequestMapping("/loadData")
@ResponseBody
public List<Permission> loadData(){
// 递归查询数据
Permission parent = new Permission();
parent.setId(0);
queryChildPermissions(parent);
Children();
}
/**
* 递归查询许可信息
* 1)⽅法⾃⼰调⽤⾃⼰
* 2)⽅法⼀定要存在跳出逻辑
* 3)⽅法调⽤时,参数之间应该有规律
* 4)递归算法,效率⽐较低
* @param parent
*/
private void queryChildPermissions(Permission parent){
List<Permission> childPermissions = permissionService.Id());
for (Permission permission :childPermissions) {
queryChildPermissions(permission);
}
parent.setChildren(childPermissions);
}
service层
public Permission queryRootPermission() {
return permissionMapper.queryRootPermission();
}
mappern层
@Select("select * from permission where pid is null")
Permission queryRootPermission();
(2)嵌套for循环⽅式读取各节点
controller层
1/**
2 * 异步加载树结点
3 * @return
4*/
5 @RequestMapping("/loadData")
6 @ResponseBody
7public List<Permission> loadData(){
8 List<Permission> permissions=new ArrayList<Permission>();
9// 查询所有的许可数据
10 List<Permission> ps = permissionService.queryAll();
11for ( Permission p : ps ) {
12// ⼦节点
13 Permission child = p;
14if ( p.getPid() == 0 ) {
15 permissions.add(p);
16 } else {
17for ( Permission innerPermission : ps ) {
18if ( Pid().Id()) ) {
19// ⽗节点
20 Permission parent = innerPermission;
21// 组合⽗⼦节点的关系
22 Children().add(child);
23break;
24 }
25 }
26 }
27 }
29
30return permissions;
31 }
service层
public List<Permission> queryAll() {
return permissionMapper.queryAll();
}
mapper层
@Select("select * from permission")
List<Permission> queryAll();
(3)map集合⽅式读取各节点
controller层
/**
* 异步加载树结点
* @return
*/
@RequestMapping("/loadData")
@ResponseBody
public List<Permission> loadData(){
List<Permission> permissions=new ArrayList<Permission>();
// 查询所有的许可数据
List<Permission> ps = permissionService.queryAll();
Map<Integer, Permission> permissionMap = new HashMap<Integer, Permission>();
for (Permission p : ps) {
permissionMap.Id(), p);
}
for ( Permission p : ps ) {
Permission child = p;
if ( Pid() == 0 ) {
permissions.add(p);
} else {
Permission parent = (Pid());
}
}
return permissions;
}
service层
public List<Permission> queryAll() {
return permissionMapper.queryAll();
}
mapper层
@Select("select * from permission")
List<Permission> queryAll();
三种⽅法的总结:
使⽤递归会使数据库查询⾮常频繁,不能进⾏性能优化
使⽤嵌套for循环没有使⽤索引,不够快速
使⽤map集合就使⽤了索引,加快查询速度。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论