手记

搭建Activiti5+Mybatis+Mysql+Spring+springmvc+freemarker项目

项目路径图

引用的jar包

页面及js,css文件

相关配置文件

//web.xml中的内容

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false" version="3.0"> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>classpath:applicationContext.xml</param-value> 
</context-param> 
<welcome-file-list> 
<welcome-file>view/index.html</welcome-file> 
</welcome-file-list>
 <listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener> 
 <filter> 
 <description>字符集过滤器</description>
  <filter-name>encodingFilter</filter-name> 
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
  <init-param> 
  <description>字符集编码</description> 
  <param-name>encoding</param-name> 
  <param-value>UTF-8</param-value>
   </init-param> </filter> 
   <filter-mapping> 
   <filter-name>encodingFilter</filter-name>
    <url-pattern>*</url-pattern> 
    </filter-mapping> 
    <servlet> 
    <servlet-name>springMvc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
    <description>spring mvc 配置文件</description> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:spring-mvc.xml
    </param-value>
     </init-param> 
     <load-on-startup>1</load-on-startup> 
     </servlet> <servlet-mapping>
      <servlet-name>springMvc</servlet-name>
       <url-pattern>*.do</url-pattern>
        </servlet-mapping> 
        </web-app>
//activiti.cfg.xml的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        ">

    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"/>
        <property name="transactionManager" ref="transactionManager"/>
        <property name="databaseSchemaUpdate" value="true"/>
        <property name="jobExecutorActivate" value="false"/>
        <property name="activityFontName" value="宋体"/>
        <property name="labelFontName" value="宋体"/>
    </bean>

    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    </bean>

    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>

</beans>
//applicationContext.xml的内容
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<aop:aspectj-autoproxy proxy-target-class="true" /> 
<!-- 自动扫描dao和service包(自动注入) --> 
<context:component-scan base-package="com.dao" /> 
<context:component-scan base-package="com.services" /> 
<context:component-scan base-package="com.entity" /> 
<context:component-scan base-package="com.util" /> 
<context:component-scan base-package="com.freemarker" /> 
<!-- freemarker的配置 --> 
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
<property name="templateLoaderPath" value="/" />
<property name="defaultEncoding" value="UTF-8" /> 
<property name="freemarkerSettings"> 
<props> 
<prop key="template_update_delay">10</prop> 
<prop key="locale">zh_CN</prop>
 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> 
 <prop key="date_format">yyyy-MM-dd</prop> 
 <prop key="number_format">#.##</prop> 
 </props> 
 </property> 
 </bean> <!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 --> 
 <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
 <property name="viewClass" value="com.freemarker.RichFreeMarkerView" /> 
 <property name="prefix" value="/view/" /> 
 <property name="suffix" value=".html" /> 
 <property name="contentType" value="text/html;charset=utf-8" /> 
 <property name="exposeRequestAttributes" value="true" /> 
 <property name="exposeSessionAttributes" value="true" /> 
 <property name="exposeSpringMacroHelpers" value="true" /> 
 </bean> 
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" />
  <!-- 添加注解驱动 --> 

  <!-- 连接池的配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass"     value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl"         value="jdbc:mysql://localhost:3306/acvititi" />
        <property name="user"            value="root" />
        <property name="password"        value="root" />
        <property name="maxPoolSize"     value="30" />
        <property name="minPoolSize"     value="5" />
        <property name="maxIdleTime"     value="60" />  
        <property name="initialPoolSize" value="10" />
    </bean>
 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
 <property name="dataSource" ref="dataSource" /> 
 </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> 
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
 <property name="configLocation" value="classpath:mybatis-config.xml">
 </property> 
 <property name="dataSource" ref="dataSource" /> 
 </bean> 

 <!-- Mapper接口(dao层接口)所在包名,Spring会自动查找其下的Mapper --> 
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
 <property name="basePackage" value="com.dao"/>
 </bean> 

 <import resource="activiti.cfg.xml"/>

  </beans>
//mybatis-config.xml的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDConfig 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<!-- mybatis的数据类型和处理器等等...都可以实现接口重写,然后配置 -->
 <configuration> 
 <mappers> 
 <!--映射文件--> 
 <mapper resource="com/mapper/UserMapper.xml"/> 
 <mapper resource="com/mapper/QueryUsers.xml"/> 
 <mapper resource="com/mapper/queryUserListById.xml"/> 
 <mapper resource="com/mapper/UserListById.xml"/> 
 <mapper resource="com/mapper/UpdateUserMesById.xml"/>
 </mappers> 
 </configuration> 
