1. 程式人生 > >《開源框架那點事兒23》:採用TinyDB元件方式開發

《開源框架那點事兒23》:採用TinyDB元件方式開發

採用TinyDB元件方式開發 步驟

Icon 前文介紹四則運算的流程程式設計開發時,說過流程編排在開發重複功能時,可以利用已有的元件庫快速開發。對於開發人員而言只需要簡單配置流程就可以完成工作了。 開發增刪改查的元件介面。本來這部分很花費時間,如果採用元件複用的話,就可以實現一次開發,終生受益。

  • 配置curd.beans.xml和tinydb.xml。
  • 使用流程編輯器定製元件流程curd.pageflow。
  • 修改頁面檔案:list.page和operate.page,使之符合流程方式呼叫。
  • 修改佈局檔案default.layout。
    完整的子工程目錄如下: <ignore_js_op>

Unnamed%20QQ%20Screenshot20141113141022.jpg (11.16 KB, 下載次數: 0)

下載附件

1 秒前 上傳

首先是開發增刪改查的元件介面,如果是開發人員自己實現的話就是實現ComponentInterface介面,然後在元件裡實現具體的資料庫邏輯,其實Tiny框架提供基礎元件庫,配合TinyDB進行開發,可以實現無Pojo,無Dao層,在本示例中開發人員不用寫一行Java程式碼,通過配置就可以完成示例需求。
基礎元件資源

Icon目前Tiny框架提供如下幾種元件庫,使用者可以根據實際需要配合流程編輯器使用。

  • org.tinygroup.flowbasiccomponent          提供了邏輯基本元件,如物件轉換元件、呼叫靜態方法和bean元件等。
  • org.tinygroup.flowservicecomponent       提供了邏輯基本服務,如呼叫服務、呼叫本地流程、呼叫頁面流程等。
  • org.tinygroup.tinydbflowcomponent         提供了TinyDB資料庫元件,如插入元件、更新元件、刪除元件、查詢記錄元件等。
  • org.tinygroup.pageflowbasiccomponent    提供了頁面基本元件,如頁面重定向、頁面轉發等。
    開發人員想要使用使用元件庫中的內建元件,必須將具體的依賴加入到工程的pom.xml,這樣在使用圖形化工具,在右側的元件列表就能看到元件清單: <ignore_js_op>

Unnamed%20QQ%20Screenshot20141113143246.jpg (15.92 KB, 下載次數: 0)

下載附件

1 秒前 上傳

本示例用到了資料庫訪問和頁面跳轉,pom.xml裡面需要有如下依賴:

<dependency>    <groupId>org.tinygroup</groupId>     <artifactId>org.tinygroup.tinydbflowcomponent</artifactId>     <version>${tiny_version}</version>   </dependency> <dependency>    <groupId>org.tinygroup</groupId>    <artifactId>org.tinygroup.pageflowbasiccomponent</artifactId>    <version>${tiny_version}</version> < /dependency>

配置crud.beans.xml可以複用先前TinyDB採用服務方式的配置檔案,只需要把如下內容刪除:

<bean id="tinyDbCrudService" class="org.tinygroup.crud.service.impl.TinyDbCrudService" scope="singleton"> <property name="beanType" value="TUser" /> < /bean>

因為本示例並沒有配置tinyDbCrudService,如果不刪除的話,Web應用啟動時會報異常。至於tinydb.xml檔案無需任何修改,可以直接複用先前例子。
接下來按“New”-“Other”-“Tiny框架”-“頁面流程”順序,建立crud.pageflow,然後按下圖拖曳元件: <ignore_js_op>

Unnamed%20QQ%20Screenshot20141113144524.jpg (8.45 KB, 下載次數: 0)

下載附件

1 秒前 上傳

接下來修改元件的基本資訊:標識、英文名和中文名。以插入元件為例,滑鼠選中畫板裡的“插入元件”,在Eclipse的下方的“Properties”,就可以看到如下內容:

<ignore_js_op>

Unnamed%20QQ%20Screenshot20141113145248.jpg (17.32 KB, 下載次數: 0)

下載附件

1 秒前 上傳

五個元件修改完畢,介面如下圖展示:

<ignore_js_op>

