springboot整合activiti简单案例:
springboot:1.5 activiti:6.0
所需要的pom依赖: springboot整合activiti只需要一个pom依赖就够了。
<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0</version>
</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
activiti.cfg.xml配置: activiti默认是找activiti.cfg.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 流程引擎配置的bean -->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?characterEncoding=UTF-8" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="root" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
流程图:
进入流程,由第一个用户进行申报,申报结果是否同意,若不同意直接结束流程,
同意后进入第二层审核,审核结果是否同意,若不同意直接结束流程,
审核同意之后进入批复,批复不管是否同意直接结束流程。
流程图对应的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="m1561012347288" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="cityDeclare" isClosed="false" isExecutable="true" name="市发起通知,区县级申报流程" processType="None">
<startEvent id="_2" name="StartEvent"/>
<userTask activiti:exclusive="true" id="_3" name="申报"/>
<exclusiveGateway gatewayDirection="Unspecified" id="_5" name="是否同意"/>
<userTask activiti:exclusive="true" id="_6" name="审核"/>
<exclusiveGateway gatewayDirection="Unspecified" id="_7" name="是否同意"/>
<userTask activiti:exclusive="true" id="_8" name="批复"/>
<endEvent id="_9" name="结束流程"/>
<sequenceFlow id="_10" sourceRef="_2" targetRef="_3"/>
<sequenceFlow id="_11" sourceRef="_3" targetRef="_5"/>
<sequenceFlow id="_14" sourceRef="_6" targetRef="_7"/>
<sequenceFlow id="_16" sourceRef="_7" targetRef="_8">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_18" sourceRef="_5" targetRef="_6">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${flag}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_20" sourceRef="_5" targetRef="_9">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${!flag}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_21" sourceRef="_7" targetRef="_9">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${!flag}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_22" sourceRef="_8" targetRef="_9"/>
</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="cityDeclare">
<bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
<omgdc:Bounds height="32.0" width="32.0" x="-10.0" y="200.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
<omgdc:Bounds height="55.0" width="85.0" x="85.0" y="190.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5" isMarkerVisible="false">
<omgdc:Bounds height="32.0" width="32.0" x="220.0" y="200.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_6" id="Shape-_6">
<omgdc:Bounds height="55.0" width="85.0" x="330.0" y="70.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7" isMarkerVisible="false">
<omgdc:Bounds height="32.0" width="32.0" x="625.0" y="80.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_8" id="Shape-_8">
<omgdc:Bounds height="55.0" width="85.0" x="850.0" y="70.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_9" id="Shape-_9">
<omgdc:Bounds height="32.0" width="32.0" x="620.0" y="405.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_14" id="BPMNEdge__14" sourceElement="_6" targetElement="_7">
<omgdi:waypoint x="415.0" y="97.5"/>
<omgdi:waypoint x="625.0" y="96.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_16" id="BPMNEdge__16" sourceElement="_7" targetElement="_8">
<omgdi:waypoint x="657.0" y="96.0"/>
<omgdi:waypoint x="850.0" y="97.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_18" id="BPMNEdge__18" sourceElement="_5" targetElement="_6">
<omgdi:waypoint x="252.0" y="216.0"/>
<omgdi:waypoint x="330.0" y="97.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_20" id="BPMNEdge__20" sourceElement="_5" targetElement="_9">
<omgdi:waypoint x="252.0" y="216.0"/>
<omgdi:waypoint x="620.0" y="421.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="_3" targetElement="_5">
<omgdi:waypoint x="170.0" y="217.5"/>
<omgdi:waypoint x="220.0" y="216.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_22" id="BPMNEdge__22" sourceElement="_8" targetElement="_9">
<omgdi:waypoint x="850.0" y="97.5"/>
<omgdi:waypoint x="652.0" y="421.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10" sourceElement="_2" targetElement="_3">
<omgdi:waypoint x="22.0" y="216.0"/>
<omgdi:waypoint x="85.0" y="217.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_21" id="BPMNEdge__21" sourceElement="_7" targetElement="_9">
<omgdi:waypoint x="638.5" y="109.5"/>
<omgdi:waypoint x="638.5" y="405.1965193707209"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
在springboot项目中流程图有专门的放流程图位置
若不放在这个地方会报错,报一个 processes/ 文件的错,新建一个文件夹把文件放进去就好
用这个流程图做一个简单案例:
代码:
public class test {
public static void main(String[] args) {
//部署一个流程
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService rs = engine.getRepositoryService();
Deployment deploy = rs.createDeployment().addClasspathResource("processes/declare.bpmn").deploy();
ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
//启动流程服务
RuntimeService runtimeService = engine.getRuntimeService();
//启动当前流程
ProcessInstance pi = runtimeService.startProcessInstanceById(pd.getId());
//当前流程id
System.out.println(pi.getId());
//任务服务
TaskService taskService = engine.getTaskService();
//当前流程的任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
Map<String,Object> map=new HashMap<>();
//用来判断当前流程是否通过,流程图中定义的判断条件 flag
map.put("flag",true);
//完成当前节点任务,flag值用于进行判断
taskService.complete(task.getId(),map);
System.out.println("完成第一个节点任务:任务id"+task.getId()+"____ 流程实例ID:"+pi.getId());
//获取第二个节点信息
Task task1 = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
System.out.println("流程实例ID:"+task1.getProcessInstanceId()+"___任务ID:"+task1.getId()+"____任务名称:"+task1.getName());
Map<String,Object> map1=new HashMap<>();
//用来判断当前流程是否通过,流程图中定义的判断条件 flag
map1.put("flag",true);
//完成当前节点任务,flag值用于进行判断
taskService.complete(task1.getId(),map1);
System.out.println("完成第二个节点任务:任务id"+task1.getId()+"____ 流程实例ID:"+pi.getId());
//获取第三个节点信息
Task task2 = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
System.out.println("流程实例ID:"+task2.getProcessInstanceId()+"___任务ID:"+task2.getId()+"____任务名称:"+task2.getName());
taskService.complete(task2.getId());
System.out.println("完成任务");
}
}
打印信息:
- 22505
- 完成第一个节点任务:任务id22509____ 流程实例ID:22505
- 流程实例ID:22505___任务ID:22513____任务名称:市级项目审核
- 完成第二个节点任务:任务id22513____ 流程实例ID:22505
- 流程实例ID:22505___任务ID:22516____任务名称:市级项目批复
- 完成任务
数据库历史记录: