1、项目结构
2、pom
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.18.0</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.188</version> </dependency> </dependencies>
3、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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 流程引擎配置 --> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration"> <property name="jdbcUrl" value="jdbc:h2:mem:Activiti" /> <property name="jdbcDriver" value="org.h2.Driver" /> <property name="jdbcUsername" value="sa"></property> <property name="jdbcPassword" value=""></property> <!-- databaseSchemaUpdate 配置流程引擎启动/关闭时对数据库Schema的策略.取值有: false:默认子,检查DBSchema版本与Activiti开发包是否一致 true:更新数据库中标信息,如果不存在则创建,推荐使用 create-drop 启动时创建表 关闭时删除表 --> <property name="databaseSchemaUpdate" value="true"></property> </bean> </beans>
4、Helloworld.bpmn20.xml
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myProcess" name="第一个流程图" isExecutable="true"> <startEvent id="startevent1" name="开始事件"></startEvent> <endEvent id="endevent1" name="结束事件"></endEvent> <userTask id="usertask1" name="HelloWorld"></userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess"> <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="90.0" y="180.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="320.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="360.0" y="180.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="125.0" y="197.0"></omgdi:waypoint> <omgdi:waypoint x="360.0" y="207.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="412.0" y="235.0"></omgdi:waypoint> <omgdi:waypoint x="367.0" y="320.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
5、HelloWorld.java
import org.activiti.engine.task.Task; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; public class HelloWorld { public static void main(String[] args) { //1 获取流程引擎配置 ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("/config/activiti.cfg.xml"); // 2 创建流程引擎 ProcessEngine engine = cfg.buildProcessEngine(); //3.获取 RepositoryService RepositoryService repositoryService = engine.getRepositoryService(); //4 部署流程图 repositoryService.createDeployment().addClasspathResource("process/Two.bpmn20.xml").deploy(); //5. 获取运行时候服务RuntimeService RuntimeService runtimeService = engine.getRuntimeService(); //6.获取流程实例 String processDefinitionKey = "mine"; ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey); System.out.println(instance.getActivityId()); //7.获取TaskService TaskService taskServer = engine.getTaskService(); long count = taskServer.createTaskQuery().count(); System.out.println("count:" + count); //8. 查询Task Task task = taskServer.createTaskQuery().singleResult(); //9 处理任务 System.out.println(task.getName()); } }
6、另H2 Test
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; public class H2DbTest { @Test public void testH2() { // 1 加载驱动 try { Class.forName("org.h2.Driver"); //2 获得数据库连接 try { Connection conn= DriverManager.getConnection("jdbc:h2:mem:Activiti","sa",""); // 3 创建Persion表 Statement stt = conn.createStatement(); String sql ="create table person(id int auto_increment primary key, name varchar(200));" + " insert into person(name) values('vic')"; stt.execute(sql); PreparedStatement ps = conn.prepareStatement("select * from person"); ResultSet rs = ps.executeQuery(); while(rs.next()) { System.out.println("id:" + rs.getInt(1)); System.out.println("name:" + rs.getString(2)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }