1.导入依赖
<dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter</artifactId><version>7.18.0</version></dependency><dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter-rest</artifactId><version>7.18.0</version></dependency><dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId><version>7.18.0</version></dependency>
去官网找对应的版本适配,这里举个例子
application.yml
server:port: 8019
# camunda登录信息配置
camunda:bpm:admin-user:id: admin #用户名password: 123456 #密码firstName: jiang #名字filter:create: All tasksdeployment-resource-pattern:- classpath:bpmn/*.bpmndatabase:type: mysqlschema-update: trueauto-deployment-enabled: false #是否自动部署 resource 下的 bpmn 文件# mysql连接信息
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.52.132:3307/camunda?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: roottype: com.mysql.cj.jdbc.MysqlDataSource
运行项目,项目会自动写入数据库里,建表
2.创建bpmn文件
不会的小伙伴看上一节,使用工具创建
3.bpmn部署
工作流程图
注释,要不然每次重启项目都部署一份
server:port: 8019
# camunda登录信息配置
camunda:bpm:admin-user:id: admin #用户名password: 123456 #密码firstName: jiang #名字
# filter:
# create: All tasks
# deployment-resource-pattern:
# - classpath:bpmn/*.bpmndatabase:type: mysqlschema-update: trueauto-deployment-enabled: false #是否自动部署 resource 下的 bpmn 文件# mysql连接信息
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.52.132:3307/camunda?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: roottype: com.mysql.cj.jdbc.MysqlDataSource
部署API
@Autowiredprivate RepositoryService repositoryService;//部署用的对象@GetMapping("/deploy")public String deploy() {Deployment deploy = repositoryService.createDeployment().name("部署的第一个流程")//部署名称.addClasspathResource("bpmn/process.bpmn")//绑定需要部署的流程文件.deploy();//部署流程String id = deploy.getId();String name = deploy.getName();Date deploymentTime = deploy.getDeploymentTime();String source = deploy.getSource();String tenantId = deploy.getTenantId();log.info("流程部署成功: id={},name={},deploymentTime={},source={},tenantId={}", id, name, deploymentTime, source, tenantId);return id+":"+name;}
对应三张表
- ACT_RE_DEPLOYMENT
- ACT_RE_PROCDEF
- ACT_GE_BYTEARRAY
启动流程
@Autowiredprivate RuntimeService runtimeService;//流程启动的对象/*** 启动流程的案例** @return*/@GetMapping("/start")public String startFlow() {// 部署流程ProcessInstance processInstance = runtimeService.startProcessInstanceById("my-project-process:3:3a63ba11-3922-11ef-ba3c-005056c00008");// 部署的流程实例的相关信息String id = processInstance.getId();String processInstanceId = processInstance.getProcessInstanceId();String rootProcessInstanceId = processInstance.getRootProcessInstanceId();String processDefinitionId = processInstance.getProcessDefinitionId();String tenantId = processInstance.getTenantId();String caseInstanceId = processInstance.getCaseInstanceId();
//id=226a591f-3926-11ef-9404-005056c00008,processInstanceId=226a591f-3926-11ef-9404-005056c00008,rootProcessInstanceId=226a591f-3926-11ef-9404-005056c00008,processDefinitionId=my-project-process:3:3a63ba11-3922-11ef-ba3c-005056c00008,tenantId=null,caseInstanceId=nulllog.info("流程启动成功: id={},processInstanceId={},rootProcessInstanceId={},processDefinitionId={},tenantId={},caseInstanceId={}", id, processInstanceId,rootProcessInstanceId, processDefinitionId, tenantId, caseInstanceId);return id;}
数据库生成一个待审批对象
生成实例ID 待审批数据
在这个表里历史记录流程任务
手动审批过后,数据就被清除了
流程实例历史记录表
查询任务
@Autowiredprivate TaskService taskService;/*** 查询任务* 代办** 流程定义ID:processDefinition:我们部署流程的时候会产生对应的 流程定义ID,每个部署流程,会产生一个流程定义ID re 部署时* 流程实例ID: processInstance :我们流程启动实例的时候,会产生一个流程实例ID;ru 运行时*** @return*/@GetMapping("/queryTask/{processInstanceId}")public String queryTask(@PathVariable("processInstanceId") String processInstanceId){List<Task> list = taskService.createTaskQuery().processInstanceId(processInstanceId).list();List<Task> list1 = taskService.createTaskQuery().list();List<Task> list2 = taskService.createTaskQuery().listPage(0,5);//分页TaskQuery admin = taskService.createTaskQuery().taskAssignee("admin");//用户的ID;查询该用户的代办任务List<Task> list3 = admin.list();log.info("admin一共有{}个代办任务",list3.size());int size = list1.size();for (Task task : list) {String assignee = task.getAssignee();String id = task.getId();String name = task.getName();log.info("查询实例ID:assignee={},id={},name={}",assignee,id,name);}log.info("一共{}代办任务",size);return list.get(0).getId();}
完成任务
/*** 完成任务* @param processInstanceId*/@GetMapping("/complete/{processInstanceId}")public void completeTask(@PathVariable("processInstanceId") String processInstanceId){// 根据实例ID查询到任务Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();boolean suspended = task.isSuspended();//判断任务是否被冻结,暂停 以后用到的System.out.println(suspended);if (task!=null){taskService.complete(task.getId());//两个参数的,后面时流程变量,也就是传参的System.out.println("任务审批完成");}}