//spring-mvc.xml的内容
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
<!-- <context:property-placeholder location="classpath:resources.properties"/> --> 
<!-- 开启controller注解支持 -->
 <!-- 注意事项请参考:http://jinnianshilongnian.iteye.com/blog/1762632 --> 
 <context:component-scan base-package="com.controller" use-default-filters="false"> 
 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 
 </context:component-scan> 
 <mvc:annotation-driven> 
 </mvc:annotation-driven> 
 <!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源 --> <mvc:default-servlet-handler/> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> 
 <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
 <property name="supportedMediaTypes"> 
 <list>
  <value>text/html;charset=UTF-8</value> 
  </list> </property> 
  </bean>
   <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> 
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
  <property name="messageConverters">
   <list> 
   <ref bean="mappingJacksonHttpMessageConverter" />
   <!-- json转换器--> 
   </list> 
   </property> 
   </bean> 
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" /> 
   </beans>

Activiti的画图插件的安装

工作流的流程步骤
(1)画出业务流程图
(2)部署流程
(3)启动流程实例
(4)查询当前办理人的个人任务
(5)完成任务
业务流程图(学生请假)

创建Activiti的表,Activiti跟Spring结合,在项目初始化就可以创建了
部署流程的两种方式()

  /**
       * 部署流程定义使用ClassPath方式(添加)
       * 表的变化:
       * act_re_deployment  流程部署表
       * act_re_procdef  流程定义表
       * act_ge_bytearray  资源文件表
       * act_ge_property   系统配置表
       * */

       @Test
       public void TestflowWithClassPath(){
           private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();//获取流程引擎对象(Activiti的核心对象)

            Deployment deployment =  processEngine.getRepositoryService()  //根据调用流程引擎对象的RepositoryService方法获得 deployment 对象
           .createDeployment()
           .addClasspathResource("diragrams/studentLeaveProcess.bpmn")
           .addClasspathResource("diragrams/studentLeaveProcess.png")
           .name("学生请假流程")
           .deploy();
            //查询流程部署表(以下数据信息)
            System.out.println("流程部署id:"+deployment.getId());
            System.out.println("流程部署名称:"+deployment.getName());
        }

   /**
    * 部署流程定义使用Zip方式(添加)
    *
    * 
    * 
    * */

   @Test
   public void TestFlowWithZip(){
       InputStream inputStream = this.getClass()         //获取当前类对象
           .getClassLoader()  //获取类加载器
           .getResourceAsStream("diragrams/HelloWord.zip");  //获取指定文件资源
       ZipInputStream zipInputStream = new ZipInputStream(inputStream);
       Deployment deployment =  processEngine.getRepositoryService()
       .createDeployment().addZipInputStream(zipInputStream)
       .name("lmy1")
       .deploy();
       System.out.println("流程部署id:"+deployment.getId());
       System.out.println("流程部署名称:"+deployment.getName());

   }

启动流程

 /**
    * 启动流程实例
    * 表的变化:
    * act_ru_excutioon  执行对象表
    * act_ru_identitylink 身份联系表
    * act_ru_task        任务表
    * act_hi_actinst  活动节点历史表
    * act_hi_identitylink  身份联系历史表
    * act_hi_procinst      流程实例历史表
    * act_hi_taskinst    历史任务表
    * */
   @Test
   public void Start(){
              // RuntimeService:在Activiti中,每当一个流程定义被启动一次之后,都会生成一个相应的流程对象实例。RuntimeService提供了启动流程、查询流程实例、设置获取流程实例变量等功能。此外它还提供了对流程部署,流程定义和流程实例的存取服务。
      ProcessInstance pi = processEngine.getRuntimeService()  //使用流程定义的key启动流程
       .startProcessInstanceByKey("myFirstProcess");
       System.out.println("流程实例id:"+pi.getId());
       System.out.println("流程定义id:"+pi.getProcessDefinitionId());
   }

查询当前人的任务

   @Test
   public void Tasklmy(){
// TaskService: 在Activiti中业务流程定义中的每一个执行节点被称为一个Task,对流程中的数据存取,状态变更等操作均需要在Task中完成。TaskService提供了对用户Task 和Form相关的操作。它提供了运行时任务查询、领取、完成、删除以及变量设置等功能。
       List<Task> taskList =  processEngine.getTaskService()
       .createTaskQuery()
       .taskAssignee("lmy")
       .list();
       for (Task task:taskList) {
           System.out.println("任务id"+task.getId());
           System.out.println("任务name"+task.getName());
           System.out.println("任务创建时间"+task.getCreateTime());
           System.out.println("任务委派人"+task.getAssignee());
           System.out.println("流程实例id"+task.getProcessInstanceId());
       }
   }

完成任务

 /**
    * 结束流程
    * 表的变化:
    * 带有re_的数据清空
    * 历史表的(hi_)数据修改或者添加
    * 
    * 
    * */
   @Test
   public void completeTask(){
       processEngine.getTaskService()
       .complete("25004");//任务的id 
   }
3人推荐
随时随地看视频
慕课网APP