1.声明
当前内容主要为本人学习和测试在SpringBoot中使用jpa以及mysql方式操作当前的activiti6这个工作流
2.基本的pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version><!-- 父类中的版本控制所有的依赖包的版本信息 -->
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter-basic -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0</version>
</dependency>
<!-- 使用jpa功能 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-jpa</artifactId>
<version>6.0.0</version>
</dependency>
<!-- 添加web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <version>1.2.6.RELEASE</version> -->
</dependency>
<!-- 添加h2数据库支持 -->
<!-- <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.183</version>
</dependency> -->
<!-- 添加mysql数据库支持 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
3.基本订单的流程配置
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="Examples" >
<process id="orderTaskProcess" name="订单流程">
<startEvent id="orderStart" />
<sequenceFlow id="order-flow1" sourceRef="orderStart" targetRef="selectOrder" />
<userTask id="selectOrder" name="选择订单" activiti:assignee="${username}" />
<sequenceFlow id="order-flow2" sourceRef="selectOrder" targetRef="conformOrder" />
<userTask id="conformOrder" name="确认订单" activiti:assignee="${username}" />
<sequenceFlow id="order-flow3" sourceRef="conformOrder" targetRef="submitOrder" />
<userTask id="submitOrder" name="提交订单" activiti:assignee="${username}" />
<sequenceFlow id="order-flow4" sourceRef="submitOrder" targetRef="orderEnd" />
<endEvent id="orderEnd" />
</process>
</definitions>
注意这里的activiti:assignee="${username}"
对应传递给activiti的变量中的username属性
4.配置文件
db.properties
mysql.url=jdbc:mysql://localhost:3306/hy_activiti6
mysql.username=root
mysql.password=root
mysql.driverClassName=com.mysql.jdbc.Driver
application.properties
spring.jpa.hibernate.ddl-auto=update
5.demo
1.基本的配置类:AppConfig
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.hy.springboot.activiti6.pojo.MySqlDBProperties;
@Configuration
@PropertySource(value = {
"classpath:db.properties" })
@EnableConfigurationProperties(value = MySqlDBProperties.class)
@EnableTransactionManagement
public class AppConfig {
@Bean(name = "dataSource")
public DataSource setDataSource(MySqlDBProperties properties) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setDriverClassName(properties.getDriverClassName());
return dataSource;
}
}
2.基本实体类
MySqlDBProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "mysql")
public class MySqlDBProperties {
private String url;
private String driverClassName;
private String username;
private String password;
// 省略getter和setter方法
}
Person实体类
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*
* @author hy
* @createTime 2021-03-20 14:18:57
* @description 对应数据库的实体类,主要用于处理哪个人执行的工作流
*
*/
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String username;
private Date birthDate;
public Person() {
}
public Person(String username, Date birthDate) {
this.username = username;
this.birthDate = birthDate;
}
// 省略getter和setter方法
}
3.dao和service层
PersonRepository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.hy.springboot.activiti6.pojo.Person;
/**
*
* @author hy
* @createTime 2021-03-20 14:19:20
* @description 默认采用jpa方式实现数据操作方式
*
*/
@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
Person findByUsername(String username);
}
OrderService
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hy.springboot.activiti6.dao.PersonRepository;
import com.hy.springboot.activiti6.pojo.Person;
@Service
public class OrderService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Transactional //默认开启
public void startProcess() {
// 开启订单任务
//指定xml配置文件为:admin
runtimeService.startProcessInstanceByKey("orderTaskProcess");
}
@Transactional
// 为某个人开始activiti工作流程(指定用户名的方式实现)
public void startProcess(String username) {
// 开启订单任务
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("username", username);
//指定xml配置文件为:
runtimeService.startProcessInstanceByKey("orderTaskProcess",variables);
}
// 获取需要指定的任务列表
@Transactional
public List<Task> getTasks(String username) {
return taskService.createTaskQuery().taskAssignee(username).list();
}
// 完成某个任务
@Transactional
public void completeTask(String taskId) {
taskService.complete(taskId);
}
@Autowired
private PersonRepository personRepository;
public void createDemoUsers() {
if (personRepository.findAll().size() == 0) {
personRepository.save(new Person("zhangsan", new Date()));
personRepository.save(new Person("admin", new Date()));
}
}
}
4.controller层
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hy.springboot.activiti6.dao.PersonRepository;
import com.hy.springboot.activiti6.pojo.Person;
import com.hy.springboot.activiti6.service.OrderService;
/**
*
* @author hy
* @createTime 2021-03-20 14:19:41
* @description 一个标准的订单控制器,向当前的activiti中执行流程控制操作
*
*/
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
// 开启工作流任务
/*
* @RequestMapping(value = "/process", method = RequestMethod.GET) public void
* startProcessInstance() { orderService.startProcess(); }
*/
// 开启工作流任务
@RequestMapping(value = "/process", method = RequestMethod.GET)
public void startProcessInstance(@RequestParam String username) {
orderService.startProcess(username);
}
// 完成工作流中的某项任务
@RequestMapping(value = "/complete", method = RequestMethod.GET)
public void completeTask(@RequestParam String taskId) {
orderService.completeTask(taskId);
}
// 获取当前的执行工作流的列表(即具有哪些未完成的工作流)
@RequestMapping(value = "/tasks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<TaskRepresentation> getTasks(@RequestParam String username) {
List<Task> tasks = orderService.getTasks(username); // 获取某个人的执行任务流程的状态
List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
for (Task task : tasks) {
dtos.add(new TaskRepresentation(task.getId(), task.getName()));
}
return dtos;
}
static class TaskRepresentation {
private String id;
private String name;
public TaskRepresentation(String id, String name) {
this.id = id;
this.name = name;
}
// 省略getter和setter方法
}
}
5.入口类
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.hy.springboot.activiti6.service.OrderService;
@EntityScan(basePackages = {
"com.hy.springboot.activiti6.pojo" })
@EnableJpaRepositories(basePackages = {
"com.hy.springboot.activiti6.dao" })
@SpringBootApplication
public class SpringBootActiviti6App {
public static void main(String[] args) {
SpringApplication.run(SpringBootActiviti6App.class, args);
}
@Bean
public CommandLineRunner init(final OrderService orderService, final RepositoryService repositoryService,
final RuntimeService runtimeService, final TaskService taskService) {
return new CommandLineRunner() {
@Override
public void run(String... strings) throws Exception {
System.out.println(
"Number of process definitions : " + repositoryService.createProcessDefinitionQuery().count());
System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
// 开始创建基本的用户,如果数据库中存在则不需要
orderService.createDemoUsers();
}
};
}
}
6.测试和结果
能够正常启动并且在mysql中创建许多的表
完成一次流程后就会产生特定的表数据信息
测试结果完成