Unnamed%20QQ%20Screenshot20141113145525.jpg (8.65 KB, 下載次數: 0)

下載附件

1 秒前 上傳

然後是配置元件的擴充套件屬性,每個元件的擴充套件屬性是根據自身功能定製的,具體的元件引數請參考各元件的幫助文件。這裡還是以“新增使用者”為例:

<ignore_js_op>

Unnamed%20QQ%20Screenshot20141113150154.jpg (7.42 KB, 下載次數: 0)

下載附件

1 秒前 上傳

說明:這裡型別就是資料庫表的值物件型別TUser,模式是指資料庫的schema,其他幾個元件也是類似。
配置好五個元件的擴充套件屬性,就是配置頁面元件的擴充套件屬性。頁面元件的擴充套件屬性就一個:頁面路徑。
頁面重定向的配置如下:

<ignore_js_op>

Unnamed%20QQ%20Screenshot20141113150706.jpg (5.93 KB, 下載次數: 0)

下載附件

1 秒前 上傳

查詢單使用者對應的頁面轉發配置如下:

<ignore_js_op>

Unnamed%20QQ%20Screenshot20141113150811.jpg (5.42 KB, 下載次數: 0)

下載附件

1 秒前 上傳

查詢使用者列表對應的頁面轉發配置如下:

<ignore_js_op>

Unnamed%20QQ%20Screenshot20141113151009.jpg (5.53 KB, 下載次數: 0)

下載附件

1 秒前 上傳

完整的curd.pageflow的內容如下:

[html] view plaincopyprint?
  1. <flow id="crud" enable="true" private-context="false"
  2.   <parameters/> 
  3.   <nodes
  4.     <node id="start" name="start" title="開始"
  5.       <next-nodes
  6.         <next-node next-node-id="addUser"/> 
  7.         <next-node next-node-id="updateUser"/> 
  8.         <next-node next-node-id="deleteUserById"/> 
  9.         <next-node next-node-id="getUserById"/> 
  10.         <next-node next-node-id="redirectComponent"/> 
  11.         <next-node next-node-id="queryUsers"/> 
  12.       </next-nodes
  13.     </node
  14.     <node id="addUser" name="addUser" title="新增使用者"
  15.       <component name="tinydbAddService" title="插入元件"
  16.         <properties
  17.           <flow-property name="beanType" value="TUser"/> 
  18.           <flow-property name="schema" value="sample"/> 
  19.         </properties
  20.       </component
  21.       <next-nodes
  22.         <next-node next-node-id="redirectComponent"/> 
  23.       </next-nodes
  24.     </node
  25.     <node id="updateUser" name="updateUser" title="更新使用者"
  26.       <component name="tinydbUpdateService" title="更新元件"
  27.         <properties
  28.           <flow-property name="beanType" value="TUser"/> 
  29.           <flow-property name="schema" value="sample"/> 
  30.         </properties
  31.       </component
  32.       <next-nodes
  33.         <next-node next-node-id="redirectComponent"/> 
  34.       </next-nodes
  35.     </node
  36.     <node id="deleteUserById" name="deleteUserById" title="刪除使用者"
  37.       <component name="tinydbDeleteService" title="刪除元件"
  38.         <properties
  39.           <flow-property name="beanType" value="TUser"/> 
  40.           <flow-property name="schema" value="sample"/> 
  41.         </properties
  42.       </component
  43.       <next-nodes
  44.         <next-node next-node-id="redirectComponent"/> 
  45.       </next-nodes
  46.     </node
  47.     <node id="getUserById" name="getUserById" title="查詢單使用者"
  48.       <component name="tinydbQueryServiceWithId" title="單記錄查詢元件"
  49.         <properties
  50.           <flow-property name="beanType" value="TUser"/> 
  51.           <flow-property name="schema" value="sample"/> 
  52.           <flow-property name="primaryKey" value="${primaryKey}"/> 
  53.           <flow-property name="resultKey" value="user"/> 
  54.         </properties
  55.       </component
  56.       <next-nodes
  57.         <next-node next-node-id="forwardComponent"/> 
  58.       </next-nodes
  59.     </node
  60.     <node id="forwardComponent" name="forwardComponent" title="頁面轉發"
  61.       <component name="forwardComponent" title="頁面轉發"
  62.         <properties
  63.           <flow-property name="path" value="/crud/operate.page"/> 
  64.         </properties
  65.       </component
  66.       <next-nodes
  67.         <next-node next-node-id="end"/> 
  68.       </next-nodes
  69.     </node
  70.     <node id="redirectComponent" name="redirectComponent" title="頁面重定向"
  71.       <component name="redirectComponent" title="頁面重定向"
  72.         <properties
  73.           <flow-property name="path" value="crud.pageflow?tiny_flow_id=queryUsers"/> 
  74.         </properties
  75.       </component
  76.       <next-nodes
  77.         <next-node next-node-id="end"/> 
  78.       </next-nodes
  79.     </node
  80.     <node id="queryUsers" name="queryUsers" title="查詢使用者列表"
  81.       <component name="tinydbQueryService" title="查詢元件"
  82.         <properties
  83.           <flow-property name="beanType" value="TUser"/> 
  84.           <flow-property name="schema" value="sample"/> 
  85.           <flow-property name="resultKey" value="users"/> 
  86.         </properties
  87.       </component
  88.       <next-nodes
  89.         <next-node next-node-id="forwardComponent_1"/> 
  90.       </next-nodes
  91.     </node
  92.     <node id="forwardComponent_1" name="forwardComponent" title="頁面轉發"
  93.       <component name="forwardComponent" title="頁面轉發"
  94.         <properties
  95.           <flow-property name="path" value="/crud/list.page"/> 
  96.         </properties
  97.       </component
  98.       <next-nodes
  99.         <next-node next-node-id="end"/> 
  100.       </next-nodes
  101.     </node
  102.     <node id="end" name="end" title="結束"
  103.       <next-nodes/> 
  104.     </node
  105.   </nodes
  106. < /flow
