Activiti数据库设计和模型映射1-GE通用表
2019-08-23 本文已影响0人
安安汐而
Demo项目代码:
https://gitee.com/AnAnXiEr/activiti6-demo
数据模型设置
ACT_GE_* :通用数据表(GE表示General)
ACT_RE_* :流程定义存储表(RE表示Repository)
ACT_ID_* :身份信息表(ID表示Identity)
ACT_RU_* :运行是数据库表(RU表示Runtime)
ACT_HI_* :历史数据库表(HI表示History)
Mysql建表语句
在库org.activiti:activiti-engin:6.0.0下db目录
- 核心引擎 activiti.mysql.create.engin.sql(必须新建的)
- 历史数据 activiti.mysql.create.history.sql (可配置新增,也可不要历史数据)
- 身份信息 activiti.mysql.create.identity.sql (可配置新增,也可不要自带的用户信息)
通过配置文件配置
<!--是否使用historyUsed数据表、默认是true-->
<property name="dbHistoryUsed" value="false"></property>
<!--是否使用Identity数据表、默认是true-->
<property name="dbIdentityUsed" value="false"></property>
image.png
通用数据库
ACT_GE_PROPERTY :属性表(保存流程引擎的kv键值属性)
ACT_GE_BYTEARRAY :资源表(存储流程定义相关的资源)
act_ge_property表:
act_ge_property表
act_ge_byteArray表:
act_ge_byteArray表
代码
DbConfigTest
package com.imooc.activiti.activitidemo.dbentity;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.activiti.engine.FormService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @Description 数据库配置测试
* @Author 胡浩
* @Date 2019/8/23
**/
public class DbConfigTest {
private static final Logger LOGGER = LoggerFactory.getLogger(DbConfigTest.class);
//测试、改为自己的mysql数据库
// @Rule
// public ActivitiRule activitiRule = new ActivitiRule("dbentity/activiti-mysql.cfg.xml");
/**
* 历史数据和身份数据都是可配置的
*/
@Test
public void testDbConfig() {
//测试、改为自己的mysql数据库
ProcessEngine processEngine = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("dbentity/activiti-mysql.cfg.xml")
.buildProcessEngine();
ManagementService managementService = processEngine.getManagementService();
//获取所有表以及表对应的数据量
Map<String, Long> tableCount = managementService.getTableCount();
//获取所有表名称
ArrayList<String> tableNames = Lists.newArrayList(tableCount.keySet());
//排个序
Collections.sort(tableNames);
for (String tableName : tableNames) {
LOGGER.info("table name = {}", tableName);
}
LOGGER.info("tableNames.size = {}", tableNames.size());//应该是28个,去除history应该有16个
// LOGGER.info("task1 = {}", task1);//task1 = null 执行完了
}
/**
* 删除表
*/
@Test
public void tesDropTable() {
ProcessEngine processEngine = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("dbentity/activiti-mysql.cfg.xml")
.buildProcessEngine();
ManagementService managementService = processEngine.getManagementService();
Object o = managementService.executeCommand(new Command<Object>() {
@Override
public Object execute(CommandContext commandContext) {
commandContext.getDbSqlSession().dbSchemaDrop();
LOGGER.info(" 删除表结构");
return null;
}
});
}
}
DbGeTest
package com.imooc.activiti.activitidemo.dbentity;
import org.activiti.engine.ManagementService;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ByteArrayEntity;
import org.activiti.engine.impl.persistence.entity.ByteArrayEntityImpl;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Description GE通用数据库配置测试
* @Author 胡浩
* @Date 2019/8/23
**/
public class DbGeTest {
private static final Logger LOGGER = LoggerFactory.getLogger(DbGeTest.class);
//测试、改为自己的mysql数据库
@Rule
public ActivitiRule activitiRule = new ActivitiRule("dbentity/activiti-mysql.cfg.xml");
/**
* 部署记录
*/
@Test
public void testByeArray() {
//会往act_ge_byteArray表插入一条部署记录
activitiRule.getRepositoryService().createDeployment().name("测试部署")
.addClasspathResource("dbentity/my-process.bpmn20.xml")
.deploy();
}
/**
* 手工添加部署记录
*/
@Test
public void Insert() {
//会往act_ge_byteArray表插入一条部署记录
ManagementService managementService = activitiRule.getManagementService();
managementService.executeCommand(new Command<Object>() {
@Override
public Object execute(CommandContext commandContext) {
ByteArrayEntity entity = new ByteArrayEntityImpl();
entity.setName("test");
entity.setBytes("test message".getBytes());
commandContext.getByteArrayEntityManager().insert(entity);
return null;
}
});
}
}
activiti-mysql.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">
<!--以druid数据源连接-->
<property name="dataSource" ref="dataSource"/>
<!--启动时自动检查并更新数据库表,不存在会创建 (一般 dev开发项目 使用)-->
<property name="databaseSchemaUpdate" value="true"/>
<!--是否使用historyUsed数据表、默认是true-->
<property name="dbHistoryUsed" value="false"></property>
<!--是否使用Identity数据表、默认是true-->
<property name="dbIdentityUsed" value="false"></property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url"
value="jdbc:mysql://10.10.1.17:3306/activiti6?useUnicode=true&characterEncoding=utf8&nullCatalogMeansCurrent=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root001"/>
<property name="password" value="Mysql_2019"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="20"/>
<property name="filters" value="stat,slf4j"/>
</bean>
</beans>
my-process.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/processdef">
<process id="my-process" name="二级审批流程" isExecutable="true">
<documentation>MDC test process</documentation>
<startEvent id="startEvent" name="开始"/>
<sequenceFlow id="sid-905AF026-9221-4C80-AF9A-29C581E59503" sourceRef="startEvent"
targetRef="tlApprove"/>
<userTask id="tlApprove" name="主管审批"/>
<sequenceFlow id="sid-905AF026-9221-4C80-AF9A-29C581E59505" sourceRef="tlApprove"
targetRef="endEventCancel"/>
<endEvent id="endEventCancel" name="取消"/>
</process>
</definitions>