使用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;
@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;
@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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章