美文网首页后端java
SpingBoot整合activiti6.0,Mysql

SpingBoot整合activiti6.0,Mysql

作者: 无我_a50f | 来源:发表于2019-05-22 12:33 被阅读0次

    1. 前置条件

         解压activiti6.0 安装包,进入 activiti-6.0.0/database/create/ 目录
    
         mysql数据库中创建数据库,这里命名为 activiti
    
        导入3个 sql 文件,activiti.mysql.create.*.sql (3个文件,* 代表不同的文字,这里不再详细说明)
    

    2. 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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.tianjishuju</groupId>
        <artifactId>project_xuji_backend_java</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>project_xuji_backend_java</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!-- springboot 基本依赖 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            
            <!-- activiti springboot 依赖 -->
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-spring-boot-starter-basic</artifactId>
                <version>6.0.0</version>
            </dependency>
    
            <!-- spring rest 服务依赖,这里必须用这个依赖,spring-boot-starter-rest 会导致其它问题产生 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
    
            <!-- mysql 驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <!-- activiti jpa 依赖,不使用也建议加上,否则在使用@Autowired 注册Bean 时编辑器会显示报错,其实不影响服务正常使用,加上后不再报错 -->
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-spring-boot-starter-jpa</artifactId>
                <version>6.0.0</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    3. yml配置

    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456
    

    4. idea打开项目加载依赖

    5. 添加资源

    resource目录下创建processes 目录
    并在此目录下创建 one-task-process.bpmn20.xml 文件
    文件内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <definitions
          xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
          xmlns:activiti="http://activiti.org/bpmn"
          targetNamespace="Examples">
    
      <process id="oneTaskProcess" name="The One Task Process">
          <startEvent id="theStart" />
          <sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
          <userTask id="theTask" name="my task" />
          <sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
          <endEvent id="theEnd" />
      </process>
    
    </definitions>
    

    6. 在 java 源码App主程序同级目录下创建 controller 和 service目录

    创建controller/MyRestController.java 、service/MyService.java 类
    MyRestController.java 内容如下:

    /**
    * @activiti 流程测试类, com.* 代表自己的包名
    */
    package com.*.controller;
    
    import com.*.service.MyService;
    import org.activiti.engine.task.Task;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController
    public class MyRestController {
    
       @Autowired
       private MyService myService;
    
       @RequestMapping(value="/process", method= RequestMethod.POST)
       public void startProcessInstance() {
           myService.startProcess();
       }
    
       @RequestMapping(value="/tasks", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
       public List<TaskRepresentation> getTasks(@RequestParam String assignee) {
           List<Task> tasks = myService.getTasks(assignee);
           List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
           for (Task task : tasks) {
               dtos.add(new TaskRepresentation(task.getId(), task.getName()));
           }
           return dtos;
       }
    
       static class TaskRepresentation {
    
           private String id;
           private String name;
    
           public TaskRepresentation(String id, String name) {
               this.id = id;
               this.name = name;
           }
    
           public String getId() {
               return id;
           }
           public void setId(String id) {
               this.id = id;
           }
           public String getName() {
               return name;
           }
           public void setName(String name) {
               this.name = name;
           }
    
       }
    
    }
    

    service/MyService.java 内容如下:

    package com.*.service;
    
    import org.activiti.engine.RuntimeService;
    import org.activiti.engine.TaskService;
    import org.activiti.engine.task.Task;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    @Service
    public class MyService {
    
        @Autowired
        private RuntimeService runtimeService;
    
        @Autowired
        private TaskService taskService;
    
        @Transactional
        public void startProcess() {
            runtimeService.startProcessInstanceByKey("oneTaskProcess");
        }
    
        @Transactional
        public List<Task> getTasks(String assignee) {
            return taskService.createTaskQuery().taskAssignee(assignee).list();
        }
    
    }
    

    7. 运行项目

    8. 访问

    分别访问 http://localhost:8080/process,
    http://localhost:8080/tasks?assignee=aaa

    相关文章

      网友评论

        本文标题:SpingBoot整合activiti6.0,Mysql

        本文链接:https://www.haomeiwen.com/subject/zwvyzqtx.html