溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot下的工作流Activiti開發(fā)

發(fā)布時(shí)間:2020-06-23 12:05:34 來源:網(wǎng)絡(luò) 閱讀:978 作者:素顏豬 欄目:編程語言

1.準(zhǔn)備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>activiti.demo</groupId>

? ? <artifactId>activiti-demo</artifactId>

? ? <version>1.0-SNAPSHOT</version>

? ? <packaging>war</packaging>


? ? <name>activiti-demo</name>

? ? <description>spring-activiti-demo</description>


? ? <parent>

? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>

? ? ? ? <version>2.0.2.RELEASE</version>

? ? ? ? <relativePath/>

? ? </parent>


? ? <properties>

? ? ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

? ? ? ? <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

? ? ? ? <java.version>1.8</java.version>

? ? </properties>


? ? <dependencies>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.projectlombok</groupId>

? ? ? ? ? ? <artifactId>lombok</artifactId>

? ? ? ? ? ? <version>1.16.20</version>

? ? ? ? ? ? <scope>provided</scope>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? <artifactId>spring-boot-starter</artifactId>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.activiti</groupId>

? ? ? ? ? ? <artifactId>activiti-spring-boot-starter-basic</artifactId>

? ? ? ? ? ? <version>6.0.0</version>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>mysql</groupId>

? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>

? ? ? ? ? ? <version>5.1.35</version>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId>

? ? ? ? ? ? <scope>test</scope>

? ? ? ? </dependency>

? ? </dependencies>


? ? <build>

? ? ? ? <plugins>

? ? ? ? ? ? <plugin>

? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>

? ? ? ? ? ? </plugin>

? ? ? ? </plugins>

? ? </build>

</project>

-----------------------------------------------------------------------------------------------------------------------

2.準(zhǔn)備流程文件one-task-process.bpmn20.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/testm1539766523202" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1539766523202" name="" targetNamespace="http://www.activiti.org/testm1539766523202" typeLanguage="http://www.w3.org/2001/XMLSchema">

? ? <process id="leave1" isClosed="false" isExecutable="true" processType="None">

? ? ? ? <startEvent id="_2" name="start"/>

? ? ? ? <userTask activiti:assignee="${leave.userId}" activiti:exclusive="true" id="_3" name="submit"/>

? ? ? ? <exclusiveGateway gatewayDirection="Unspecified" id="_4" name="result"/>

? ? ? ? <userTask activiti:assignee="${leave.approver1}" activiti:exclusive="true" id="_5" name="approve1"/>

? ? ? ? <exclusiveGateway gatewayDirection="Unspecified" id="_6" name="result"/>

? ? ? ? <userTask activiti:assignee="${leave.approver2}" activiti:exclusive="true" id="_7" name="approve2"/>

? ? ? ? <exclusiveGateway gatewayDirection="Unspecified" id="_8" name="result"/>

? ? ? ? <endEvent id="_9" name="end"/>

? ? ? ? <sequenceFlow id="_10" sourceRef="_2" targetRef="_3"/>

? ? ? ? <sequenceFlow id="_11" sourceRef="_3" targetRef="_4"/>

? ? ? ? <sequenceFlow id="_12" name="y" sourceRef="_4" targetRef="_5">

? ? ? ? ? ? <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.submit==true}]]></conditionExpression>

? ? ? ? </sequenceFlow>

? ? ? ? <sequenceFlow id="_13" name="n" sourceRef="_4" targetRef="_9">

? ? ? ? ? ? <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.submit==false}]]></conditionExpression>

? ? ? ? </sequenceFlow>

? ? ? ? <sequenceFlow id="_14" sourceRef="_5" targetRef="_6"/>

? ? ? ? <sequenceFlow id="_15" name="n" sourceRef="_6" targetRef="_7">

? ? ? ? ? ? <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree1==true}]]></conditionExpression>

? ? ? ? </sequenceFlow>

? ? ? ? <sequenceFlow id="_16" sourceRef="_7" targetRef="_8"/>

? ? ? ? <sequenceFlow id="_17" name="y" sourceRef="_6" targetRef="_3">

? ? ? ? ? ? <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree1==false}]]></conditionExpression>

? ? ? ? </sequenceFlow>

? ? ? ? <sequenceFlow id="_18" name="n" sourceRef="_8" targetRef="_3">

? ? ? ? ? ? <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree2==false}]]></conditionExpression>

? ? ? ? </sequenceFlow>

? ? ? ? <sequenceFlow id="_19" name="y" sourceRef="_8" targetRef="_9">

? ? ? ? ? ? <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree2==true}]]></conditionExpression>

? ? ? ? </sequenceFlow>

? ? </process>

</definitions>

-----------------------------------------------------------------------------------------------------------------------

