1、创建好jfinal-maven项目后,在pom.xml文件中引入activiti7.1相关jar包:
<!--activiti--> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>7.1.0.M6</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-model</artifactId> <version>7.1.0.M6</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-converter</artifactId> <version>7.1.0.M6</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-json-converter</artifactId> <version>7.1.0.M6</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-layout</artifactId> <version>7.1.0.M6</version> </dependency> <!-- mysql 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>
其中根据自己mysql版本引入对应的驱动,我本机是mysql8
2、在src/main/resources下创建activiti.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.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/数据库名?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT" /> <property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver" /> <property name="jdbcUsername" value="用户名" /> <property name="jdbcPassword" value="密码" /> <property name="databaseSchemaUpdate" value="true" /> </bean> </beans>
3、在src/main/resources下创建bpmn文件,自行百度,网上一大堆
4、测试类:
package process; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; public class Demo01 { /** * 流程变量测试 */ //获取默认流程引擎实例,会自动读取activiti.cfg.xml文件创建表 private ProcessEngine pe=ProcessEngines.getDefaultProcessEngine(); //部署流程 @Test public void deploy() { System.out.println(pe); Deployment deploy=pe.getRepositoryService() //获取部署相关service .createDeployment()//创建部署 .addClasspathResource("diagrams/student01.bpmn")// // .addClasspathResource("diagrams/StudentLeave02.png")//加载资源文件 .name("学生请假流程1")//流程名称 .deploy();//部署 System.out.println("流程id:"+deploy.getId()); System.out.println("流程名称:"+deploy.getName()); } //启动流程实例 @Test public void start() { ProcessInstance pi=pe.getRuntimeService()//运行service .startProcessInstanceByKey("studentleave1");//流程定义表的id值 System.out.println("流程实例ID:"+pi.getId()); System.out.println("流程定义ID:"+pi.getProcessDefinitionId()); } //查看任务 @Test public void findTask() { List<Task> list=pe.getTaskService()//获取相关service .createTaskQuery()//创建任务查询 .taskAssignee("小红")//指定某个人 .list(); for(Task task:list) { System.out.println("任务id:"+task.getId()); System.out.println("任务名称:"+task.getName()); System.out.println("任务创建时间:"+task.getCreateTime()); System.out.println("任务委派人:"+task.getAssignee()); System.out.println("流程实例id:"+task.getProcessInstanceId()); } } //完成任务 @Test public void complete() { pe.getTaskService()//任务相关service .complete("65002");//填入对应任务id } /** * 设置流程变量 */ @Test public void Set01() { TaskService ts=pe.getTaskService(); String taskId="55004"; ts.setVariable(taskId, "days", 3); ts.setVariable(taskId, "date", new Date()); ts.setVariable(taskId, "reason", "感冒了!"); // Student s=new Student("001","张三"); // ts.setVariable(taskId, "student", s); } /** * 设置流程变量 */ @Test public void Get01() { TaskService ts=pe.getTaskService(); String taskId="65002"; Integer days=(Integer) ts.getVariable(taskId, "days"); Date date=(Date) ts.getVariable(taskId, "date"); String reason=(String) ts.getVariable(taskId, "reason"); // Student s=(Student) ts.getVariable(taskId, "student"); // System.out.println("请假人id:"+s.getId()); // System.out.println("请假人:"+s.getName()); // System.out.println("请假天数:"+days); // System.out.println("请假时间:"+date); // System.out.println("请假理由:"+reason); } /** * 使用map设置流程变量 */ @Test public void Set02() { TaskService ts=pe.getTaskService(); String taskId="67504"; // Student s=new Student("001","张三"); Map<String, Object> variables=new HashMap<String, Object>(); variables.put("days", 6); variables.put("date", new Date()); variables.put("reason", "感冒了02!"); // variables.put("student", s); ts.setVariables(taskId, variables); } /** * 设置流程变量 */ @Test public void Get02() { TaskService ts=pe.getTaskService(); String taskId="67504"; Map<String, Object> map=ts.getVariables(taskId); Integer days=(Integer) map.get("days"); Date date=(Date) map.get("date"); String reason=(String) map.get("reason"); // Student s=(Student) map.get("student"); // System.out.println("请假人id:"+s.getId()); // System.out.println("请假人:"+s.getName()); System.out.println("请假天数:"+days); System.out.println("请假时间:"+date); System.out.println("请假理由:"+reason); } }
(1)执行
deploy()方法,创建25张表,并且在对应表里面添加对应数据
(2)从上往下,执行对应方法,在对应表里面都有对应数据,自行摸索。
本文参考链接:http://blog.java1234.com/blog/articles/157.html