Springboot集成activity的用法
这篇文章主要讲解了Springboot集成activity的用法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
专注于为中小企业提供网站设计、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业江汉免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
1.第一步添加bpmn文件得插件,不然没法查看和编辑bpmn文件,添加插件的方法各自百度即可,很简单
2.安装好bpmn插件后开始新建bpmn文件,也就是画流程图
一般是在代码中进行指定流程审批人的,我这里只做入门案例
3.添加pom依赖
这里包含了在线设计流程图的相关依赖,在下一篇会写出来,项目中一般都是在前端在线设计;所以直接放上去也没事
<?xml version="1.0" encoding="UTF-8"?>4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.0.RELEASE com springboot_activiti 0.0.1-SNAPSHOT springboot_activiti Demo project for Spring Boot 2.2.0.RELEASE UTF-8 UTF-8 1.8 6.0.0 org.activiti activiti-engine ${activiti.version} org.activiti activiti-spring ${activiti.version} org.codehaus.jackson jackson-core-asl org.slf4j slf4j-log4j12 spring-context org.springframework spring-jdbc org.springframework spring-tx org.springframework spring-orm org.springframework spring-beans org.springframework mybatis org.mybatis activation javax.activation org.activiti activiti-spring-boot-starter-basic ${activiti.version} org.activiti activiti-common-rest ${activiti.version} org.activiti activiti-modeler 5.22.0 spring-beans org.springframework spring-context org.springframework spring-core org.springframework spring-tx org.springframework spring-web org.springframework spring-security-config org.springframework.security spring-security-core org.springframework.security spring-security-crypto org.springframework.security spring-security-web org.springframework.security spring-webmvc org.springframework activation javax.activation commons-io commons-io org.springframework.boot spring-boot-autoconfigure ${springboot.version} org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web commons-io commons-io 2.3 com.fasterxml.jackson.core jackson-core 2.10.0 org.springframework.boot spring-boot-starter-data-jpa MySQL mysql-connector-java runtime org.junit.jupiter junit-jupiter-api RELEASE compile org.springframework.boot spring-boot-maven-plugin org.apache.tomcat.maven tomcat7-maven-plugin 8081 /activity 4000
4.yml文件
这里采用的数据库还是mysql
这里有个需要注意的地方,springboot2.2中的driver-class-name值变了,不是以前的那个了,不然会报错
spring:
datasource:
url: jdbc:mysql://localhost:3307/t2?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
activiti:
# 自动部署验证设置:true-开启(默认)、false-关闭
check-process-definitions: false
#自动部署文件路径后缀
# process-definition-location-prefix: classpath:/processes/
# process-definition-location-suffixes:
# - **.bpmn
# - **.bpmn20.xml
server:
port: 8082
5.配置已经好了,下面开始写代码了,只是初步写了一些新建流程,正常提交流程,直接结束流程,以及查询待办的案例,细节还需要各自自己去调试学习
controller
package com.springboot_activiti.controller; import com.springboot_activiti.service.ActivityConsumerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/activiti") public class ActivitiController { @Autowired private ActivityConsumerService activityConsumerService; /** * 注册流程 * @return */ @RequestMapping("/createDeployment") public Boolean createDeployment(){ return activityConsumerService.createDeployment(); } /** * 启动流程 * @return */ @RequestMapping("/startActivityDemo") public Boolean startActivityDemo(){ return activityConsumerService.startActivityDemo("test01"); } /** * 获取待办 * @return */ @RequestMapping("/getTaskList") public boolean getTaskList(){ return activityConsumerService.getTaskList(); } /** * 提交 * @param taskId * @return */ @RequestMapping("/complete") public boolean complete(String taskId){ return activityConsumerService.complete(taskId); } /** * 根据流程id直接结束流程 * @paramru * @return */ @RequestMapping("/deleteProcessInstance") public boolean deleteProcessInstance(String runId){ return activityConsumerService.deleteProcessInstance(runId); } }
service
package com.springboot_activiti.service; public interface ActivityConsumerService { boolean createDeployment(); boolean startActivityDemo(String key); boolean getTaskList(); boolean complete(String taskId); boolean deleteProcessInstance(String runId); }
实现类
package com.springboot_activiti.service; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.DeploymentBuilder; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("activityService") public class ActivityConsumerServiceImpl implements ActivityConsumerService { @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Autowired private RepositoryService repositoryService; /** * 注册一个流程 * @return */ @Override public boolean createDeployment() { DeploymentBuilder builder=repositoryService.createDeployment(); builder.addClasspathResource("test01.bpmn"); builder.deploy(); return true; } /** * 查询待办 * @return */ public boolean getTaskList(){ //获取待办的一些信息,这里可以传入需要查询的用户, //我这里查询的所有待办 Listtasks = taskService.createTaskQuery().list(); for (Task t:tasks) { System.out.println(t.getCreateTime()); System.out.println(t.getId()); System.out.println(t.getName()); System.out.println(t.getProcessInstanceId()); System.out.println(t.getTaskDefinitionKey()); System.out.println(t.getParentTaskId()); } return true; } /** * 根据流程key开启一个流程 * @param key * @return */ @Override public boolean startActivityDemo(String key) { ProcessInstance test01 = runtimeService.startProcessInstanceByKey(key); String id = test01.getId(); System.out.println("流程id="+id); /* */ /* String assignee = "abc";*/ return true; } /** * 根据任务id提交任务 * @param taskId * @return */ @Override public boolean complete(String taskId){ taskService.complete(taskId); return true; } /** * 根据流程id直接结束流程 * @param runId * @return */ @Override public boolean deleteProcessInstance(String runId){ runtimeService.deleteProcessInstance(runId,"结束"); return true; } }
启动类,这里存在一个在线视图设计的验证处理,
package com.springboot_activiti; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com") @SpringBootApplication @EnableAutoConfiguration(exclude = { org.activiti.spring.boot.SecurityAutoConfiguration.class }) public class SpringbootActivitiApplication { public static void main(String[] args) { SpringApplication.run(SpringbootActivitiApplication.class, args); } }
所有的代码已经写好了
备注说明:
项目启动会新建28张表,各自代表的含义自行百度把
因为我的数据都是直接在流程图bpmn文件里面写好的,实际中肯定是在代码里面指定的
首先访问注册流程
启动流程(会返回一个流程id)
查看待办就可以获取相关的信息了
正常提交任务,会审批到下一节点(查看待办可以得到任务id)
直接结束流程(非正常结束,启动流程的id传入即可)
请求案例
查询待办
启动流程
看完上述内容,是不是对Springboot集成activity的用法有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。
文章标题:Springboot集成activity的用法
浏览路径:http://hbruida.cn/article/pggehh.html