Springboot整合activity過程圖解
1.第一步新增bpmn檔案得外掛,不然沒法檢視和編輯bpmn檔案,新增外掛的方法各自百度即可,很簡單
2.安裝好bpmn外掛後開始新建bpmn檔案,也就是畫流程圖
一般是在程式碼中進行指定流程審批人的,我這裡只做入門案例
3.新增pom依賴
這裡包含了線上設計流程圖的相關依賴,在下一篇會寫出來,專案中一般都是在前端線上設計;所以直接放上去也沒事
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com</groupId> <artifactId>springboot_activiti</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_activiti</name> <description>Demo project for Spring Boot</description> <properties> <springboot.version>2.2.0.RELEASE</springboot.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <activiti.version>6.0.0</activiti.version> </properties> <dependencies> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>${activiti.version}</version> <exclusions> <exclusion> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-jdbc</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-tx</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-orm</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>mybatis</artifactId> <groupId>org.mybatis</groupId> </exclusion> <exclusion> <artifactId>activation</artifactId> <groupId>javax.activation</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>${activiti.version}</version> </dependency> <!-- Activiti 流程圖 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-common-rest</artifactId> <version>${activiti.version}</version> </dependency> <!-- Activiti 線上設計 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-modeler</artifactId> <version>5.22.0</version> <exclusions> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-tx</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-web</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-security-config</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-security-core</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-security-crypto</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-security-web</artifactId> <groupId>org.springframework.security</groupId> </exclusion> <exclusion> <artifactId>spring-webmvc</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>activation</artifactId> <groupId>javax.activation</groupId> </exclusion> <exclusion> <artifactId>commons-io</artifactId> <groupId>commons-io</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>${springboot.version}</version> </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-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8081</port> <path>/activity</path> <systemProperties> <com.sun.management.jmxremote.port>4000</com.sun.management.jmxremote.port> </systemProperties> </configuration> </plugin> </plugins> </build> </project>
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.Driveractiviti:
# 自動部署驗證設定: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(){ //獲取待辦的一些資訊,這裡可以傳入需要查詢的使用者, //我這裡查詢的所有待辦 List<Task> tasks = 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傳入即可)
請求案例
查詢待辦
啟動流程
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。