很多时候,我们会把一棵树存放到数据库中,当前台需要展示一个树形列表时,将这棵树读取出来并显示,这个过程是怎么实现的呢?

这篇文章是以构造一棵easyui前台框架的一个树形列表为例,后台框架是spring MVC+JPA。

首先看一下数据库中这颗树是怎么存的:

树的结构一目了然,这是一棵表示部门的树。

下面是实体类:

/**
* 部门类 用户所属部门(这里的部门是一个相对抽象的词)
* 使用前缀编码,每级增加三个数字,如:第一级 001,第二级001001,第三级001001001
* @author Administrator
*
*/
@Entity
public class Department { private String id; //部门id
private String pid; //父id
private String text; //部门名称
private List<Department> children; //用于存储子节点
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
} public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@OneToMany
public List<Department> getChildren() {
return children;
}
public void setChildren(List<Department> children) {
this.children = children;
} }

由于使用的是easyui ,为了树可以正确显示,必须包含属性id和text。

下面是控制类,用于读取数据库中的数据,生成一个List,该list返回到前台时会转成json格式的数据,重点需要理解的就是getTree()和buildTree()这两个方法,通过递归生成这棵树,基本实现原理是遍历树的某一层,获取每个节点的子节点们(一个list),然后将它作为父节点的一个属性set进去

@Controller
@RequestMapping("/department")
public class DepartmentController { private static final String DEPARTEMNTMANAGE = "module/jsp/system/departmentManage.jsp"; @Resource(name="departmentServiceImpl")
private DepartmentService departmentService;
/**
* 获取部门管理页面.
* @return
*/
@RequestMapping("/getPage.do")
public String getUserManagePage(){
return DEPARTEMNTMANAGE;
} /**
* 获取部门树形列表
*/
@RequestMapping(value = "/getTree.do", method = RequestMethod.POST)
@ResponseBody //此注解表明返回值跳过视图处理部分,直接写入 http response body中
public List<Department> getTree(){
List<Department> root = departmentService.findById("0"); //获取根节点(获取的值存到list中)
net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(buildTree(root));
System.out.println(jsonArray.toString()); return buildTree(root);
}
public List<Department> buildTree(List<Department> root){
for(int i=0;i<root.size();i++){
List<Department> children = departmentService.findByPid(root.get(i).getId()); //查询某节点的子节点(获取的是list)
buildTree(children);
root.get(i).setChildren(children);
}
return root;
} }

这里用查询的方法findById和findByPid是基于spring data JPA的方法,下面是相关类:

public interface DepartmentDao extends Repository<Department, Integer>{
public List<Department> findByPid(String pid);
public List<Department> findById(String id);
}
public interface DepartmentService {
public List<Department> findByPid(String pid);
public List<Department> findById(String id);
}
@Service("departmentServiceImpl")
public class DepartmentServiceImpl implements DepartmentService{ @Autowired
private DepartmentDao departmentDao; @Override
public List<Department> findByPid(String pid) {
return departmentDao.findByPid(pid);
} @Override
public List<Department> findById(String id) {
return departmentDao.findById(id);
} }

前台调用如下:

 $(function(){
$('#tt').tree({
url: 'department/getTree.do',
loadFilter: function(data){
if (data.d){
return data.d;
} else {
return data;
}
}
});
});

这是后台返回给前台一个如下的json格式的数据:

[
{
"children": [
{
"children": [
{
"children": [],
"id": "001001",
"pid": "001",
"text": "部门一"
},
{
"children": [],
"id": "001002",
"pid": "001",
"text": "部门二"
}
],
"id": "001",
"pid": "0",
"text": "一分公司"
},
{
"children": [
{
"children": [],
"id": "002001",
"pid": "002",
"text": "部门一"
},
{
"children": [],
"id": "002002",
"pid": "002",
"text": "部门二"
}
],
"id": "002",
"pid": "0",
"text": "二分公司"
},
{
"children": [],
"id": "003",
"pid": "0",
"text": "三分公司"
}
],
"id": "0",
"pid": "-1",
"text": "总公司"
}
]

在页面上如下显示:



大致就是这样实现的,如果觉得小编写的不明白可以加Q探讨或在下边回复

版权声明:本文为博主原创文章,未经博主允许不得转载。