<flow id="crud" enable="true" private-context="false">
  <parameters/>
  <nodes>
    <node id="start" name="start" title="開始">
      <next-nodes>
        <next-node next-node-id="addUser"/>
        <next-node next-node-id="updateUser"/>
        <next-node next-node-id="deleteUserById"/>
        <next-node next-node-id="getUserById"/>
        <next-node next-node-id="redirectComponent"/>
        <next-node next-node-id="queryUsers"/>
      </next-nodes>
    </node>
    <node id="addUser" name="addUser" title="新增使用者">
      <component name="tinydbAddService" title="插入元件">
        <properties>
          <flow-property name="beanType" value="TUser"/>
          <flow-property name="schema" value="sample"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="redirectComponent"/>
      </next-nodes>
    </node>
    <node id="updateUser" name="updateUser" title="更新使用者">
      <component name="tinydbUpdateService" title="更新元件">
        <properties>
          <flow-property name="beanType" value="TUser"/>
          <flow-property name="schema" value="sample"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="redirectComponent"/>
      </next-nodes>
    </node>
    <node id="deleteUserById" name="deleteUserById" title="刪除使用者">
      <component name="tinydbDeleteService" title="刪除元件">
        <properties>
          <flow-property name="beanType" value="TUser"/>
          <flow-property name="schema" value="sample"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="redirectComponent"/>
      </next-nodes>
    </node>
    <node id="getUserById" name="getUserById" title="查詢單使用者">
      <component name="tinydbQueryServiceWithId" title="單記錄查詢元件">
        <properties>
          <flow-property name="beanType" value="TUser"/>
          <flow-property name="schema" value="sample"/>
          <flow-property name="primaryKey" value="${primaryKey}"/>
          <flow-property name="resultKey" value="user"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="forwardComponent"/>
      </next-nodes>
    </node>
    <node id="forwardComponent" name="forwardComponent" title="頁面轉發">
      <component name="forwardComponent" title="頁面轉發">
        <properties>
          <flow-property name="path" value="/crud/operate.page"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="end"/>
      </next-nodes>
    </node>
    <node id="redirectComponent" name="redirectComponent" title="頁面重定向">
      <component name="redirectComponent" title="頁面重定向">
        <properties>
          <flow-property name="path" value="crud.pageflow?tiny_flow_id=queryUsers"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="end"/>
      </next-nodes>
    </node>
    <node id="queryUsers" name="queryUsers" title="查詢使用者列表">
      <component name="tinydbQueryService" title="查詢元件">
        <properties>
          <flow-property name="beanType" value="TUser"/>
          <flow-property name="schema" value="sample"/>
          <flow-property name="resultKey" value="users"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="forwardComponent_1"/>
      </next-nodes>
    </node>
    <node id="forwardComponent_1" name="forwardComponent" title="頁面轉發">
      <component name="forwardComponent" title="頁面轉發">
        <properties>
          <flow-property name="path" value="/crud/list.page"/>
        </properties>
      </component>
      <next-nodes>
        <next-node next-node-id="end"/>
      </next-nodes>
    </node>
    <node id="end" name="end" title="結束">
      <next-nodes/>
    </node>
  </nodes>
