《開源框架那點事兒23》:採用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)
首先是開發增刪改查的元件介面,如果是開發人員自己實現的話就是實現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)
本示例用到了資料庫訪問和頁面跳轉,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)
接下來修改元件的基本資訊:標識、英文名和中文名。以插入元件為例,滑鼠選中畫板裡的“插入元件”,在Eclipse的下方的“Properties”,就可以看到如下內容:
<ignore_js_op>
Unnamed%20QQ%20Screenshot20141113145248.jpg (17.32 KB, 下載次數: 0)
五個元件修改完畢,介面如下圖展示:
<ignore_js_op>
Unnamed%20QQ%20Screenshot20141113145525.jpg (8.65 KB, 下載次數: 0)
然後是配置元件的擴充套件屬性,每個元件的擴充套件屬性是根據自身功能定製的,具體的元件引數請參考各元件的幫助文件。這裡還是以“新增使用者”為例:
<ignore_js_op>
Unnamed%20QQ%20Screenshot20141113150154.jpg (7.42 KB, 下載次數: 0)
說明:這裡型別就是資料庫表的值物件型別TUser,模式是指資料庫的schema,其他幾個元件也是類似。
配置好五個元件的擴充套件屬性,就是配置頁面元件的擴充套件屬性。頁面元件的擴充套件屬性就一個:頁面路徑。
頁面重定向的配置如下:
<ignore_js_op>
Unnamed%20QQ%20Screenshot20141113150706.jpg (5.93 KB, 下載次數: 0)
查詢單使用者對應的頁面轉發配置如下:
<ignore_js_op>
Unnamed%20QQ%20Screenshot20141113150811.jpg (5.42 KB, 下載次數: 0)
查詢使用者列表對應的頁面轉發配置如下:
<ignore_js_op>
Unnamed%20QQ%20Screenshot20141113151009.jpg (5.53 KB, 下載次數: 0)
完整的curd.pageflow的內容如下:
[html] view plaincopyprint?- <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>
<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?- <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>
<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?- 使用者管理介面:
- <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>
使用者管理介面:
<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?- <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>
<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)
具體的增刪改查效果,使用者可以根據教程自行嘗試。