一、搭建一个基于maven的基础框架
1.环境说明:JDK11、Mysql8
2.项目结构概览:
二、项目相关代码及配置
1.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>sdata.ops.platform</groupId>
<artifactId>flowable</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--Springboot项目自带 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Springboot Web项目 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<!-- idm依赖提供身份认证 -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-ui-idm</artifactId>
<version>6.7.1</version>
</dependency>
<!-- modeler绘制流程图 -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-ui-modeler</artifactId>
<version>6.7.1</version>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- flowable -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.7.1</version>
</dependency>
</dependencies>
</project>
2.application.yml配置
server:
port: 8088
flowable:
idm:
app:
admin:
# 登录的用户名
user-id: admin
# 登录的密码
password: admin
# 用户的名字
first-name: *
last-name: **
spring:
# mysql连接信息
datasource:
# mysql8之后
driver-class-name: com.mysql.cj.jdbc.Driver
# mysql8之前
# driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/flowable?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT&nullCatalogMeansCurrent=true
username: root
password: root
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
open-in-view: true
3.流程引擎配置文件flowable.cfg.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration"
class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/flowable?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT&nullCatalogMeansCurrent=true" />
<property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="root" />
<property name="databaseSchemaUpdate" value="true" />
<property name="asyncExecutorActivate" value="false" />
</bean>
</beans>
4.log4j.properties配置
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n
5.启动类JavaSkillPointApplication
package sdata;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JavaSkillPointApplication {
public static void main(String[] args) {
SpringApplication.run(JavaSkillPointApplication.class, args);
}
}
6.TestController
package sdata.controller;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.ui.modeler.domain.AbstractModel;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author lzw
* @date 2024-08-20-17:51
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
@Autowired
private ModelService modelService;
@Autowired
private RepositoryService repositoryService;
@GetMapping("sub")
void sub(String key, String id, String name, Integer day) {
// 员工提交请假申请
Map<String, Object> map = new HashMap<>();
map.put("id", id);
map.put("day", day);
map.put("studentUser", name);
// leave为员工请假流程xml文件中的id
ProcessInstance a1 = runtimeService.startProcessInstanceByKey(key, map);
Task task = taskService.createTaskQuery().processInstanceId(a1.getId()).singleResult();
taskService.complete(task.getId());
}
@GetMapping("queryLeadTask")
Object queryLeadTask() {
List<Object> list = new ArrayList<>();
// 查询领导分组的任务
List<Task> teacher = taskService.createTaskQuery().taskCandidateGroup("a").list();
for (Task task : teacher) {
// 根据任务id查询当前任务参数
Map<String, Object> variables = taskService.getVariables(task.getId());
System.out.println(variables.get("day") + "," + variables.get("studentUser"));
list.add(variables);
}
return list;
}
@GetMapping("queryBossTask")
Object queryBossTask() {
List<Object> list = new ArrayList<>();
// 查询老板分组的任务
List<Task> teacher = taskService.createTaskQuery().taskCandidateGroup("b").list();
for (Task task : teacher) {
// 根据任务id查询当前任务参数
Map<String, Object> variables = taskService.getVariables(task.getId());
System.out.println(variables.get("day") + "," + variables.get("studentUser"));
list.add(variables);
}
return list;
}
@GetMapping("LeadApprovalTask")
void LeadApprovalTask(String id, String outcome) {
// 领导审批
List<Task> teacherTaskList = taskService.createTaskQuery().taskCandidateGroup("a").list();
Map<String, Object> teacherMap = new HashMap<>();
teacherMap.put("outcome", outcome);
for (Task teacherTask : teacherTaskList) {
Map<String, Object> variables = taskService.getVariables(teacherTask.getId());
if (variables.get("id").equals(id)) {
taskService.complete(teacherTask.getId(), teacherMap);
}
}
}
@GetMapping("boosApprovalTask")
void boosApprovalTask(String outcome, String id) {
// 老板审批
List<Task> teacherTaskList = taskService.createTaskQuery().taskCandidateGroup("b").list();
Map<String, Object> teacherMap = new HashMap<>();
teacherMap.put("outcome", outcome);
for (Task teacherTask : teacherTaskList) {
Map<String, Object> variables = taskService.getVariables(teacherTask.getId());
if (variables.get("id").equals(id)) {
taskService.complete(teacherTask.getId(), teacherMap);
}
}
}
@GetMapping("queryHistory")
void queryHistory() {
List<ProcessInstance> processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey("leave").orderByStartTime().desc().list();
if (CollectionUtils.isEmpty(processInstance)) {
System.out.println("------------------------------------------");
}
// 获取最近的一个流程
List<HistoricActivityInstance> activities = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstance.get(0).getId())
// 只查询已经完成的活动
.finished()
// 按照结束时间排序
.orderByHistoricActivityInstanceEndTime().desc()
.list();
List<String> collect = activities.stream().map(a -> "活动名称:" + a.getActivityName() + ";活动执行时间:" + a.getDurationInMillis() + "毫秒").collect(Collectors.toList());
for (String s : collect) {
System.out.println(s);
}
}
@GetMapping("test")
void test() {
// 发起请假
Map<String, Object> map = new HashMap<>();
map.put("day", 5);
map.put("studentUser", "小明");
ProcessInstance studentLeave = runtimeService.startProcessInstanceByKey("leave", map);
Task task = taskService.createTaskQuery().processInstanceId(studentLeave.getId()).singleResult();
taskService.complete(task.getId());
// 领导审批
List<Task> teacherTaskList = taskService.createTaskQuery().taskCandidateGroup("a").list();
Map<String, Object> teacherMap = new HashMap<>();
teacherMap.put("outcome", "通过");
for (Task teacherTask : teacherTaskList) {
taskService.complete(teacherTask.getId(), teacherMap);
}
// 老板审批
List<Task> principalTaskList = taskService.createTaskQuery().taskCandidateGroup("b").list();
Map<String, Object> principalMap = new HashMap<>();
principalMap.put("outcome", "通过");
for (Task principalTask : principalTaskList) {
taskService.complete(principalTask.getId(), principalMap);
}
// 查看历史
List<HistoricActivityInstance> activities = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(studentLeave.getId())
.finished()
.orderByHistoricActivityInstanceEndTime().asc()
.list();
for (HistoricActivityInstance activity : activities) {
System.out.println(activity.getActivityName());
}
}
@PostMapping("/deploy")
public Object deployModel(@RequestParam String modelId) {
Model model = modelService.getModel(modelId);
BpmnModel bpmnModel = modelService.getBpmnModel(model);
Deployment deployment = repositoryService.createDeployment()
.name(model.getName())
.addBpmnModel(model.getKey() + ".bpmn", bpmnModel).deploy();
return deployment.getId(); //部署ID
}
}
三、项目启动及流程配置
1.启动日志
2.登录:http://localhost:8088 账号:admin 密码:admin
3.进入流程引擎系统(这里我已经建好了一个流程,下面有完整的创建过程)
4.点击创建流程
5.创建新模型
6.进入流程绘制页面
四、自定义流程绘制(根据以下截图进行配置)
注:流程ID必须已字母开头
1.流程节点生成示意,点击其他图标会生成不同类型节点
2.开始节点配置
3.请假申请节点配置
4.领导审批节点配置
①基本配置
②点击候选组分配用户
5、驳回
6.通过
7.进行请假天数判断
①小于等于两天
②大于两天
8.老板审批
①基础配置
②分配用户
9.通过
10.驳回
11.流程保存
五、通过ID自动部署
1.数据库中找到act_de_model表,获取创建流程模型是对应的ID
2.调用部署接口传入ID进行部署
六、测试
1.请假申请
2.查询领导分组的任务
3.领导通过
4.查询老板分组的任务
5.老板通过