詳細代碼demo,在這裏→https://download.csdn.net/download/qq_28582847/10344437
準備工作:
1.搭建一個springboot+maven開發環境,參考網址:https://blog.csdn.net/qq_28582847/article/details/79891695
2.在IDEA中安裝actiBPM插件 或者 在eclipse中安裝activiti插件(eclipse對activiti畫圖集成較好,本文使用這個)
1.畫工作流程圖
安裝好插件後,使用eclipse新建空項目,右鍵→new→other,選擇activiti Diagram→命名爲demo2.bpmn→finish
本文不教怎麼手動畫流程圖,直接使用xml方式生成,我們使用xml方式打開
將下列代碼複製進去
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1523330736616" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="demo2" isClosed="false" isExecutable="true" name="My process" processType="None">
<startEvent id="startevent1" name="Start"/>
<userTask activiti:exclusive="true" id="usertask1" name="請假"/>
<userTask activiti:exclusive="true" id="usertask2" name="審批"/>
<endEvent id="endevent1" name="End"/>
<sequenceFlow id="flow1" sourceRef="usertask2" targetRef="endevent1"/>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"/>
<sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask1"/>
</process>
<bpmndi:BPMNDiagram documentation="background=#FFFFFF;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
<bpmndi:BPMNPlane bpmnElement="demo2">
<bpmndi:BPMNShape bpmnElement="startevent1" id="Shape-startevent1">
<omgdc:Bounds height="32.0" width="32.0" x="130.0" y="180.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="Shape-usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="271.0" y="170.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="105.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="Shape-usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="471.0" y="170.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="105.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="Shape-endevent1">
<omgdc:Bounds height="32.0" width="32.0" x="690.0" y="180.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1" sourceElement="usertask2" targetElement="endevent1">
<omgdi:waypoint x="576.0" y="197.5"/>
<omgdi:waypoint x="690.0" y="196.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2" sourceElement="usertask1" targetElement="usertask2">
<omgdi:waypoint x="376.0" y="197.5"/>
<omgdi:waypoint x="471.0" y="197.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3" sourceElement="startevent1" targetElement="usertask1">
<omgdi:waypoint x="162.0" y="196.0"/>
<omgdi:waypoint x="271.0" y="197.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
注意:id="demo2"保持和文件名一致
重新右鍵demo2.bpmn→open with→Activiti Diagram Editor打開
2.搭建springboot+maven的項目,結構如下
1)修改pom.xml文件,代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.activiti</groupId>
<artifactId>MyActiviti</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional><!-- optional=true,依賴不會傳遞,該項目依賴devtools;之後依賴myboot項目的項目如果想要使用devtools,需要重新引入 -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)在application.properties增加數據庫連接(程序啓動後,表都是系統自建)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activitiy?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=
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=/workFlow
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
spring.activiti.check-process-definitions=false
3)將demo2.bpmn文件放入到resources/templates目錄下
4)編寫一個controller類
代碼如下:
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@RequestMapping("/firstDemo")
public void firstDemo() {
//根據bpmn文件部署流程
Deployment deployment = repositoryService.createDeployment().addClasspathResource("templates/demo2.bpmn").deploy();
//獲取流程定義
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//啓動流程定義,返回流程實例
ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
String processId = pi.getId();
System.out.println("流程創建成功,當前流程實例ID:"+processId);
Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();
System.out.println("第一次執行前,任務名稱:"+task.getName());
taskService.complete(task.getId());
task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
System.out.println("第二次執行前,任務名稱:"+task.getName());
taskService.complete(task.getId());
task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
System.out.println("task爲null,任務執行完畢:"+task);
}
}
5)啓動application.java類,訪問http://localhost:8081/workFlow/demo/firstDemo,查看控制檯
講解:從代碼上我們可以知道runtimeService.startProcessInstanceById該方法啓動了流程,通過獲取 Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();節點的信息後,我們進行taskService.complete(task.getId());完成該節點,從而流程走到下一節點,直到流程走完。