activiti使用詳解(一)

Better_Mee發表於2015-11-10
簡單的api程式示例(maven工程):






pom.xml
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.activiti.examples</groupId>
  <artifactId>activiti-examples</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>BPMN 2.0 with Activiti - Examples</name>
  <properties>
    <activiti-version>5.18.0</activiti-version>
  </properties>
  <dependencies>
    <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>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.3</version>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.3.168</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.6</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>1.7.6</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
    <!-- MySQL驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
  </dependencies>
 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.2</version>
        <configuration>
         <source>1.6</source>
         <target>1.6</target>
       </configuration>
     </plugin>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <inherited>true</inherited>
        <configuration>
         <classpathContainers>
           <classpathContainer>org.eclipse.jdt.USER_LIBRARY/Activiti Designer Extensions</classpathContainer>
         </classpathContainers>
       </configuration>
     </plugin>
    </plugins>
 </build>
</project>

第一種方式使用程式碼形式建立資料庫(不推薦,建議使用方法二的:配置檔案):

package junit;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;
public class TestActivit{
 
 @Test
 public void creatTable(){
  ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
  processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
  processEngineConfiguration.setJdbcUrl("jdbc:mysql://172.31.2.14:3306/activiti?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true");
  processEngineConfiguration.setDatabaseSchemaUpdate(processEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP);
  processEngineConfiguration.setJdbcUsername("root");
  processEngineConfiguration.setJdbcPassword("yunjisuan");
 
  ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
 }
}





方法二(使用spring 配置檔案依賴注入-建議使用此方法):
package junit;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;
public class TestActivit{
 
 @Test
 public void creatTable(){
  ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
  processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
  processEngineConfiguration.setJdbcUrl("jdbc:mysql://172.31.2.14:3306/activiti?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true");
  processEngineConfiguration.setDatabaseSchemaUpdate(processEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP);
  processEngineConfiguration.setJdbcUsername("root");
  processEngineConfiguration.setJdbcPassword("yunjisuan");
 
  processEngineConfiguration.buildProcessEngine();
 }
 
 @Test
 public void creatTable2(){
  ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
 }
}

需要增加一個配置檔案:

<?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.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://172.31.2.14:3306/activiti?createDatabaseIfNotExist=true" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="yunjisuan" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
  
</beans>







相關文章