springBoot2.1.5集成Activiti7.1.24

原文地址:https://www.xugj520.cn/archives/springBoot_Activiti.html

Activiti系列文章地址:https://www.xugj520.cn/category/Activiti/

一、使用IDEA快速搭建SpringBoot项目

请在百度中自行搜索
或者直接跳转:https://blog.csdn.net/frank_come/article/details/80824968

二、设置pom.xml

pom文件配置如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.example
    activiti7
    0.0.1-SNAPSHOT
    activiti7
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
        ${java.version}
        ${java.version}
        7.1.24
    

    
        
            
                org.activiti.dependencies
                activiti-dependencies
                ${activiti-dependencies.version}
                import
                pom
            
        
    

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

        
            mysql
            mysql-connector-java
            runtime
        

        
        
            com.alibaba
            druid
            1.1.16
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-security
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            alfresco
            Activiti Releases
            https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases/
            
                true
            
        
    




三、设计流程图

springBoot2.1.5集成Activiti7.1.24_第1张图片
流程图代码



  
    
      
      
      
    
    
    
    
    
    
    
    
    
    
      
    
    
      3}]]>
    
    
    
    
    
  
  
    
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
        
          
        
      
      
        
        
        
          
        
      
    
  

四、配置文件


spring:
    #Activiti property configuration
    activiti:
        database-schema-update: true
        history-level: full
        db-history-used: true

    datasource:
        url: jdbc:mysql://localhost:3306/activiti7?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&nullCatalogMeansCurrent=true
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource

        initialization-mode: always

        initialSize: 5
        minIdle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        filters: stat,wall,log4j
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500


logging:
    level:
        com.ascendant: debug
    pattern:
        console: "%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(-%5p)  %clr(${PID:- }){magenta} --- %clr([%15.15t]){faint} %highlight(%-80.80logger{300}){cyan} %clr(:) %m %n%wEx"

五、SpringBootTest测试

部署流程定义

    @Test
    public void prepare() {
        Deployment deployment = repositoryService.createDeployment()//创建一个部署对象
                .name("请假流程")
                .addClasspathResource("processes/myProcess.bpmn")
                .addClasspathResource("processes/myProcess.png")
                .deploy();
        System.out.println("部署ID:"+deployment.getId());
        System.out.println("部署名称:"+deployment.getName());
    }

启动流程实例分配任务给个人

    /**启动流程实例分配任务给个人*/
    @Test
    public void start() {

        String userKey="PTM";//脑补一下这个是从前台传过来的数据
        String processDefinitionKey ="myProcess";//每一个流程有对应的一个key这个是某一个流程内固定的写在bpmn内的
        HashMap variables=new HashMap<>();
        variables.put("userKey", userKey);//userKey在上文的流程变量中指定了
        variables.put("day", 2);
        variables.put("users", "a");

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

        System.out.println("流程实例ID:"+instance.getId());
        System.out.println("流程定义ID:"+instance.getProcessDefinitionId());
    }

查询当前人的个人任务

/**查询当前人的个人任务*/
    @Test
    public void findTask(){
        String assignee = "a";
        List list = taskService.createTaskQuery()//创建任务查询对象
                .taskAssignee(assignee)//指定个人任务查询
                .list();
        if(list!=null && list.size()>0){
            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());
                System.out.println("执行对象ID:"+task.getExecutionId());
                System.out.println("流程定义ID:"+task.getProcessDefinitionId());
                System.out.println("getOwner:"+task.getOwner());
                System.out.println("getCategory:"+task.getCategory());
                System.out.println("getDescription:"+task.getDescription());
                System.out.println("getFormKey:"+task.getFormKey());
                Map map = task.getProcessVariables();
                for (Map.Entry m : map.entrySet()) {
                    System.out.println("key:" + m.getKey() + " value:" + m.getValue());
                }
                for (Map.Entry m : task.getTaskLocalVariables().entrySet()) {
                    System.out.println("key:" + m.getKey() + " value:" + m.getValue());
                }

            }
        }
    }

完成任务

    @Test
    public void completeTask(){
        //任务ID
        String taskId = "6825fdf2-7e04-11e9-a0c6-408d5ccf513c";

        HashMap variables=new HashMap<>();
        variables.put("days", 1);//userKey在上文的流程变量中指定了

        taskService.complete(taskId,variables);
        System.out.println("完成任务:任务ID:"+taskId);
    }

查询当前人的组任务

    /**查询当前人的组任务*/
    @Test
    public void findTaskGroup(){

        String assignee = "a";
        List list = taskService.createTaskQuery()//创建任务查询对象
//                .taskCandidateUser("ZJ")//指定组任务查询
                .taskAssignee(assignee)
                .list();
        String taskid ="";
        String instanceId ="";
        if(list!=null && list.size()>0){
            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());
                System.out.println("执行对象ID:"+task.getExecutionId());
                System.out.println("流程定义ID:"+task.getProcessDefinitionId());
            }
        }
    }

参考网址:
https://blog.csdn.net/qq_40451631/article/details/84937251#21_ProcessEngine_5
https://blog.csdn.net/pan1458689676?t=1
https://salaboy.com/2018/08/13/getting-started-with-activiti-core-7-0-0-beta1/

你可能感兴趣的:(Activiti,Spring,Boot,Activiti)