3.準(zhǔn)備SpringBoot屬性文件application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xh_activiti?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=123456

spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.show-sql=true

server.port=8082

server.tomcat.uri-encoding=UTF-8


spring.activiti.history-level=full

spring.activiti.database-schema-update=true

spring.activiti.check-process-definitions=false

-----------------------------------------------------------------------------------------------------------------------

4.實(shí)體類

包名:com.springboot.demo.domain

類名:Leave

實(shí)現(xiàn):

package com.springboot.demo.domain;


import lombok.Data;


import java.io.Serializable;

import java.util.Date;


/**

?* @ClassName Leave

?* @Description TODO

?* @Author yunshuodeng

?* @Date 2019-05-05 15:47

?* @Version 1.0

?**/

@Data

public class Leave implements Serializable {

? ? private String userId;


? ? private Boolean submit;


? ? private String startDate;


? ? private String endDate;


? ? private float totalDay;


? ? private String desc;


? ? private String taskId;


? ? private String taskName;


? ? private String approver1;


? ? private Boolean agree1;


? ? private String approveDesc1;


? ? private String approver2;


? ? private Boolean agree2;


? ? private String approveDesc2;


? ? private String processId;

}

-----------------------------------------------------------------------------------------------------------------------

5.控制器

包名:com.springboot.demo.controller

類名:LeaveController

實(shí)現(xiàn):

package com.springboot.demo.controller;


import com.springboot.demo.domain.Leave;

import org.activiti.engine.HistoryService;

import org.activiti.engine.ProcessEngine;

import org.activiti.engine.RuntimeService;

import org.activiti.engine.TaskService;

import org.activiti.engine.history.HistoricProcessInstance;

import org.activiti.engine.runtime.ProcessInstance;

import org.activiti.engine.task.Task;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.CollectionUtils;

import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


/**

?* @ClassName LeaveController

?* @Description TODO

?* @Author yunshuodeng

?* @Date 2019-05-05 15:41

?* @Version 1.0

?**/

@RestController

@RequestMapping("level/v1")

public class LeaveController {

? ? public static final Logger log = LoggerFactory.getLogger(LeaveController.class);


? ? // 注入流程實(shí)例進(jìn)行管理與控制的服務(wù)

? ? @Autowired

? ? private RuntimeService runtimeService;


? ? // 注入流程任務(wù)進(jìn)行管理服務(wù)

? ? @Autowired

? ? private TaskService taskService;


? ? // 注入流程引擎

? ? @Autowired

? ? private ProcessEngine processEngine;


? ? /**

? ? ?* 部門主管審批

? ? ?* @param leave

? ? ?* @return

? ? ?*/

? ? @RequestMapping(value = "/approve2", method = RequestMethod.POST)

? ? public Map<String, Object> approve2(@RequestBody Leave leave){

? ? ? ? Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();

? ? ? ? Map<String, Object> vars = new HashMap<>();

? ? ? ? Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");

? ? ? ? origin.setApproveDesc2(leave.getApproveDesc2());

? ? ? ? origin.setAgree2(leave.getAgree2());

? ? ? ? vars.put("leave", origin);

? ? ? ? taskService.complete(leave.getTaskId(),vars);

? ? ? ? Map<String, Object> resultMap = new HashMap<>();

? ? ? ? resultMap.put("taskId",task.getId());

? ? ? ? return resultMap;

? ? }


? ? /**

? ? ?* 直接主管審批

? ? ?* @param leave

? ? ?* @return

? ? ?*/

? ? @RequestMapping(value = "/approve1", method = RequestMethod.POST)

? ? public Map<String, Object> approve1(@RequestBody Leave leave){

? ? ? ? Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();

? ? ? ? Map<String, Object> vars = new HashMap<>();

? ? ? ? Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");

? ? ? ? origin.setApproveDesc1(leave.getApproveDesc1());

? ? ? ? origin.setAgree1(leave.getAgree1());

? ? ? ? vars.put("leave", origin);

? ? ? ? taskService.complete(leave.getTaskId(),vars);

? ? ? ? Map<String, Object> resultMap = new HashMap<>();

? ? ? ? resultMap.put("taskId",task.getId());

? ? ? ? return resultMap;

? ? }



? ? /**

? ? ?* 查詢用戶流程

? ? ?* @param userId

? ? ?* @return

? ? ?*/

? ? @RequestMapping(value = "/find", method = RequestMethod.GET)

? ? public Map<String, Object> find(@RequestParam("userId")String userId){

? ? ? ? List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).list();

? ? ? ? System.out.println(taskList.size());

? ? ? ? List<Leave> resultList = new ArrayList<>();

? ? ? ? if(!CollectionUtils.isEmpty(taskList)){

? ? ? ? ? ? for(Task task : taskList){

? ? ? ? ? ? ? ? Leave leave = (Leave) taskService.getVariable(task.getId(),"leave");

? ? ? ? ? ? ? ? leave.setTaskId(task.getId());

? ? ? ? ? ? ? ? leave.setTaskName(task.getName());

? ? ? ? ? ? ? ? resultList.add(leave);

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? Map<String, Object> resultMap = new HashMap<>();

? ? ? ? resultMap.put("datas", resultList);

? ? ? ? return resultMap;

? ? }


? ? /**

? ? ?* 填寫請(qǐng)假單

? ? ?* @param leave

? ? ?* @return

? ? ?*/

? ? @RequestMapping(value = "apply",method = RequestMethod.POST)

? ? public Map<String,Object> apply(@RequestBody Leave leave){

? ? ? ? Task task = taskService.createTaskQuery().processInstanceId(leave.getProcessId()).singleResult();

? ? ? ? Map<String,Object> map = new HashMap<>();

? ? ? ? Leave origin = (Leave) taskService.getVariable(task.getId(),"leave");

? ? ? ? origin.setDesc(leave.getDesc());

? ? ? ? origin.setStartDate(leave.getStartDate());

? ? ? ? origin.setEndDate(leave.getEndDate());

? ? ? ? origin.setTotalDay(leave.getTotalDay());

? ? ? ? origin.setApprover1(leave.getApprover1());

? ? ? ? origin.setApprover2(leave.getApprover2());

? ? ? ? origin.setSubmit(leave.getSubmit());

? ? ? ? map.put("leave",origin);

? ? ? ? taskService.complete(task.getId(),map);


? ? ? ? Map<String,Object> resultMap = new HashMap<>();

? ? ? ? resultMap.put("taskId",task.getId());

? ? ? ? return resultMap;


? ? }


? ? /**

? ? ?* 啟動(dòng)流程

? ? ?* @param userId

? ? ?* @return

? ? ?*/

? ? @RequestMapping(value = "/start",method = RequestMethod.GET)

? ? public Map<String,Object> start(@RequestParam String userId){

? ? ? ? Map<String,Object> map = new HashMap<>();

? ? ? ? Leave leave = new Leave();

? ? ? ? leave.setUserId(userId);

? ? ? ? map.put("leave",leave);


? ? ? ? ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave1",map);


? ? ? ? Map<String,Object> resultMap = new HashMap<>();

? ? ? ? resultMap.put("processId",processInstance.getId());


? ? ? ? return resultMap;

? ? }


}

-----------------------------------------------------------------------------------------------------------------------

6.啟動(dòng)應(yīng)用,并按如下順序進(jìn)行訪問

1)啟動(dòng)流程

