建議收藏,從零開始創建一個Activiti工作流,手把手教你完成

環境配置

項目環境:
JDK1.8
tomcat7
maven3.5
開發工具:
IDEA
activiti7

創建項目

    目標:創建一個maven項目,集成Activiti,並自動生成25張數據庫表

準備工作

    在數據庫中運行:
    CREATE DATABASE activiti DEFAULT CHARACTER SET utf8;
    用於創建Activiti數據庫
    在項目中會使用到BPMN圖形,所以要先安裝actiBPM插件,安裝方法如下:
    在IDEA中實用快捷鍵Shift+Ctrl+Alt+S打開環境中心

新建一個maven項目

1.點擊new Project創建一個新項目


2.選擇maven項目創建


3.填寫項目信息


4.設置maven信息


5.點擊flsh完成項目創建,創建結構如下

6.補全項目中main文件夾下的java,resource等源文件夾
在IDEA中實用快捷鍵Shift+Ctrl+Alt+S打開環境中心,選擇main包,右鍵new folder依次新建缺少的文件夾


7.創建完成後與對應文件源一一點擊對應


8.在resources包下創建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">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.0.114:3306/activiti"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"></property>
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
</beans>

9.在resources包下創建log4j.properties


代碼如下

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
​
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
​
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
​
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/Users/apple/學習/study/activity/activity_01/xis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m     

9.在test包下創建測試類,生成數據庫表文件

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

/**
 * @author : YXC
 * @version : 1.0
 * @description :
 * @date : 2020/11/17 16:50
 */
public class ActivitiTest {
    @Test
    public  void testGenTable(){
        //1.創建ProcessEngineConfiguration對象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //2.創建ProcessEngine對象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println(processEngine);

    }

    @Test
    public void testGenTable2(){
        //使用下面這種方式生成表的條件
        //1.activiti配置文件名稱必須爲activiti.cfg.xml
        //2.bean的id必須爲"processEngineConfiguration"
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(defaultProcessEngine);

    }
}

10.生成表結構如下

最後

歡迎關注公衆號:前程有光,領取一線大廠Java面試題總結+各知識點學習思維導+一份300頁pdf文檔的Java核心知識點總結!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章