sql server 遍历表成一棵树结构

一棵树的层次结构都在一张表内,当有这样的需要的时候。。

可以这样玩:

复制代码
<!-- DepartmentDTO 对象对应 department表_查询sql -->
 <sql id="department_select_sql">
  with ldepartment as (
    select
     dept_id,
     parent_id,
     0 as dept_level,
     row_number()over(order by getdate()) as orderid
    from
     department
    where
     (
      parent_id is null
      or parent_id = ''
     )
    union all
     select
      a.dept_id,
      a.parent_id,
      b.dept_level + 1 as dept_level,
      b.orderid*100+row_number()over(order by getdate()) as orderid  
     from
      department a,
      ldepartment b
     where
      a.parent_id = b.dept_id
   ) select
   t1.dept_level,
   t1.orderid,
   t2.paic_unique_deptid,
   t2.deptid_descr,
   t2.parent_id,
   (select aa.deptid_descr + '('+aa.dept_id+')' from department aa where aa.dept_id=t2.parent_id ) as parent_id_desc,
   t2.dept_id,
   t2.ou_type,
   t2.date_created,
   t2.created_by,
   t2.date_updated,
   t2.updated_by,
   t2.row_id
  from ldepartment t1, department t2
  where t1.dept_id = t2.dept_id
  <isNotEmpty prepend="and" property="deptid_descr">
   t2.deptid_descr like '%+#deptid_descr#+%'
  </isNotEmpty>
  <isNotEmpty prepend="and" property="parent_id">
   t2.parent_id = #parent_id#
  </isNotEmpty>

order by ltrim(t1.orderid) // 关键点(字符串排序)
 </sql>
复制代码

 

调用:

<select id="department_find" parameterClass="java.util.Map"
        resultClass="com.pasc.supms.parameter.dto.DepartmentDTO">
        <include refid="department_select_sql" />
    </select>


 

java对象:

复制代码
public class DepartmentDTO extends SupmsBaseDTO {

    private String paic_unique_deptid; // 部门唯一编号
    private String deptid_descr; // 部门名称
    private String parent_id; // 上级部门编号
    private String parent_id_desc; // 上级部门名称编号
    private String dept_id; // 部门属主编号
    private String ou_type; // 部门类型
    private String dept_level; // 部门层级
复制代码

 

jsp页面:

复制代码
<table cellpadding="0" cellspacing="0" class="table_list_2" >
            <thead align="center">
                <tr>
                    <th >部门编号</th>
                    <th >部门名称</th>
                    <th >上级部门编号</th>
                    <th >部门类型</th>
                </tr>
            </thead>
            <tbody align="center">
                <c:choose>
                    <c:when test="${not empty  pageBean.resultList}">
                            <c:forEach var="doc" items="${pageBean.resultList}" varStatus="i">    
                                <tr>
                                    <td><!-- <a href="#" onclick="detailDo('${doc.row_id}');" title="点击查看详情" class="blue"></a> -->
                                    <c:forEach begin="1" end="${doc.dept_level }">--&nbsp;</c:forEach>
                                    ${doc.dept_id }</td>
                                    <td>${doc.deptid_descr }</td>
                                    <td>${doc.parent_id_desc }</td>
                                    <td>${doc.ou_type }</td>
                                </tr>
                                </c:forEach>
                        </c:when>
                        <c:otherwise>
                            <tr id="noList">
                                <td colspan="11" align="center">对不起,暂时还没有记录!</td>
                            </tr>
                        </c:otherwise>
                </c:choose>
            </tbody>
        </table>
复制代码

 

结果:

posted on   陈惟鲜的博客  阅读(920)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示