最近在熟悉activiti的使用,翻阅了很多资料,自己也整理了一点出来方便以后自己使用

一、引入依赖

<dependencies><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-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.49</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-spring-boot-starter-basic</artifactId><version>5.22.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>

二、配置application.yml

server:port: 8081spring:datasource:# 使用阿里巴巴的 druid 数据源url: jdbc:mysql://127.0.0.1:3306/dtactiviti?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource# Activiti 配置activiti:#1.flase:默认值。activiti在启动时,对比数据库表中保存的版本,如果没有表或者版本不匹配,将抛出异常#2.true: activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建#3.create_drop: 在activiti启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)#4.drop-create: 在activiti启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)database-schema-update: true#记录历史等级 可配置的历史级别有none, activity, audit, full#none:不保存任何的历史数据,因此,在流程执行过程中,这是最高效的。#activity:级别高于none,保存流程实例与流程行为,其他数据不保存。#audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。audit为history的默认值。#full:保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括一些流程参数等。
#    history-level: full#校验流程文件,默认校验resources下的processes文件夹里的流程文件check-process-definitions: false

注意database-schema-update: true 这里,第一次要用true,自动创表,后续要改为false

三、启动类加配置,因为activiti默认整合了springSecurity,暂时用不到,需要排除

@SpringBootApplication(exclude = {//activiti-spring-boot-starter-basic中的配置类,里面依赖了spring-security,不排除这个启动会报错org.activiti.spring.boot.SecurityAutoConfiguration.class})
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

四、画流程图,这个步骤我就不描述了,画好的流程图需要放在resources,创建一个文件夹processes(activiti默认这个文件夹查找流程),下面放两个我写好的流程图和文件

leaveProcess.bpmn20.xml

<?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:xsd="http://www.w3.org/2001/XMLSchema" 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/processdef"><process id="leaveProcess" name="leaveProcess" isExecutable="true"><startEvent id="sid-1a912a42-47e5-4319-9cf3-fcd91c9bf487"/><userTask id="sid-a8ae0d0e-6a1e-4e6c-a2f6-fca15fc58fce" name="申请人" activiti:assignee="${applicant}"/><userTask id="sid-de2ebfb3-e445-4d94-a341-021de2062247" name="经理审批" activiti:assignee="${manager}"/><sequenceFlow id="sid-507b9525-d071-4554-a4db-2aa55d7922b5" sourceRef="sid-a8ae0d0e-6a1e-4e6c-a2f6-fca15fc58fce" targetRef="sid-de2ebfb3-e445-4d94-a341-021de2062247"/><sequenceFlow id="sid-7e8afb38-b67c-4083-8589-c15b370de59d" sourceRef="sid-1a912a42-47e5-4319-9cf3-fcd91c9bf487" targetRef="sid-a8ae0d0e-6a1e-4e6c-a2f6-fca15fc58fce"/><exclusiveGateway id="sid-3c5079d4-ab1d-496e-808f-40f63417acdb"/><sequenceFlow id="sid-4352501d-3870-442d-96bd-41e618a6902f" sourceRef="sid-3c5079d4-ab1d-496e-808f-40f63417acdb" targetRef="sid-a8ae0d0e-6a1e-4e6c-a2f6-fca15fc58fce" name="不通过"><conditionExpression xsi:type="tFormalExpression">${managerOpinion==0}</conditionExpression></sequenceFlow><sequenceFlow id="sid-d0ec934e-1e17-4c12-8c1c-a17450297694" sourceRef="sid-de2ebfb3-e445-4d94-a341-021de2062247" targetRef="sid-3c5079d4-ab1d-496e-808f-40f63417acdb"/><userTask id="sid-e3cc17ad-6c47-4eaf-840b-169593e45923" name="人事复核" activiti:assignee="${personnel}"/><sequenceFlow id="sid-e7375a5c-173f-4b3f-bde7-339f5891f11e" sourceRef="sid-3c5079d4-ab1d-496e-808f-40f63417acdb" targetRef="sid-e3cc17ad-6c47-4eaf-840b-169593e45923" name="通过"><conditionExpression xsi:type="tFormalExpression">${managerOpinion == 1}</conditionExpression></sequenceFlow><exclusiveGateway id="sid-9bf62355-4c10-4932-9929-930ee1c301ef"/><sequenceFlow id="sid-7e9c5565-8e9d-4162-8191-86711ef50116" sourceRef="sid-e3cc17ad-6c47-4eaf-840b-169593e45923" targetRef="sid-9bf62355-4c10-4932-9929-930ee1c301ef"/><endEvent id="sid-38b4bfab-1d4b-4e70-930b-62c037d6803a"/><sequenceFlow id="sid-a16911a2-ab8b-438e-9d4f-e141f55f4000" sourceRef="sid-9bf62355-4c10-4932-9929-930ee1c301ef" targetRef="sid-38b4bfab-1d4b-4e70-930b-62c037d6803a" name="通过"><conditionExpression xsi:type="tFormalExpression">${isPass == 1}</conditionExpression></sequenceFlow><sequenceFlow id="sid-c1cb7ab6-9aa8-4414-a0fa-d553f8ea5991" sourceRef="sid-9bf62355-4c10-4932-9929-930ee1c301ef" targetRef="sid-de2ebfb3-e445-4d94-a341-021de2062247" name="不通过"><conditionExpression xsi:type="tFormalExpression">${isPass == 0}</conditionExpression></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_leaveProcess"><bpmndi:BPMNPlane bpmnElement="leaveProcess" id="BPMNPlane_leaveProcess"><bpmndi:BPMNShape id="shape-a36260e2-af77-4d5d-be73-abb84f02ebb4" bpmnElement="sid-1a912a42-47e5-4319-9cf3-fcd91c9bf487"><omgdc:Bounds x="-325.0" y="-30.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-b8e0a02e-1390-4461-bec6-4b1db6f31717" bpmnElement="sid-a8ae0d0e-6a1e-4e6c-a2f6-fca15fc58fce"><omgdc:Bounds x="-270.0" y="-40.0" width="60.0" height="50.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-82eb6a41-2b6b-4629-9883-b28ab2b33f3f" bpmnElement="sid-de2ebfb3-e445-4d94-a341-021de2062247"><omgdc:Bounds x="-195.0" y="-40.0" width="60.0" height="50.000004"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-fcf30947-b7fc-47a9-bd0c-e46c33206592" bpmnElement="sid-507b9525-d071-4554-a4db-2aa55d7922b5"><omgdi:waypoint x="-210.0" y="-15.0"/><omgdi:waypoint x="-200.0" y="-14.999998"/><omgdi:waypoint x="-195.0" y="-14.999998"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-4641beca-3169-4dba-ab19-4cdf6f5d657a" bpmnElement="sid-7e8afb38-b67c-4083-8589-c15b370de59d"><omgdi:waypoint x="-295.0" y="-15.0"/><omgdi:waypoint x="-270.0" y="-15.0"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-299c70b4-8108-4f3c-a052-5c739ed074bd" bpmnElement="sid-3c5079d4-ab1d-496e-808f-40f63417acdb"><omgdc:Bounds x="-120.0" y="-34.999996" width="40.0" height="40.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-aa6d70c3-3b82-43f6-9ed6-2b16d295f496" bpmnElement="sid-4352501d-3870-442d-96bd-41e618a6902f"><omgdi:waypoint x="-100.0" y="-34.999996"/><omgdi:waypoint x="-100.0" y="-83.75"/><omgdi:waypoint x="-240.0" y="-83.75"/><omgdi:waypoint x="-240.0" y="-40.0"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-294a065a-3603-41d5-beca-ec89f2f5834f" bpmnElement="sid-d0ec934e-1e17-4c12-8c1c-a17450297694"><omgdi:waypoint x="-135.0" y="-14.999998"/><omgdi:waypoint x="-120.0" y="-14.999996"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-38a2f728-ee57-4bff-9888-a036cfce59b3" bpmnElement="sid-e3cc17ad-6c47-4eaf-840b-169593e45923"><omgdc:Bounds x="-45.0" y="-39.999996" width="60.000004" height="50.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-56c07539-e7f8-4e69-a7ab-8e506b279614" bpmnElement="sid-e7375a5c-173f-4b3f-bde7-339f5891f11e"><omgdi:waypoint x="-80.0" y="-14.999996"/><omgdi:waypoint x="-45.0" y="-14.999996"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-592448bd-b2d2-421c-84ee-0012bcd0be4c" bpmnElement="sid-9bf62355-4c10-4932-9929-930ee1c301ef"><omgdc:Bounds x="30.0" y="-35.0" width="40.0" height="40.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-00ce58d5-f0cb-4f92-afe7-c4d31b2eff36" bpmnElement="sid-7e9c5565-8e9d-4162-8191-86711ef50116"><omgdi:waypoint x="15.000004" y="-14.999996"/><omgdi:waypoint x="30.0" y="-15.0"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-b65e0a57-510a-47a3-b875-19d66c64157c" bpmnElement="sid-38b4bfab-1d4b-4e70-930b-62c037d6803a"><omgdc:Bounds x="124.99999" y="-30.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-a41ee42d-86c4-49b4-8151-380d4fc84bd8" bpmnElement="sid-a16911a2-ab8b-438e-9d4f-e141f55f4000"><omgdi:waypoint x="70.0" y="-15.0"/><omgdi:waypoint x="124.99999" y="-15.0"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-9b9db2c4-6d38-4ecc-b5ad-ce6d7fd1e234" bpmnElement="sid-c1cb7ab6-9aa8-4414-a0fa-d553f8ea5991"><omgdi:waypoint x="50.0" y="5.0"/><omgdi:waypoint x="50.0" y="66.25"/><omgdi:waypoint x="-165.0" y="66.25"/><omgdi:waypoint x="-165.0" y="10.000004"/></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

leaveProcess.bpmn20.png 这里是图片,放个截图

放置位置截图

五、代码

包含部署流程,申请人申请流程,经理审批,人事审批等

package com.example.demo.ctrl;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.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @Author: hzx* @CreateTime: 2022-07-12* @Description: 一个完整的请假流程*/
@RestController
@RequestMapping("processes")
public class LeaveProcessCtrl {@Resourceprivate RepositoryService repositoryService;@Resourceprivate RuntimeService runtimeService;@Resourceprivate TaskService taskService;/*** 部署流程(将bpmn和png文件部署到数据库中)* @return*/@RequestMapping("/deployment")public Map<String, Object> deployment() {Deployment deployment = repositoryService.createDeployment().name("部署请假流程").addClasspathResource("processes/leaveProcess.bpmn20.xml").addClasspathResource("processes/leaveProcess.bpmn20.png").deploy();Map<String, Object> map = new HashMap<>();map.put("msg", "->>>请假流程部署成功!");map.put("部署流程ID:", deployment.getId());map.put("部署流程名称:", deployment.getName());map.put("部署时间:", deployment.getDeploymentTime());return map;}/*** 获取流程的所有任务* @return*/@RequestMapping("/getAllList")public List<Map<String,Object>> getAllList(){List<Task> list = taskService.createTaskQuery().processDefinitionKey("leaveProcess").list();List<Map<String,Object>> returnList = new ArrayList<>();Map<String, Object> map;// 输出当前用户具有的任务for (Task task : list) {map = new HashMap<>();map.put("流程实例id", task.getProcessInstanceId());map.put("任务id", task.getId());map.put("当前任务负责人", task.getAssignee());map.put("任务名称", task.getName());returnList.add(map);}return returnList;}/*** name  申请人,这里是申请人初次申请,直接开始流程,如果是退回的,需要判断或者重新一个方法用于处理多次提交的* @param name* @return*/@RequestMapping("startProcess")public Map<String, Object> startProcess(String name) {//设置任务委派人Map<String, Object> variables = new HashMap<>();
//        variables.put("applicant", name);variables.put("applicant", "张三");//根据流程定义key来启动流程定义,返回流程实例ProcessInstance pi = runtimeService.startProcessInstanceByKey("leaveProcess",variables);//申请人无需审批,直接通过,流程流转到下一个人,Map<String, Object> variablesNext = new HashMap<>();//TODO 这里需要获取数据库定义的配置来确定经理是谁variablesNext.put("manager","经理");//根据流程id获取任务Task task = taskService.createTaskQuery().processInstanceId(pi.getProcessInstanceId()).singleResult();Map<String, Object> map = new HashMap<>();map.put("流程实例ID:", task.getProcessInstanceId());map.put("任务ID:", task.getId());map.put("任务负责人:", task.getAssignee());map.put("任务名称:", task.getName());//自动通过申请人,交给下任处理taskService.complete(task.getId(),variablesNext);//处理完成,交给下个人return map;}/*** 根据名字来查当前人的代办任务* @param name* @return*/@RequestMapping("myTask")public List<Map<String,Object>> myTask(String name) {List<Task> list = taskService.createTaskQuery().taskAssignee(name).list();//任务列表的展示List<Map<String,Object>> returnList = new ArrayList<>();Map<String, Object> map;// 输出当前用户具有的任务for (Task task : list) {map = new HashMap<>();map.put("流程实例id", task.getProcessInstanceId());map.put("任务id", task.getId());map.put("任务负责人", task.getAssignee());map.put("任务名称", task.getName());returnList.add(map);}return returnList;}/*** 申请人重新提交* @param processInstanceId  流程实例id* @return*/@RequestMapping("applicant")public String applicant(String processInstanceId){//根据流程id获取任务Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();if (task == null) {return "找不到任务";}Map<String, Object> variablesNext = new HashMap<>();variablesNext.put("manager","经理");taskService.complete(task.getId(),variablesNext);//处理完成,交给下个人return "申请人重新申请";}/*** 经理审批操作* @param processInstanceId  流程实例id* @param managerOpinion  1为通过,0为不通过,不通过需要转回给申请人* @return*/@RequestMapping("managerApproval")public String managerApproval(String processInstanceId,Integer managerOpinion){//根据流程id获取任务Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();if (task == null) {return "找不到任务";}Map<String, Object> variablesNext = new HashMap<>();//通过if(managerOpinion == 1){//TODO 这里需要获取数据库定义的配置来确定人事名字variablesNext.put("personnel","人事");}else{//TODO 这里需要获取数据库定义的配置来确定回退给谁variablesNext.put("applicant","张三");}variablesNext.put("managerOpinion", managerOpinion);taskService.complete(task.getId(),variablesNext);//处理完成,交给下个人return "经理处理操作成功";}/*** 人事审批操作* @param  processInstanceId  流程实例id* @param isPass  1为通过,0为不通过,不通过需要转回给经理* @return*/@RequestMapping("personnelApproval")public String personnelApproval(String processInstanceId,Integer isPass){//根据流程id获取任务Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();if (task == null) {return "找不到任务";}Map<String, Object> variablesNext = new HashMap<>();//不通过需要转为给经理,通过则不需要指定人if(isPass == 0){//TODO 这里需要获取数据库定义的配置来确定回退给谁,这里的流程定义是退回给经理variablesNext.put("manager","经理");}variablesNext.put("isPass", isPass);taskService.complete(task.getId(),variablesNext);//处理完成,交给下个人(回退给经理或者结束流程)return "人事操作成功";}}

大致的Demo就到这里了

SpringBoot 整合activiti5.22 实现一个完整的请假流程相关推荐

  1. 全网最新Spring Boot2.5.1整合Activiti5.22.0企业实战教程<流程挂起与激活篇>

    文章目录 前言 一.单个流程实例挂起 1.1 部署流程 1.2 启动流程 1.3 挂起流程(suspendProcessInstanceById) 1.4 执行任务 1.5 激活流程(activate ...

  2. 利用vue-cli配合vue-router搭建一个完整的spa流程

    好文章备忘录: 转自:https://segmentfault.com/a/1190000009160934?_ea=1849098 demo源码:https://github.com/1590123 ...

  3. pycharm的项目文件中包括什么_一个完整的项目管理流程包括什么?

    首先,我们先了解一下什么是「项目」和「项目管理」. 「项目」是为完成某一独特的产品或服务所做的临时性努力.在项目的实施过程中,通常都会遇到一些问题,这些问题会阻碍项目的实施.而「项目管理」就是指在项目 ...

  4. 一个完整的项目管理流程包括什么?项目管理者必看

    一个完整的项目管理流程包括规划.执行.收尾三大阶段. 第一阶段:规划 规划包括创建项目事项.分解复杂事项.派发任务 我们公司的项目管理者都在用飞项项目管理工具,对于项目内的事项,可以直接在PC/小程序 ...

  5. 数据分析人必看,分享一个完整的数据分析流程

    想入门数据分析应该怎么学?新手做数据分析有哪些好用的工具?会用 Excel 但是做分析总是没思路怎么办?做数据分析有哪些方法...... 以上这些问题,相信各位想入门数据分析的小伙伴们或多或少都会遇到 ...

  6. 如何设计一个完整的交互流程,提升产品用户体验

    交互流程设计是一项关乎用户体验的重要工作.通过设计和规划用户与产品或服务的交互方式和流程,我们可以提高用户的满意度和使用效果.在本文中,我们将深入探讨交互流程设计的关键要素以及其对用户体验的重要性. ...

  7. 一个完整的测试流程包括哪些?测试人员需要做什么?

    目录 前言 1.需求交接 2.编写测试用例 3.冒烟测试 4.SIT测试 5.数据升级测试(视情况而定) 6.系统培训(视情况而定) 7.UAT测试 8.上线 结语 前言 在实际工作中,其实很少有公司 ...

  8. 基于 Spring Boot 2.x 使用 Activiti 创建一个简易的请假流程

    文章目录 1 摘要 2 准备工作 3 请假流程图 3 核心代码 3.1 数据库表 3.2 流程管理工具类 3.3 流程的使用 4 推荐参考资料 5 Github 源码 ​ ​ ​ 1 摘要 工作流是需 ...

  9. 一个完整项目的流程都涉及哪些内容

    最近在跟着老师学做一个有关图书馆的项目,目标是做出一个移动端的包含校内图书馆内容的图书馆.上完第一节课,梳理一下有关内容. 第一节课主要介绍了做一个完整的项目的流程都有哪些,涉及哪方面的内容,具体如下 ...

最新文章

  1. [Bzoj2282]消防(二分答案+树的直径)
  2. 2.aop中几个注解的含义
  3. 【Windows 逆向】OD 调试器工具 ( CE 工具通过查找访问的方式找到子弹数据基地址 | 使用 OD 工具附加游戏进程 | 在 OD 工具中查看子弹数据地址 | 推荐 )
  4. ajax img标签,如何将属性添加到img标签以用于ajax调用
  5. leetcode Number of 1 Bits
  6. 牛客 - 共鸣问题(贪心+思维)
  7. iOS开发Swift篇—(七)函数(1)
  8. 送花(洛谷 2073)
  9. spark 不同模式用途_Spark 的四种模式
  10. 漫画:要跳槽?这道缓存设计题你有必要看看!
  11. pb 如何判断数据窗口中是否有某个字段_怎么判断数据窗口中某个字段存在重复的数据...
  12. 信息林-青岛分类信息网.net源码
  13. 十分钟掌握Nodejs下载和安装
  14. 通俗易懂的理解BiLSTM-CRF模型中的CRF层
  15. wireshark使用教程
  16. wps表格怎么添加附页_关于WPS里面页码的问题
  17. 组合数问题(NOIP2016提高组Day2T1)
  18. 救命啊!还是讨厌的中文问题
  19. [学习笔记]什么是存储过程?
  20. 关于File()中的pathname的路径

热门文章

  1. 什么是信息增益(Information Gain)?
  2. linux系统设置自动拨号上网
  3. 华为手机8.0.0怎么找到云相册_华为云相册下载-华为云相册预约 安卓版v1.0-PC6安卓网...
  4. Battery_MSCCC:基于MATLAB/Simulink的具有多级(5级)恒流控制的电池充放电仿真模型
  5. 目录操作的相关API 和 获取文件的属性信息
  6. 六爻排盘php源码,六爻排盘-六爻在线排盘-六爻在线起卦-纳甲六爻起局-六爻在线摇卦-元亨利贞网在线六爻排盘系统...
  7. 极具挑战性的工作:升级模式库从CQ2002到CQ7
  8. 甘特图——项目计划与实际进度比较与调整
  9. 喵懂区块链23期|9012年了,侧链还是一只又死又活的薛定谔猫?
  10. python-selenium自动化测试网页