如何将数据库中存的树转化为树形列表(以easyui的tree为例)的更多相关文章

  1. 数据库中树形列表(以easyui的tree为例)

    构造一棵easyui前台框架的一个树形列表为例后台框架是spring MVC+JPA. 先看一下数据库是怎么建的,怎么存放的数据 下面是实体类 /** * 部门类 用户所属部门(这里的部门是一个相对抽 ...

  2. 数据库中的B树和B+树

    B树与B+树 数据库中建立索引能加快数据的存取,但是当索引变得很大时,可能导致内存装不下.这时就需要使用多级索引来实现.而B树和B+树是实现多级索引的一种数据结构. B树 B树是多叉树,其树中每个节点 ...

  3. 如何将Sql server数据库中的模型图转化到Word中--并能够查看字段的属性信息

    1. 在Sql server数据库中创建数据库的模型图 -- Database Diagrams 2. 控制面板--管理工具--ODBC数据源链接--创建一个Sql server的数据源链接 3. 打 ...

  4. 从数据库中导出excel报表

    通常需要将后台数据库中的数据集或者是其他列表等导出excel 报表,这里主要引用了Apose.cells dll 类库, (1)直接上主要代码: protected void txtExport_Cl ...

  5. ASP.NET中让图片以二进制的形式存储在数据库中

    今早有个网友问到我这问题,以前我都是直接在数据库中存文件名的,还没有试过存储整张图片到数据库中,上网搜索了一下,自己又测试了一番,代码如下:建立保存图片的表的SQL语句: USE [niunantes ...

  6. 将数据库中的内容展示出来并将某些value值转换成汉字

    1.将数据库中的内容展示出来 前台代码未做改变,刚开始未显示的原因是因为 data-field 跟数据库不一样data-field 需要跟数据库中的一样才可以 2.将某些value值转换成汉字 在li ...

  7. mybatis查询mysql 数据库中 BLOB字段,结果出现乱码

    起因 mybatis-plus 通过Mapper 查询数据,映射出来的BLOB字段中的yml数据中文是乱码的 --- DefaultValue: '' Formula: '' HintContent: ...

  8. C#将图片存放到SQL SERVER数据库中的方法

    本文实例讲述了C#将图片存放到SQL SERVER数据库中的方法.分享给大家供大家参考.具体如下: 第一步: ? 1 2 3 4 5 6 7 8 9 10 //获取当前选择的图片 this.pictu ...

  9. Java实现购物车功能:方式一:存放在session中.方式二:存储在数据库中

    //将购物车产品加入到cookie中,方式同浏览记录.Java实现购物车,方式一(简易版):存储在session中.这种方式实现还不严谨,大家看的时候看思路即可.(1). JSP页面中,选择某一款产品 ...

随机推荐

  1. 链路层 - SLIP,PPP,

    最常使用的封装格式是RFC 894定义的格式.图2 - 1显示了两种不同形式的封装格式.图中每个方框下面的数字是它们的字节长度. 两种帧格式都采用48 bit(6字节)的目的地址和源地址( 8 0 2 ...

  2. 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题

    分装库下载:https://github.com/facebook/FBMemoryProfiler FBMemoryProfiler类库使用教程:http://ifujun.com/fbmemory ...

  3. How to configure ODBC DSN in Client to access remote DB2 for Windows

      How to configure ODBC DSN in Client to access remote DB2 for Windows MA Gen feng (Guangdong Unito ...

  4. 使用kubeadm搭建Kubernetes(1.10.2)集群(国内环境)

    目录 目标 准备 主机 软件 步骤 (1/4)安装 kubeadm, kubelet and kubectl (2/4)初始化master节点 (3/4) 安装网络插件 (4/4)加入其他节点 (可选 ...

  5. 布局display属性(一)--【Flex】

    一.Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 Flex 布局. .box ...

  6. JAVA全套学习视频

    链接: https://pan.baidu.com/s/1miE7kvQ 密码: jj8x

  7. plsql使用

    本文由jay8605162432贡献 本课重点: 1.写 SELECT 语句进行数据库查询 2.进行数学运算 3.处理空值 4.使用别名 ALIASES 5.连接列 6.在 SQL PLUS 中编辑缓 ...

  8. InnoDB的4个特性

    innodb 的四个特性 insert buffer innodb使用insert buffer"欺骗"数据库:对于为非唯一索引,辅助索引的修改操作并非实时更新索引的叶子页,而是把 ...

  9. Failed building wheel for scandir 解决方案

    unbuntu 16.04 运行 pip install jupyter --upgrade 的时候出现了下面的错误 Failed building wheel for scandir Running ...

  10. ecshop 修改记录20150710

    ecshop 修改记录20150710 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop 前台部分:在后台商店设置 - 商店标题修改 后者打开includes/lib ...