使用SpringBoot整合Activiti6工作流的操作方法
1.idea安裝actibpm流程設計器
打開idea插件搜索actibpm,安裝成功後重啟idea
2.創建maven項目,導入相關依賴,添加配置文件
pom.xml依賴
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dongmen</groupId> <artifactId>testActSpringBoot</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <properties> <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> <mysql.version>5.1.29</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-rest-api</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件
#啟動端口 server.port=81 spring.datasource.url=jdbc:mysql://localhost:3306/acttest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #1.false,默認值,acticiti啟動時對比數據庫表中保存的版本,如果沒有表或者版本不匹配,將拋出異常 #2.true,acticiti會對數據中所有的表進行更新操作,如果表不存在,則自動創建 #3.create_drop,在acticiti啟動時創建表,關閉時刪除表(必須手動關閉引擎才能刪除表) #4.drop_create,在acticiti啟動時刪除原來的表,然後創建新表(不需要手動關閉引擎) spring.activiti.database-schema-update=true #檢測歷史表是否存在,acticit7默認沒有開啟歷史記錄信息 spring.activiti.db-history-used=true #歷史記錄等級 #1.none:不保存任何歷史記錄,因此在流程執行過程中,這是最高效的 #2.acticiti:級別高於none,保存流程實例與流程行為,其他數據不保存 #3.audit:除activiti級別會保存的數據外,還會保存全部的流程任務及其屬性,audit為默認值 #4.full:保存歷史數據的最高級別,除瞭保存audit級別的數據外,還會保存其他流程相關的細節數據,包括一些流程參數等 spring.activiti.history-level=full #默認true,效驗流程文件,默認效驗resources下的processes文件夾裡的流程,為true自動部署流程,為false則不部署 #spring.activiti.check-process-definitions=false
3.新增啟動類,剔除掉activiti默認集成springsecurity
package com.dongmen; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author TANGSHUAI * @version 1.0 * @date 2021-12-13 11:13 * 項目啟動會自動部署processes文件夾的bpmn文件 * 剔除springsecurity */ @SpringBootApplication(exclude = { org.activiti.spring.boot.SecurityAutoConfiguration.class, org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class}) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
4.新建文件夾processes(名稱不能更改),新增Bpmn文件,畫流程圖
這裡我們用到瞭三種標簽,
連接下一個標簽的線條,需要鼠標移動到標簽中間來獲取線條
畫好流程圖後點擊空白處,左邊會出現流程參數,這裡我們隻需要設置流程的id與name即可
設置審批用戶信息,這裡我第一個節點設置的時zhangsan,第二個節點設置的是lisi,設置成功後保存
左側菜單介紹
5.創建Controller
package com.dongmen.controller; import org.activiti.engine.HistoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.history.HistoricTaskInstance; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author TANGSHUAI * @version 1.0 * @date 2021-12-13 13:21 */ @RestController public class TestController { @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Autowired private HistoryService historyService; //啟動流程 @RequestMapping("/startAct") public void startAct() { ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1"); System.out.println(processInstance.getId()); } //查詢代辦任務 @RequestMapping("taskAgents") public void taskAgents(){ String user="zhangsan"; List<Task> list = taskService.createTaskQuery().taskAssignee(user).list(); for (Task task : list) { System.out.println("任務id:"+task.getId()); System.out.println("當前審批用戶:"+task.getAssignee()); System.out.println("任務名稱:"+task.getName()); } } //審批流程 @RequestMapping("/approvalProcess") public void approvalProcess() { String key = "myProcess_1"; String user="zhangsan"; //流程實例id,有多個流程時傳遞,單個不需要傳,act_ru_task表id String id="2505"; //根據流程key,與用戶名稱查詢審批 Task task = taskService.createTaskQuery() .processDefinitionKey(key) .taskAssignee(user) .taskId(id) .singleResult(); if(task!=null){ //審批流程 taskService.complete(task.getId()); System.out.println("審批成功!"); } } //查詢已辦任務 @RequestMapping("/hasToDoTasks") public void hasToDoTasks(){ String user="zhangsan"; List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskAssignee(user).list(); for (HistoricTaskInstance historicTaskInstance : list) { System.out.println("開始時間"+historicTaskInstance.getStartTime()+",結束時間:"+historicTaskInstance.getEndTime()); System.out.println("流程名稱"+historicTaskInstance.getName()); } } }
6.啟動項目,Activiti會自動創建28張表,自動部署processes目錄Bpnm文件
表結構介紹
7.測試Controller
localhost:81/startAct–啟動流程,查看數據表act_ru_task
localhost:81/taskAgents–查詢待辦任務,訪問後查看後臺打印
localhost:81/approvalProcess–審批流程,點擊審批流程後變成第二個節點審批
localhost:81/hasToDoTasks–查詢已辦任務,
到此這篇關於使用SpringBoot整合Activiti6(工作流)的文章就介紹到這瞭,更多相關SpringBoot整合Activiti6內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- spring boot隻需兩步優雅整合activiti示例解析
- Activiti7整合Springboot使用記錄
- SpringBoot環境Druid數據源使用及特點
- 使用IDEA配置Mybatis-Plus框架圖文詳解
- Easycode自動化springboot的curd