Article Directory
1. Setting up the environment
Note: This environment uses Activiti 6.0.0, no need to configure the config file, start the deployment process will automatically create a table
1.1 Introducing POM
<!--1 确定spring boot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<!--test测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<!--activiti6-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0</version>
</dependency>
</dependencies>
1.2 Add yml configuration file
#端口号
server:
port: 9090
#服务名和数据源(连接池)
spring:
application:
name: activitiservice
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/activiti6_jpa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&nullCatalogMeansCurrent=true
username: root
password: 1234
jpa:
show-sql: true
activiti:
check-process-definitions: false #是否自动检查、部署流程定义文件
database-schema-update: true #自动更新数据库结构
#流程定义文件存放目录
process-definition-location-prefix: classpath:/processes/
#process-definition-location-suffixes: #流程文件格式
history-level: full
1.3 Startup
Note: (exclude = SecurityAutoConfiguration.class) must be added to the startup class, otherwise an error will be reported
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class StudyActivitiJpaApplication {
public static void main(String[] args) {
SpringApplication.run(StudyActivitiJpaApplication.class,args);
}
}
1.4 Create process definition file directory
在resources下创建包processes,用来存放流程定义文件
2. Test Environment-Generate Data Sheet
2.1 Create a flowchart
这里暂且使用IDEA插件activBpm进行画图
You need to download the plug-in activBpm. After
downloading the plug-in, restart IDEA to use it.
Design flow chart: After creating the flow chart,
processes
need to manually copy the bpmn file, paste and modify it as an xml file, in order to generate a png image
2.2 Create a test class
在test测试区创建测试类ActivitiJpaTest01进行环境的初步测试
2.3 Test-Generate Data Sheet
ActivitiJpaTest01:
package com.yb;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @author [email protected]
* @version 1.0
* @date 2020/8/5
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiJpaTest01 {
@Resource
private RepositoryService repositoryService;
@Resource
private RuntimeService runtimeService;
@Resource
private TaskService taskService;
@Resource
private HistoryService historyService;
/**
* 部署流程
*/
@Test
public void demo01(){
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("processes/avtiviti_demo01.bpmn")
.addClasspathResource("processes/avtiviti_demo01.png")
.name("整合JPA测试01")
.deploy();
System.out.println("部署ID:"+deploy.getId());
System.out.println("部署名称"+deploy.getName());
}
}
Console:
Database: The
test is successful, the initial environment is correct, and the database generates 28 tables.
Want to know the meaning of each table, please refer to the article 5.1
3. Integrate JPA in Activiti
Solve the large amount of data generated by dynamic forms
3.1 Why integrate JPA
If you choose to use dynamic forms, you will face a more "serious" problem-Large amount of data, We know that the content of the dynamic form is stored in a table (ACT_HI_DETAIL), and we also know that every Field in the dynamic form will insert a record in the table. If there are 20 fields in a process, you can calculate this amount of data. How many process instances per day, how many per month, per year?
Accumulation of big data will affect the performance of the system, especially when it comes to related queries. In addition to performance, there is a drawback of dynamic forms that is that the data is stored in the form of rows. There is no data structure at all, and it is generated during process operation. Data is difficult to be used for analysis and query, how to crack it?
3.2 How to integrate JPA
In addition to the core Engine, Activiti supports the existing technology, platform, and architecture of the enterprise. Of course, it also supports the persistence of business entities, which is one of the EJB standards. ——JPA, The engine introduces the JPA API into the internal, when using the JPA function, only need to inherit JpaRepository in the interface.
Refer to
the basic use of JPA 1 Refer to the basic use of JPA 2 Refer to
the basic use of JPA 3
4. Case Analysis-Test
4.1 Generate entity classes, dao, service, Controller
4.2 Startup process
/**
* 发布流程
*/
@Test
public void runtimeRelease(){
ProcessInstance pi = runtimeService.startProcessInstanceByKey("demo01");
System.out.println("流程实例ID:"+pi.getId());
System.out.println("流程定义ID:"+pi.getProcessDefinitionId());
}
4.3 Query and complete tasks
/**
* 查询及完成任务
*/
@Test
public void taskQueryComplete(){
List<Task> list = taskService.createTaskQuery()
.taskAssignee("user2")
.list();
for (Task task : list) {
System.out.println("--------------------------------------------");
System.out.println("任务ID:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务创建时间:" + task.getCreateTime());
System.out.println("任务委派人:" + task.getAssignee());
System.out.println("流程实例ID:" + task.getProcessInstanceId());
//完成任务
taskService.complete(task.getId());
}
}
4.4 Create an xml file to store custom sql statements
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_0.xsd"
version="2.1">
<named-query name="ActHiTaskinst.findByIdAndName">
<query>
<![CDATA[
select u from ActHiTaskinst u
where u.ID_=?1 and u.NAME_=?2
]]>
</query>
</named-query>
</entity-mappings>
4.5 Test custom JPA query
dao
@Query(name = "ActHiTaskinst.findByIdAndName")
ActHiTaskinst findByIdAndName(String id,String name);
orm.xml
<named-query name="ActHiTaskinst.findByIdAndName">
<query>
<![CDATA[
select u from ActHiTaskinst u
where u.ID_=?1 and u.NAME_=?2
]]>
</query>
</named-query>
Test category:
/*注入Dao*/
@Resource
private ActHiTaskinstDao actHiTaskinstDao;
/**
* 测试自定义JPA查询
*/
@Test
public void testJpa01(){
ActHiTaskinst idAndName = actHiTaskinstDao.findByIdAndName("2505", "请假申请");
System.out.println(idAndName);
}