采用TinyDB組件方式開發
Icon 前文介紹四則運算的流程編程開發時,說過流程編排在開發重復功能時,可以利用已有的組件庫快速開發。對于開發人員而言只需要簡單配置流程就可以完成工作了。
開發增刪改查的組件接口。本來這部分很花費時間,如果采用組件復用的話,就可以實現一次開發,終生受益。
- 配置curd.beans.xml和tinydb.xml。
- 使用流程編輯器定制組件流程curd.pageflow。
- 修改頁面文件:list.page和operate.page,使之符合流程方式調用。
- 修改布局文件default.layout。
完整的子工程目錄如下:
首先是開發增刪改查的組件接口,如果是開發人員自己實現的話就是實現ComponentInterface接口,然后在組件里實現具體的數據庫邏輯,其實Tiny框架提供基礎組件庫,配合TinyDB進行開發,可以實現無Pojo,無Dao層,在本示例中開發人員不用寫一行Java代碼,通過配置就可以完成示例需求。
基礎組件資源
Icon目前Tiny框架提供如下幾種組件庫,用戶可以根據實際需要配合流程編輯器使用。
- org.tinygroup.flowbasiccomponent 提供了邏輯基本組件,如對象轉換組件、調用靜態方法和bean組件等。
- org.tinygroup.flowservicecomponent 提供了邏輯基本服務,如調用服務、調用本地流程、調用頁面流程等。
- org.tinygroup.tinydbflowcomponent 提供了TinyDB數據庫組件,如插入組件、更新組件、刪除組件、查詢記錄組件等。
- org.tinygroup.pageflowbasiccomponent 提供了頁面基本組件,如頁面重定向、頁面轉發等。
開發人員想要使用使用組件庫中的內置組件,必須將具體的依賴加入到工程的pom.xml,這樣在使用圖形化工具,在右側的組件列表就能看到組件清單:
本示例用到了數據庫訪問和頁面跳轉,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,然后按下圖拖曳組件:
接下來修改組件的基本信息:標識、英文名和中文名。以插入組件為例,鼠標選中畫板里的“插入組件”,在Eclipse的下方的“Properties”,就可以看到如下內容:
五個組件修改完畢,界面如下圖展示:
然后是配置組件的擴展屬性,每個組件的擴展屬性是根據自身功能定制的,具體的組件參數請參考各組件的幫助文檔。這里還是以“新增用戶”為例:
說明:這里類型就是數據庫表的值對象類型TUser,模式是指數據庫的schema,其他幾個組件也是類似。
配置好五個組件的擴展屬性,就是配置頁面組件的擴展屬性。頁面組件的擴展屬性就一個:頁面路徑。
頁面重定向的配置如下:
查詢單用戶對應的頁面轉發配置如下:
查詢用戶列表對應的頁面轉發配置如下:
完整的curd.pageflow的內容如下:
- <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代碼如下:
- <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代碼如下:
- 用戶管理界面:
- <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的配置如下:
- <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>
到這一步,流程編排的例子算是開發完畢。
演示效果
具體的增刪改查效果,用戶可以根據教程自行嘗試。
歡迎訪問開源技術社區:http://bbs.tinygroup.org。本例涉及的代碼和框架資料,將會在社區分享?!蹲约簞邮謱懣蚣堋烦蓡TQQ群:228977971,一起動手,了解開源框架的奧秘!或點擊加入QQ群:http://jq.qq.com/?_wv=1027&k=d0myfX