《开源框架那点事儿23》:採用TinyDB组件方式开发

採用TinyDB组件方式开发

步骤

Icon 前文介绍四则运算的流程编程开发时,说过流程编排在开发反复功能时。能够利用已有的组件库高速开发。对于开发者而言仅仅须要简单配置流程就能够完毕工作了。
开发增删改查的组件接口。本来这部分非常花费时间,假设採用组件复用的话,就能够实现一次开发,终生受益。

  • 配置curd.beans.xml和tinydb.xml。
  • 使用流程编辑器定制组件流程curd.pageflow。

  • 改动页面文件:list.page和operate.page。使之符合流程方式调用。
  • 改动布局文件default.layout。



    完整的子project文件夹例如以下:


首先是开发增删改查的组件接口,假设是开发者自己实现的话就是实现ComponentInterface接口,然后在组件里实现详细的数据库逻辑。事实上Tiny框架提供基础组件库,配合TinyDB进行开发。能够实现无Pojo,无Dao层。在本演示样例中开发者不用写一行Java代码,通过配置就能够完毕演示样例需求。

基础组件资源

Icon眼下Tiny框架提供例如以下几种组件库,用户能够依据实际须要配合流程编辑器使用。

  • org.tinygroup.flowbasiccomponent          提供了逻辑基本组件,如对象转换组件、调用静态方法和bean组件等。
  • org.tinygroup.flowservicecomponent       提供了逻辑基本服务,如调用服务、调用本地流程、调用页面流程等。
  • org.tinygroup.tinydbflowcomponent         提供了TinyDB数据库组件,如插入组件、更新组件、删除组件、查询记录组件等。
  • org.tinygroup.pageflowbasiccomponent    提供了页面基本组件。如页面重定向、页面转发等。

    开发者想要使用使用组件库中的内置组件,必须将详细的依赖增加到project的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。本例涉及的代码和框架资料,将会在社区分享。《自己动手写框架》成员QQ群:228977971,一起动手,了解开源框架的奥秘!

或点击增加QQ群:http://jq.qq.com/?_wv=1027&k=d0myfX

原文地址:https://www.cnblogs.com/lytwajue/p/6746876.html