Bootstrap

activiti5基础和springboot整合

说明

主要针对springboot和activiti5进行整合使用。

开发环境:

  • jdk1.8
  • activiti5.22.0
  • mysql5.7.26

软件推荐:

什么是activiti?

自行百度

pom依赖项(包括了整合springboot所需的)

<?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>org.example</groupId>
    <artifactId>activiti5-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <properties>
        <mysql.version>8.0.19</mysql.version>
        <activiti.version>5.22.0</activiti.version>
        <mybatis.version>2.1.3</mybatis.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>
        <!--activiti-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>${activiti.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

普通方式生成数据表

java

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;

@Slf4j
public class InitTables {
   

    public static void main(String[] args) {
   
        init2();
    }

    /**
     * 方式1
     */
    public static void init1() {
   
        ProcessEngineConfiguration configuration
                = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        configuration.setDatabaseSchemaUpdate("true");
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti5_study?serverTimezone=PRC&nullCatalogMeansCurrent=true&useUnicode=true&charaterEncoding=utf-8&useSSL=false");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("123456");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        log.info("成功获取到引擎={},初始化数据表完成",processEngine);
    }

    /**
     * 方式2,用这种方式必须保证resource目录下存在activiti.cfg.xml文件,
     * 否则defaultProcessEngine为null并且不能初始化出25张数据表
     */
    public static void init2() {
   
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        log.info("成功获取到引擎={},初始化数据表完成",defaultProcessEngine);
    }

    /**
     * 方式3,用这种方式必须保证resource目录下存在activiti.cfg.xml文件,
     * 否则会抛出异常并且不能初始化出25张数据表
     */
    public static void init3() {
   
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault().buildProcessEngine();
        log.info("成功获取到引擎={},初始化数据表完成",processEngine);
    }

}

activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contex
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 这里可以使用 链接池 dbcp-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/activiti5_study?serverTimezone=PRC&amp;nullCatalogMeansCurrent=true&amp;useUnicode=true&amp;charaterEncoding=utf-8&amp;useSSL=false" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <property name="maxActive" 
;