10分钟轻松搞定SpringBoot集成Activiti6教程

第一步在项目pom.xml文件中添加所需依赖



	4.0.0

	com.springboot.demo
	springboot-activiti-demo
	1.0.0
	war

	springboot-activiti-demo
	springboot-demo

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.9.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	  
          
            org.springframework.boot  
            spring-boot-starter-web  
          
          
            org.activiti  
            activiti-spring-boot-starter-basic  
            6.0.0  
          
          
            org.springframework.boot  
            spring-boot-starter-data-jpa  
          
          
            org.springframework.boot  
            spring-boot-starter-thymeleaf  
          
          
            org.springframework.boot  
            spring-boot-starter-web  
          
  
          
            mysql  
            mysql-connector-java  
            runtime  
          
          
            org.springframework.boot  
            spring-boot-starter-tomcat  
          
          
            org.springframework.boot  
            spring-boot-starter-test  
            test  
          
     

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
			
		        org.apache.tomcat.maven
		        tomcat7-maven-plugin
		        
		          8081
		          /activity
		          
		            4000
		          
		        
		     
		
	



第二步:编写示例接口和接口实现类

接口类(ActivityConsumerService.java)

package com.springboot.demo.service;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController  
@RequestMapping("/test")  
public interface ActivityConsumerService {  
    /** 
     * 流程demo 
     * @return 
     */  
    @RequestMapping(value="/activitiDemo",method=RequestMethod.GET)  
    public boolean startActivityDemo();  
      
}  


接口实现类(ActivityConsumerServiceImpl.java)

package com.springboot.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springboot.demo.service.ActivityConsumerService;

@Service("activityService")  
public class ActivityConsumerServiceImpl implements ActivityConsumerService {  
  
    @Autowired  
    private RuntimeService runtimeService;  
    @Autowired  
    private TaskService taskService;  
    
    @Autowired
    private RepositoryService repositoryService;
      
    @Override  
    public boolean startActivityDemo() {  
        System.out.println("method startActivityDemo begin....");  
        
        System.out.println( "调用流程存储服务,查询部署数量:"
                + repositoryService.createDeploymentQuery().count());
        

      
        Map map = new HashMap();  
        map.put("apply","zhangsan");  
        map.put("approve","lisi");  
        
        //流程启动  
        ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave",map);  
       
        List tq=taskService.createTaskQuery().taskAssignee("zhangsan").list();
        System.out.println(tq.size());
        String assignee = "zhangsan";//当前任务办理人  
        List tasks = taskService//与任务相关的Service  
                .createTaskQuery()//创建一个任务查询对象  
                .taskAssignee(assignee)  
                .list();  
        if(tasks !=null && tasks.size()>0){  
            for(Task task:tasks){  
                System.out.println("任务ID:"+task.getId());  
                System.out.println("任务的办理人:"+task.getAssignee());  
                System.out.println("任务名称:"+task.getName());  
                System.out.println("任务的创建时间:"+task.getCreateTime());  

                System.out.println("流程实例ID:"+task.getProcessInstanceId());
                System.out.println("#####################################");  
            }  
        }  
     
        System.out.println("method startActivityDemo end....");  
        return false;  
    }  
}  


第三步:在src/main/java/resources目录下新建processes目录,并在该目录下新增一个业务流程文件

MyProcess.bpmn:



  
    
    
    
    
    
    
      
    
    
    
    
    
    
      
    
    
  
  
    
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
        
      
      
        
        
      
    
  



项目目录结构如下图:

10分钟轻松搞定SpringBoot集成Activiti6教程_第1张图片




第四步:在mysql数据库服务器增加数据库:activiti-demo


第五步:设置application.properties配置自己应用的相关信息

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activiti-demo?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=ROOT
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
server.port=8081
server.context-path=/
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8

第六步:启动应用后,在浏览器地址栏输入访问地址:

http://localhost:8081/test/activitiDemo

第七步:看控制后台输出结果如下图,即代表成功。

10分钟轻松搞定SpringBoot集成Activiti6教程_第2张图片



你可能感兴趣的:(springboot,activiti)