【Activiti工作流】3. 搭建Activiti的开发环境

搭建Activiti的开发环境

1. 创建Activiti项目

打开Eclipse ,点击File->New->Other,选择如下:
这里写图片描述

项目创建成功后:
;

引入开发需要的JAR包:Activiti开发jar包

引入成功以后如图:
这里写图片描述

2. 创建Activiti表

新建CreateTableTest.java文件,如下图:
这里写图片描述

代码如下:

package cpm.activiti;

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

public class CreateTableTable {
    /**使用代码创建工作流需要的23张表*/  
    @Test  
    public void createTable(){  
        //流程引擎ProcessEngine对象,所有操作都离不开引擎对象  
        ProcessEngineConfiguration processEngineConfiguration =  
                ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();  
        //连接数据库的配置  
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");  
        processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti_learn?useUnicode=true&characterEncoding=utf8");  
        processEngineConfiguration.setJdbcUsername("root");  
        processEngineConfiguration.setJdbcPassword("123456");  
        //三个配置  
        //1.先删除表,再创建表:processEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP="create-drop"  
        //2.不能自动创建表,需要表存在:processEngineConfiguration.DB_SCHEMA_UPDATE_FALSE="false"  
        //3.如果表存在,就自动创建表:processEngineConfiguration.DB_SCHEMA_UPDATE_TRUE="true"  
        processEngineConfiguration.setDatabaseSchema(processEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);  
        //获取工作流的核心对象,ProcessEngine对象  
        ProcessEngine processEngine=processEngineConfiguration.buildProcessEngine();  
        System.out.println("processEngine:"+processEngine+"Create Success!!");  
    }  
}

使用配置文件创建23张表:

添加配置文件: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"
  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


    <!-- 流程引擎的配置对象 -->  
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">  
        <!-- 流程引擎配置对象的相关参数 -->  
        <!-- 1.连接数据库的配置 -->  
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>  
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti_learn?useUnicode=true&amp;characterEncoding=utf8"/>  
        <property name="jdbcUsername" value="root"/>  
        <property name="jdbcPassword" value="123456"/>  
        <!-- 没有表就创建表 -->  
        <property name="databaseSchemaUpdate" value="true"/>  
    </bean> 

</beans>

添加日志配置:log4j.properties
代码如下:

log4j.rootLogger=INFO, CA

# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n

代码结构如下:
这里写图片描述

添加刚刚的测试类(CreateTableTable.java)方法:

扫描二维码关注公众号,回复: 1668585 查看本文章
/**使用配置文件创建工作流需要的23张表*/  
public void createTable_2(){  
    //流程引擎ProcessEngine对象,所有操作都离不开引擎对象  
    ProcessEngineConfiguration processEngineConfiguration =   
            ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");  
    //获取工作流的核心对象,ProcessEngine对象  
    ProcessEngine processEngine=processEngineConfiguration.buildProcessEngine();  
    System.out.println("processEngine:"+processEngine+"Create Success!!");  
}

然后junit运行程序就能看到数据库Activiti的表被自动创建出来了。

猜你喜欢

转载自blog.csdn.net/yk10010/article/details/80598837
今日推荐