0 maven包
<?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.itheima</groupId>
<artifactId>activit01</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<activiti.version>7.0.0.Beta1</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>
</dependency>
<!-- bpmn 模型处理 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 转换 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn json数据转换 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 布局 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- activiti 云支持 -->
<dependency>
<groupId>org.activiti.cloud</groupId>
<artifactId>activiti-cloud-services-api</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- 链接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>
1 idea 插件安装
2创建activiti.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contex
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 默认id对应的值 为processEngineConfiguration -->
<!-- processEngine Activiti的流程引擎 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///activiti"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="3"/>
<property name="maxIdle" value="1"/>
</bean>
<!--在默认方式下 bean的id 固定为 processEngineConfiguration-->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!--引入上面配置好的 链接池-->
<property name="dataSource" ref="dataSource"/>
<!--actviti数据库表在生成时的策略 true - 如果数据库中已经存在相应的表,那么直接使用,如果不存在,那么会创建-->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
3 创建数据库配置文件
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r[%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=E:\activiti\activiti.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r[%15.15t] %-5p %30.30c %x - %m\n
4 数据库 创建Activiti表格
5 使用代码创建25张表格
/**
* 生成 activiti的数据库表
*/
@Test
public void testCreateDbTable(){
//使用classpath下的activiti.cfg.xml中的配置创建processEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(processEngine);
}
6 使用idea 创建任务bmnp文件
Activiti创建bpmn任务
bilibili更加清晰—Activiti创建bpmn任务
7 整体操作流程(简单的部署)
7.1 部署任务
@Test
public void testDeployment(){
//1.创建processEngine
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//2.获取
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.name("出差申请流程")
.addClasspathResource("bpmn/text.bpmn")
.addClasspathResource("bpmn/text.png")
.deploy();
//输出信息
System.out.println("流程部署id:"+deploy.getId());
System.out.println("流程部署名字:"+deploy.getName());
}
数据里面的表格
7.2 启动
关于id
@Test
public void testStart(){
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
//taskid 看上图
ProcessInstance instance = runtimeService.startProcessInstanceByKey("taskid");
System.out.println("流程定义ID:"+instance.getProcessDefinitionId());
System.out.println("流程实例ID:"+instance.getId());
System.out.println("当前活动ID:"+instance.getActivityId());
}
执行完毕之后 数据库里面
7.3 查询个人待执行任务
/**
* 查询当前个人待执行的任务
*/
@Test
public void testFindPersonalTaskList() {
// 任务负责人
String assignee = "李四";
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 创建TaskService
TaskService taskService = processEngine.getTaskService();
// 根据流程key 和 任务负责人 查询任务
List<Task> list = taskService.createTaskQuery()
.processDefinitionKey("taskid") //流程Key
.taskAssignee(assignee)//只查询该任务负责人的任务
.list();
for (Task task : list) {
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("任务名称:" + task.getName());
}
}
控制台输出
流程实例id:140001
任务id:140005
任务负责人:李四
任务名称:创建任务
7.4 完成个人任务(创建任务:李四)
@Test
public void completTask(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery()
.processDefinitionKey("taskid")
.taskAssignee("李四")
.singleResult();
taskService.complete(task.getId());
}
数据库里面
7.5 完成个人任务(审核任务:总经理)
@Test
public void completTask(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery()
.processDefinitionKey("taskid")
.taskAssignee("总经理")
.singleResult();
taskService.complete(task.getId());
}
数据库 act_ru_task 已经没有任务了
7.6 查询流程
/**
* 查询流程定义
*/
@Test
public void queryPrecessDefinition(){
//获取引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取 repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取 processDefinitionQuery 对象
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//查询当前所有的流程定义 返回流程定义信息的集合
//processDefinitionKey(流程定义key)
//orderByProcessDefinitionVersion 根据版本排序
//desc 倒序
//list 查询出所有的内容
//查询的是 act_re_procdef表
List<ProcessDefinition> processDefinitionList = processDefinitionQuery.processDefinitionKey("taskid")
.orderByProcessDefinitionVersion()
.desc()
.list();
//输出信息
for (ProcessDefinition processDefinition : processDefinitionList) {
System.out.println("流程定义ID:"+processDefinition.getId());
System.out.println("流程定义名称:"+processDefinition.getName());
System.out.println("流程定义key:"+processDefinition.getKey());
System.out.println("流程定义版本:"+processDefinition.getVersion());
System.out.println("流程部署ID:"+processDefinition.getDeploymentId());
}
}
7.7 删除流程(根据部署id)
/**
* 删除流程部署信息
* 当前的流程如果没有完成,想要删除的话需要使用特殊方式,原理就是 级联删除
*/
@Test
public void deleteDeployMent(){
/**
* 被操作的表
* act_re_deployment
* act_re_procdef
* act_ge_bytearray
*/
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//通过引擎来获取 repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
//通过部署ID来删除流程部署信息
String deploymentId = "137501";
repositoryService.deleteDeployment(deploymentId);
//下行为强制删除
//repositoryService.deleteDeployment(deploymentId,true);
}
7.8 从数据库下载bpmn与png
/**
* 下载资源文件
* 方案1:使用activiti提供的api,来下载资源文件
* 方案2:使用自己的方式 从数据库中下载文件 使用jdbc对blob 类型,clob类型读取出来,保存到文件目录
* 结果IO操作:commons-io.jar
*/
@Test
public void getDeployment() throws FileNotFoundException {
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("taskid")
.singleResult();
String deploymentId = processDefinition.getDeploymentId();
InputStream resourceAsStreamPng = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
InputStream resourceAsStreamBpmn = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
File pngFile = new File("E:/activiti/taskid.png");
File bpmnFile = new File("E:/activiti/taskid.bpmn");
try {
FileOutputStream fileOutputStreamPng = new FileOutputStream(pngFile);
FileOutputStream fileOutputStreamBpmn = new FileOutputStream(bpmnFile);
IOUtils.copy(resourceAsStreamPng,fileOutputStreamPng);
IOUtils.copy(resourceAsStreamBpmn,fileOutputStreamBpmn);
fileOutputStreamPng.close();
resourceAsStreamPng.close();
fileOutputStreamBpmn.close();
resourceAsStreamBpmn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
下载后的效果演示
8 整体操作流程(变量的方式)
8.1 创建bmnp文件
**视频中有一处错误请注意 **
Activiti创建有分支的bpmn【视频中 v0 应该修改成${v0}】
bilibili更加清晰—Activiti创建有分支的bpmn【视频中 v0 应该修改成${v0}】
8.2 部署
//注意次key 下面都会用到 与 8.1创建bmnp的key是相关的
String key = "evectionVariablesTest";
@Test
public void testDeployment(){
//1.创建processEngine
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//2.获取
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.name("出差申请流程-有分支的")
.addClasspathResource("bpmn/evection-variables-test.bpmn")
.addClasspathResource("bpmn/evection-variables-test.png")
.deploy();
//输出信息
System.out.println("流程部署id:"+deploy.getId());
System.out.println("流程部署名字:"+deploy.getName());
/**
* 流程部署id:160001
* 流程部署名字:出差申请流程-有分支的
*/
}
数据库
8.3 创建类(Evection ) 注意类的名字与 bmnp里面的相关
package com.itheima.demo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 出差申请中的流程变量对象
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Evection implements Serializable {
/**
* 主键ID
*/
private Long id;
/**
* 出差单的名字
*/
private String evectionName;
/**
* 出差天数
*/
private double num;
/**
* 出差的开始时间
*/
private Date beginDate;
/**
* 出差的结束时间
*/
private Date endDate;
/**
* 出差的目的地
*/
private String destination;
/**
* 出差的原因
*/
private String reson;
}
8.3 启动
@Test
public void testStart(){
//获取流程引擎
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//获取runtimeService
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
//流程变量的map
Map<String,Object> variables = new HashMap<>();
//设置流程变量
Evection evection = new Evection();
//设置出差日期
evection.setNum(3d);
//把流程变量的pojo放入map
variables.put("evection",evection);
//把设定任务的负责人
variables.put("v0","v0创建者李四");
variables.put("v1","v1审核者王经理");
variables.put("v2","v2审核者万总经理");
variables.put("v3","v3审核者小小财务");
ProcessInstance instance = runtimeService.startProcessInstanceByKey(key,variables);
System.out.println("流程定义ID:"+instance.getProcessDefinitionId());
System.out.println("流程实例ID:"+instance.getId());
System.out.println("当前活动ID:"+instance.getActivityId());
/**
* 流程定义ID:evectionVariablesTest:1:160004
* 流程实例ID:162501
* 当前活动ID:null
*/
}
数据库
表act_ru_task
表act_ru_variable
8.4 查询个人待执行任务
/**
* 查询当前个人待执行的任务
*/
@Test
public void testFindPersonalTaskList() {
// 任务负责人
String assignee = "v0创建者李四";
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 创建TaskService
TaskService taskService = processEngine.getTaskService();
// 根据流程key 和 任务负责人 查询任务
List<Task> list = taskService.createTaskQuery()
.processDefinitionKey(key) //流程Key
.taskAssignee(assignee)//只查询该任务负责人的任务
.list();
for (Task task : list) {
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("任务名称:" + task.getName());
}
/**
* 流程实例id:162501
* 任务id:162512
* 任务负责人:v0创建者李四
* 任务名称:创建任务
*/
}
8.5 完成个人任务
v0创建者李四
/**
* 完成个人任务
*/
@Test
public void completTaskV0(){
String assingee = "v0创建者李四";
//获取流程引擎
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//taskService
TaskService taskService = defaultProcessEngine.getTaskService();
//完成任务
Task task = taskService.createTaskQuery()
.processDefinitionKey(key)
.taskAssignee(assingee)
.singleResult();
if(task!=null){
//完成任务 根据id来完成
taskService.complete(task.getId());
System.out.println(task);
}
}
v1审核者王经理
/**
* 完成个人任务
*/
@Test
public void completTaskV1(){
String assingee = "v1审核者王经理";
//获取流程引擎
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//taskService
TaskService taskService = defaultProcessEngine.getTaskService();
//完成任务
Task task = taskService.createTaskQuery()
.processDefinitionKey(key)
.taskAssignee(assingee)
.singleResult();
if(task!=null){
//完成任务 根据id来完成
taskService.complete(task.getId());
System.out.println(task);
}
}
- 当v1审核者王经理 完成任务时 有一个 分支 需要根据 num来判断
- 本列中 num=3 是启动的时候 赋值的 [evection.setNum(3d)摘自启动里面的代码]
- 所以 num>=3 应该执行 总经理审批
- 查看一下数据库里面的 act_ru_task 看一下待执行人
v2审核者万总经理
/**
* 完成个人任务
*/
@Test
public void completTaskV2(){
String assingee = "v2审核者万总经理";
//获取流程引擎
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//taskService
TaskService taskService = defaultProcessEngine.getTaskService();
//完成任务
Task task = taskService.createTaskQuery()
.processDefinitionKey(key)
.taskAssignee(assingee)
.singleResult();
if(task!=null){
//完成任务 根据id来完成
taskService.complete(task.getId());
System.out.println(task);
}
}
此时已经走到了小小财务
v3审核者小小财务 【最终完成act_ru_task里面的数据删除 】
/**
* 完成个人任务
*/
@Test
public void completTaskV3(){
String assingee = "v3审核者小小财务";
//获取流程引擎
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//taskService
TaskService taskService = defaultProcessEngine.getTaskService();
//完成任务
Task task = taskService.createTaskQuery()
.processDefinitionKey(key)
.taskAssignee(assingee)
.singleResult();
if(task!=null){
//完成任务 根据id来完成
taskService.complete(task.getId());
System.out.println(task);
}
}