< /flow>

操作頁面operate.page程式碼如下:

[html] view plaincopyprint?
  1. <form action="${TINY_CONTEXT_PATH}/crud.pageflow"
  2. 姓名:<input type="text" name="name" value="${user?.name}" /><br/> 
  3. 年齡:<input type="text" name="age" value="${user?.age}" /><br/> 
  4. <input type="hidden" name="id" value="${user?.id}"/> 
  5. #if($user) 
  6. <input type="hidden" name="tiny_flow_id" value="updateUser"/> 
  7. #else 
  8. <input type="hidden" name="tiny_flow_id" value="addUser"/> 
  9. #end 
  10. < input type="submit" value="提交"
  11. < /form
<form action="${TINY_CONTEXT_PATH}/crud.pageflow">
姓名:<input type="text" name="name" value="${user?.name}" /><br/>
年齡:<input type="text" name="age" value="${user?.age}" /><br/>
 <input type="hidden" name="id" value="${user?.id}"/>
 #if($user)
 <input type="hidden" name="tiny_flow_id" value="updateUser"/>
 #else
 <input type="hidden" name="tiny_flow_id" value="addUser"/>
 #end
< input type="submit" value="提交">
< /form>

列表頁面list.page程式碼如下:

[html] view plaincopyprint?
  1. 使用者管理介面: 
  2. <form
  3. < div
  4. <p
  5.   <input type="button" id="add" value="新增"/> 
  6.      <input type="button" id="update" value="修改"/> 
  7.      <input type="button" id="delete" value="刪除"/> 
  8. </p
  9. <table cellpadding="0" cellspacing="1" border="0" bgcolor="#ebebeb" width="500px"
  10.   <tbody
  11.    <tr bgcolor="#ffffff"
  12.     <th width="35"><input type="checkbox" id="selectAll"/></th
  13.     <th width="100px" height="35">名稱</th
  14.     <th width="365px" >年齡</th
  15.    </tr
  16.    #foreach($user in $users) 
  17.    <tr bgcolor="#ffffff"
  18.     <td align="center"><input type="checkbox" name="id" value="$user.id"/></td
  19.     <td align="center" height="30">$user.name</td
  20.     <td align="center">$user.age</td
  21.    </tr
  22.    #end 
  23.   </tbody
  24. </table
  25. < /div
  26. < form
  27. < script
  28. $(document).ready(function(){ 
  29.    $("#selectAll").click(function(){ 
  30.         var checked=$(this).get(0).checked; 
  31.         $(":checkbox:not('#selectAll')").each(function(){ 
  32.                $(this).get(0).checked=checked; 
  33.         }); 
  34.      }); 
  35.    $("#add").click(function(){ 
  36.        location.href="${TINY_CONTEXT_PATH}/crud/operate.page"; 
  37.     } 
  38.    ); 
  39.    $("#update").click(function(){ 
  40.     var checkboxs=$(":checked:not('#selectAll')"); 
  41.     var size=checkboxs.size(); 
  42.         if(size==0){ 
  43.            alert("修改前請先選擇記錄"); 
  44.         }else if(size>1){ 
  45.            alert("只能選擇一條記錄進行修改"); 
  46.         }else{ 
  47.          var checkbox=checkboxs.get(0); 
  48.          location.href="${TINY_CONTEXT_PATH}/crud.pageflow?primaryKey="+checkbox.value+"&tiny_flow_id=getUserById"; 
  49.         }  
  50.     } 
  51.    ); 
  52.     $("#delete").click(function(){ 
  53.         if(confirm("確定要刪除選擇的記錄嗎?")){ 
  54.               var checkboxs=$(":checked:not('#selectAll')"); 
  55.                var size=checkboxs.size(); 
  56.                if(size==0){ 
  57.                 alert("刪除前請先選擇記錄"); 
  58.                }else if(size>1){ 
  59.                  alert("只能選擇一條記錄進行刪除"); 
  60.                }else{ 
  61.               var checkbox=checkboxs.get(0); 
  62.              location.href="${TINY_CONTEXT_PATH}/crud.pageflow?id="+checkbox.value+"&tiny_flow_id=deleteUserById"; 
  63.              } 
  64.         } 
  65.     } 
  66.    ); 
  67. }); 
  68. < /script
