MySql遞迴查詢過程Hibernate實現

來源:互聯網
上載者:User

1、MySql遞迴查詢過程

表:

CREATE TABLE `torg` (
  `TorgID` varchar(50) NOT NULL,
  `TorgName` varchar(200) NOT NULL,
  `TorgCode` varchar(50) NOT NULL,
  `TorgRank` varchar(50) NOT NULL,
  `TorgFlag` varchar(5) NOT NULL,
  `TorgOrder` varchar(5) NOT NULL,
  `TorgPCode` varchar(50) NOT NULL,
  PRIMARY KEY (`TorgID`),
  UNIQUE KEY `Torg_PK` (`TorgID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

遞迴查詢過程:

CREATE DEFINER=`root`@`%` PROCEDURE `getOrgChildListByOrgPId`(in orgPId varchar(50))
BEGIN
    DECLARE sTemp VARCHAR(1000);
    DECLARE sTempChd VARCHAR(1000);

    #get org child Iplist
    SET sTemp = '#';
    SET sTempChd = orgPId;
   
    WHILE sTempChd is not null DO
         SET sTemp = concat(sTemp,',',sTempChd);
         SELECT group_concat(torgId) INTO sTempChd FROM torg where FIND_IN_SET(TorgPCode,sTempChd)>0;
    END WHILE;
    #SELECT getChildList(orgPId) into orgIdList ;

    # select all torg by orgIdList
    SET @str = CONCAT('select * from Torg where FIND_IN_SET(torgId,"', sTemp,'")');
    PREPARE stmt FROM @str;
    EXECUTE stmt;
   DEALLOCATE PREPARE stmt;

END

 

2、表對應Hibernate的xml檔案

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.macren.model.Torg" table="torg" catalog="lzproject">
        <id name="torgId" type="java.lang.String">
            <column name="TorgID" length="50" />
            <generator class="uuid.hex" />
        </id>
        <property name="torgName" type="java.lang.String">
            <column name="TorgName" length="200"   />
        </property>
        <property name="torgCode" type="java.lang.String">
            <column name="TorgCode" length="50"   />
        </property>
        <property name="torgRank" type="java.lang.String">
            <column name="TorgRank" length="50"   />
        </property>
        <property name="torgFlag" type="java.lang.String">
            <column name="TorgFlag" length="5"  />
        </property>
          <property name="torgOrder" type="java.lang.String">
            <column name="TorgOrder" length="5"   />
        </property>
        <property name="torgPcode" type="java.lang.String">
            <column name="TorgPCode" length="50"   />
        </property>
    </class>
   

<!--  sql procedure Mapping     -->
    <sql-query name="getOrgChildListFromOrgId" callable="true">
    <return alias="torg" class="com.macren.model.Torg">
     <return-property name="torgId" column="torgId"/>
     <return-property name="torgName" column="torgName"/>
     <return-property name="torgCode" column="torgCode" />
     <return-property name="torgRank" column="torgRank" />
     <return-property name="torgFlag" column="torgFlag" />
     <return-property name="torgOrder" column="torgOrder" />
     <return-property name="torgPcode" column="torgPcode" />
    </return>
    {call getOrgChildListByOrgPId(?)}
  </sql-query>
   
</hibernate-mapping>

3、Java sever端實現

 /**
  *
  * @param userId
  * @return
  * @throws ParseException
  */
 @SuppressWarnings("unchecked")
 public List<Torg> getOrgListForUserId(String userId) throws ParseException{
  try {
   // get user
   List<Torg> retList = new ArrayList<Torg>();
      // get local
   String torgId = getOrgIdByUserId(userId);

   // getOrgChildListByOrgPId
   Query query = getSession().getNamedQuery("getOrgChildListByOrgPId");
   query.setParameter(0, torgId);
   retList = query.list();
   
   return retList;
  } catch (RuntimeException re) {
      throw re;
  }
 }

 

4、參考

http://blog.csdn.net/shangtongwei/archive/2010/04/17/5496319.aspx

MySQL中進行樹狀所有子節點的查詢

http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/02/4142971.aspx

MYSQL--遞迴查詢

http://unix-cd.com/vc/www/26/2010-09/16547.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.