activiti表的创建和简单示例

2023-10-12 14:03:44 浏览数 (1)

activiti23表的创建

代码语言:javascript复制
package junit;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;

public class TestActiviti {
    /**
     * 第一种方式:使用代码创建工作流需要的23张表
     */
    @Test
    public void createActivitiTable() {
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
        processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/itcastactiviti?useUnicode=true&characterEncoding=utf8");
        processEngineConfiguration.setJdbcUsername("root");
        processEngineConfiguration.setJdbcPassword("123456");

        //DB_SCHEMA_UPDATE_TRUE 如果表不存在 自动创建表
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        //工作流核心对象 processEngine 
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("processEngine:" processEngine);
    }

    /**
     * 第二种方式:配置文件测试
     */
    @Test
    public void createActivitiCfgTable() {
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("processEngine:" processEngine);
    }
}

其中配置文件如下:

代码语言:javascript复制
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcastactiviti?useUnicode=true&amp;characterEncoding=utf8"></property>
        <property name="jdbcUsername" value="root"></property>
        <property name="jdbcPassword" value="123456"></property>
        <!-- 没有表 创建表  -->
        <property name="databaseSchemaUpdate" value="true"></property>
    </bean>

</beans>

第三种方式: sql语句创建23张表

activiti-5.13databasecreate 文件中

流程定义 启动 查询当前个人任务和完成我的任务

代码语言:javascript复制
package cn.panda.a_helloworld;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class HelloWorld {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    /**
     * 部署流程定义
     */
    @Test
    public void deployementProcessDefinition() {
        Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署相关的service
                        .createDeployment()//创建部署对象
                        .name("helloworld入门程序")//添加部署对象
                        .addClasspathResource("diagrams/helloworld.bpmn")//从classpath资源中加载 一次加载一个文件
                        .addClasspathResource("diagrams/helloworld.png")//从classpath资源中加载 一次加载一个文件
                        .deploy();//完成部署
        System.out.println(deployment.getId());
        System.out.println(deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance() {
        //使用key值启动 默认按照最新版本的流程定义启动
        String processDefinitionKey="helloworld";
        ProcessInstance processInstance = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service
                        .startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例 key对应bpmn文件的id属性值
        System.out.println("流程实例id" processInstance.getId());//流程实例id
        System.out.println("流程定义ID" processInstance.getProcessDefinitionId());//流程定义ID
    }

    /**
     * 查询当前人的个人任务
     */
    @Test
    public void findMyPersonalTask() {
        String assignee="王五";
        List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
                        .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());

            }
        }
    }

    /**
     * 完成我的任务
     */
    @Test
    public void completeMyPersonalTask() {
        String taskId = "302";
        processEngine.getTaskService()
                        .complete(taskId);
        System.out.println("完成任务 任务ID:" taskId);
    }
}

流程图:

0 人点赞