訪問地址:http://localhost:8082/level/v1/start?userId=10

返回結(jié)果:{"processId":"10001"}

2)填寫請(qǐng)假單申請(qǐng)

訪問地址:http://localhost:8082/level/v1/apply

參數(shù)列表:

{

"desc":"測(cè)試",

"startDate":"2019-05-05",

"endDate":"2019-05-09",

"totalDay":4,

"approver1":11,

"approver2":12,

"submit":true,

"processId":"10001"

}

返回結(jié)果:

{

? ? "taskId": "10010"

}

3)查看流程中待辦任務(wù)列表

訪問地址:http://localhost:8082/level/v1/find?userId=11

返回結(jié)果:{"datas":[{"userId":"10","submit":true,"startDate":"2019-05-05","endDate":"2019-05-09","totalDay":4.0,"desc":"測(cè)試","taskId":"10016","taskName":"approve1","approver1":"11","agree1":null,"approveDesc1":null,"approver2":"12","agree2":null,"approveDesc2":null,"processId":null}]}

4)直接領(lǐng)導(dǎo)審批

訪問地址:http://localhost:8082/level/v1/approve1

請(qǐng)求參數(shù):

{

"taskId":10016,

"approveDesc1":"同意",

"agree1":true

}

返回結(jié)果:{

? ? "taskId": "10016"

}

5)部門領(lǐng)導(dǎo)審批

查看流程中待辦任務(wù)列表

訪問地址:http://localhost:8082/level/v1/find?userId=12

返回結(jié)果:

{"datas":[{"userId":"10","submit":true,"startDate":"2019-05-05","endDate":"2019-05-09","totalDay":4.0,"desc":"測(cè)試","taskId":"10022","taskName":"approve2","approver1":"11","agree1":true,"approveDesc1":"同意","approver2":"12","agree2":null,"approveDesc2":null,"processId":null}]}

執(zhí)行審批操作

訪問地址:http://localhost:8082/level/v1/approve2

請(qǐng)求參數(shù):

{

"taskId":10022,

"approveDesc2":"同意",

"agree2":true

}

返回結(jié)果:

{

? ? "taskId": "10022"

}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI