此處參考過幾篇網友寫的博客,然後自己測試之後做出來的總結。如有雷同,純屬抄襲他們:

一個簡單的例子,工作流的作用不再贅述,直接上。

開發工具為 Idea,安裝 Activiti 插件:

新建的 maven 項目,項目結構:

 

因為是開發工具的問題,新建的 bpmn 文件需要改成 xml 後綴名,然後做如下操作生成 png 文件之後再改回來

       

 

流程圖(如上圖),此處是 xml 格式打開的代碼:

1.亂碼問題,需要在Idea 安裝目錄下的 bin 文件夾中的

idea.exe.vmoptions 和idea64.exe.vmoptions 文件中最後一行添“-Dfile.encoding=UTF-8”

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="demo2" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="請假"></userTask>
    <userTask id="usertask2" name="審批"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="130.0" y="180.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="271.0" y="170.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="471.0" y="170.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="690.0" y="180.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="576.0" y="197.0"></omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="197.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="376.0" y="197.0"></omgdi:waypoint>
        <omgdi:waypoint x="471.0" y="197.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="165.0" y="197.0"></omgdi:waypoint>
        <omgdi:waypoint x="271.0" y="197.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

 

啓動類 AdminApplication.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author Created by zwb on 2018/7/18.
 */
@SpringBootApplication
@ComponentScan({"controller"})
public class AdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}

 

測試類 TestController.java:

1.啓動流程定義,返回流程實例後,一般都會自動在數據庫創建25 張工作流需要的表格。其中數據隨着流程的進程變化而改變,無需關心;

2.SpringBoot會把常用的服務自動配置注入,使用時直接裝配使用即可:

package controller;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Created by zwb on 2018/7/18.
 */
@RestController
@RequestMapping("/testconrtoller")
public class TestConrtoller {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @RequestMapping("/firstDemo")
    public void firstDemo() {

        //根據bpmn文件部署流程
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/shenqing.bpmn").deploy();
        //獲取流程定義
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        //啓動流程定義的 3 種方法,返回流程實例
//        ProcessInstance pi1 = runtimeService.startProcessInstanceById(processDefinition.getId());
        //定義流程時手動填寫的流程 Id
//        ProcessInstance pi2 = runtimeService.startProcessInstanceByKey(processDefinitionKey);
//        實際開發情況一般都會傳一個BusinessKey【此處舉例是"uuid"】,用於在別的域獲取ProcessInstance,然後獲取task 對流進行操作。
        ProcessInstance pi3 = runtimeService.startProcessInstanceById(processDefinition.getId(), "uuid");

//        在別的域(類)獲取流的方法
        ProcessInstance newPi = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey("uuid").singleResult();
        String processId = newPi.getId();
        System.out.println("流程創建成功,當前流程實例ID:" + processId);

        Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("第一次執行前,任務名稱:" + task.getName() + ", 任務Id:" + task.getId());
        taskService. complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("第二次執行前,任務名稱:" + task.getName() + ", 任務Id:" + task.getId());
        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("task為null,任務執行完畢:" + task);
    }

}

 

配置文件 application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/activitipractice?useUnicode=true&amp;characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=zeng
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.context-path=/activitiTest2
server.session.timeout=10

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
//端口號
server.port=8081

 

測試結果:

(每一步審批操作 Id 都會改變,但是 BusinessKey 不會變,所以保證拿到的都是同一個 ProcessInstance ,操作同一個流程)

0fb9b7bb828e5ee1dd368fe5c2667ca03ff.jpg

轉載於:https://my.oschina.net/u/3780366/blog/1859267