使用者管理介面:
<form>
< div>
 <p>
  <input type="button" id="add" value="新增"/>
     <input type="button" id="update" value="修改"/>
     <input type="button" id="delete" value="刪除"/>
 </p>
 <table cellpadding="0" cellspacing="1" border="0" bgcolor="#ebebeb" width="500px">
  <tbody>
   <tr bgcolor="#ffffff">
    <th width="35"><input type="checkbox" id="selectAll"/></th>
    <th width="100px" height="35">名稱</th>
    <th width="365px" >年齡</th>
   </tr>
   #foreach($user in $users)
   <tr bgcolor="#ffffff">
    <td align="center"><input type="checkbox" name="id" value="$user.id"/></td>
    <td align="center" height="30">$user.name</td>
    <td align="center">$user.age</td>
   </tr>
   #end
  </tbody>
 </table>
< /div>
< form>
< script>
$(document).ready(function(){
   $("#selectAll").click(function(){
        var checked=$(this).get(0).checked;
        $(":checkbox:not('#selectAll')").each(function(){
               $(this).get(0).checked=checked;
        });
     });
   $("#add").click(function(){
       location.href="${TINY_CONTEXT_PATH}/crud/operate.page";
    }
   );
   $("#update").click(function(){
    var checkboxs=$(":checked:not('#selectAll')");
    var size=checkboxs.size();
        if(size==0){
           alert("修改前請先選擇記錄");
        }else if(size>1){
           alert("只能選擇一條記錄進行修改");
        }else{
         var checkbox=checkboxs.get(0);
         location.href="${TINY_CONTEXT_PATH}/crud.pageflow?primaryKey="+checkbox.value+"&tiny_flow_id=getUserById";
        } 
    }
   );
    $("#delete").click(function(){
        if(confirm("確定要刪除選擇的記錄嗎?")){
              var checkboxs=$(":checked:not('#selectAll')");
               var size=checkboxs.size();
               if(size==0){
                alert("刪除前請先選擇記錄");
               }else if(size>1){
                 alert("只能選擇一條記錄進行刪除");
               }else{
              var checkbox=checkboxs.get(0);
             location.href="${TINY_CONTEXT_PATH}/crud.pageflow?id="+checkbox.value+"&tiny_flow_id=deleteUserById";
             }
        }
    }
   );
});
< /script>

預設佈局檔案default.layout的配置如下:

[html] view plaincopyprint?
  1. <table border="1" width="100%"
  2.    <tr
  3.       <td colspan="2"
  4.         <a href="${TINY_CONTEXT_PATH}/crud.pageflow?tiny_flow_id=queryUsers">使用者管理</a><br/> 
  5.       </td
  6.     </tr
  7.     <tr>   
  8.       <td width="20%">tinydb流程方式</td
  9.       <td
  10.          ${pageContent} 
  11.       </td
  12.    </tr
  13. < /table
<table border="1" width="100%">
   <tr>
      <td colspan="2">
        <a href="${TINY_CONTEXT_PATH}/crud.pageflow?tiny_flow_id=queryUsers">使用者管理</a><br/>
      </td>
    </tr>
    <tr>  
      <td width="20%">tinydb流程方式</td>
      <td>
         ${pageContent}
      </td>
   </tr>
< /table>

到這一步,流程編排的例子算是開發完畢。 演示效果

Unnamed%20QQ%20Screenshot20141113152410.jpg (10.3 KB, 下載次數: 0)

下載附件

1 秒前 上傳

具體的增刪改查效果,使用者可以根據教程自行嘗試。