SpringBoot整合activiti7学生请假示例

参考博客:https://blog.csdn.net/weixin_44096448/article/details/103613377?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.baidujsUnder6&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.baidujsUnder6

Java版本:1.8

SpringBoot版本2.4.4(SpringBoot和Activit7最好都用最新版本)


            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
        

        
            com.github.xiaoymin
            knife4j-spring-boot-starter
            2.0.2
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.3.1
        

        
            p6spy
            p6spy
            3.9.1
        

        
            com.alibaba
            druid
            1.1.20
        

        
            mysql
            mysql-connector-java
            5.1.47
        

        
            org.activiti
            activiti-spring-boot-starter
            7.0.0.Beta3
        

配置文件

server:
  port: 8080
spring:
  #Activiti property configuration
  activiti:
    database-schema-update: true
    history-level: full
    db-history-used: true
  datasource:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://localhost:3306/activiti?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

安装actibpm插件SpringBoot整合activiti7学生请假示例_第1张图片

把编码设置成UTF8SpringBoot整合activiti7学生请假示例_第2张图片 SpringBoot整合activiti7学生请假示例_第3张图片

SpringBoot整合activiti7学生请假示例_第4张图片

-Dfile.encoding=UTF-8

SpringBoot整合activiti7学生请假示例_第5张图片

构建流程文件

SpringBoot整合activiti7学生请假示例_第6张图片

 SpringBoot整合activiti7学生请假示例_第7张图片

 SpringBoot整合activiti7学生请假示例_第8张图片

SpringBoot整合activiti7学生请假示例_第9张图片

 复制一份刚刚构建的流程文件,并将后缀名改为bpmn20.xml,打开后将报错的xmlns加上文件顶部的xmln约束

SpringBoot整合activiti7学生请假示例_第10张图片

SpringBoot整合activiti7学生请假示例_第11张图片

生成png文件,保存下来放到一起

SpringBoot整合activiti7学生请假示例_第12张图片

接口

package com.example.mindsa.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class TestController {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    /**
     * 项目启动部署请假流程引擎。
     */
    @PostConstruct
    public void prepare() {
        // 创建一个部署对象
        Deployment deployment = repositoryService.createDeployment()
                .name("请假流程")
                .addClasspathResource("qbhs_bpmn20.xml")
                .addClasspathResource("qbhs.png")
                .deploy();
        log.info("部署ID:" + deployment.getId());
        log.info("部署名称:" + deployment.getName());
    }

    @ApiOperation("发起流程")
    @PostMapping("startProcess")
    public void startProcess(@RequestParam String student, @RequestParam String teacher) {
        String processDefinitionKey = "test01";
        Map map = new HashMap<>();

        //使用UEL 表达式设置

        // 学生填写申请单    Assignee:${student}
        map.put("student", student);

        // 班主任审批    Assignee:${teacher}
        map.put("teacher", teacher);

        ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);

        log.info("流程实例ID:" + instance.getId());
        log.info("流程定义ID:" + instance.getProcessDefinitionId());
    }

    @ApiOperation("任务查询")
    @PostMapping("searchTask")
    public List searchTask() {
        //流程启动后,各各任务的负责人就可以查询自己当前需要处理的任务,查询出来的任务都是该用户的待办任务。
        List list = taskService.createTaskQuery()
                //流程实例key
                .processDefinitionKey("test01")
                //查询谁的任务
                //.taskAssignee("")
                .list();
        List idList = new ArrayList();

        for (Task task : list) {
            idList.add(task.getId());
            log.info("任务ID:" + task.getId());
            log.info("任务名称:" + task.getName());
            log.info("任务的创建时间:" + task.getCreateTime());
            log.info("任务的办理人:" + task.getAssignee());
            log.info("流程实例ID:" + task.getProcessInstanceId());
            log.info("执行对象ID:" + task.getExecutionId());
            log.info("流程定义ID:" + task.getProcessDefinitionId());
        }
        return idList;

    }

    @ApiOperation("任务处理")
    @PostMapping("disposeTask")
    public void disposeTask(List list) {
        for (String id : list) {
            // 任务id
            taskService.complete(id);
            System.out.println("处理任务id:" + id);
        }
    }
}

如果启动报错找不到act_ge_property表的话就在数据库连接加上

&nullCatalogMeansCurrent=true

例如

jdbc:mysql://localhost:3306/activiti?characterEncoding=UTF-8&nullCatalogMeansCurrent=true&serverTimezone=GMT&useSSL=fals

你可能感兴趣的:(流程引擎,spring,boot,restful,java)