共计 6772 个字符,预计需要花费 17 分钟才能阅读完成。
这篇文章主要介绍“activiti 原表怎么增加新字段”,在日常操作中,相信很多人在 activiti 原表怎么增加新字段问题上存在疑惑,丸趣 TV 小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”activiti 原表怎么增加新字段”的疑惑有所帮助!接下来,请跟着丸趣 TV 小编一起来学习吧!
activiti 自带了很多表,如图:
Activiti 工作流引擎的数据库表中的表名称都是以 ACT_. 第二部分两个字母表示表的类型。使用模糊匹配的方式说明表的类型匹配 activiti 的服务 API.
middot; ACT_RE_*: RE 代表仓储(Repository). 这种表前缀以“static”表示流程定义信息或者流程资源信息(如流程的图表和规则等).
middot; ACT_RU_*: RU 标识为运行 (Runtime) 时表。包含流程实例,用户任务和变量任务等在运行时的数据信息。这些表只存储 Activiti 在流程实例运行执行的数据,在流程结束的时候从表中去除数据。从而保持运行时候数据的表的快速和小数据量.
middot; ACT_ID_*:ID 标识为唯一 (Identity) 的。包含一些唯一的信息如用户,用户做等信息。
middot; ACT_HI_*:HI 表示历史数据 (History) 表,包括过期的流程实例,过期的变量和过期的任务等。
middot; ACT_GE_*:GE 表示公用 (General data) 的数据库表类型。
ACT_GE_BYTEARRAY 表保存了开发时候的文件,在工作流部署的时候需要上传相关的工作流文件到相关的项目中。其中如果是文件采用方式如下,将图片和或者文件转换为二进制字节流存储。
bytes_字段保存了文件内容,如果是图片,则是保存了二进制。
由于各个项目的业务特殊性,想扩展 ACT_GE_BYTEARRAY 的字段,增加 2 个新字段 SYS_,SWITCHBOARD_字段。
怎么把数据保存到表中,这里采用的是修改源码的办法:
步骤 1:修改 ACT_GE_BYTEARRAY 表对应实体 org.activiti.engine.impl.persistence.entity.ResourceEntity,添加 SYS_,SWITCHBOARD_字段:
public class ResourceEntity implements Serializable, PersistentObject {
private static final long serialVersionUID = 1L;
protected String id;
protected String name;
protected byte[] bytes;
protected String deploymentId;
protected boolean generated = false;
//-------------------------------
private String switchboard;
private boolean sys;
...
}
步骤 2:修改相应的 sql 的配置,添加 SYS_,SWITCHBOARD_字段。
文件 org.activiti.db.mapping.entity.Resource.xml:
?xml version= 1.0 encoding= UTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespace= org.activiti.engine.impl.persistence.entity.ResourceEntity
!-- RESOURCE INSERT --
insert id= insertResource parameterType= org.activiti.engine.impl.persistence.entity.ResourceEntity
insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_, GENERATED_,SWITCHBOARD_,SYS_) values (#{id, jdbcType=VARCHAR}, 1, #{name, jdbcType=VARCHAR}, #{bytes, jdbcType=BLOB}, #{deploymentId, jdbcType=VARCHAR}, #{generated, jdbcType=BOOLEAN}, #{switchboard, jdbcType=VARCHAR},#{sys, jdbcType=BOOLEAN}) /insert
!-- RESOURCE UPDATE --
!-- RESOURCE DELETE --
delete id= deleteResourcesByDeploymentId parameterType= string
delete from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{id}
/delete
!-- RESOURCE RESULTMAP --
resultMap id= resourceResultMap type= org.activiti.engine.impl.persistence.entity.ResourceEntity
id property= id column= ID_ jdbcType= VARCHAR /
result property= name column= NAME_ jdbcType= VARCHAR /
result property= bytes column= BYTES_ jdbcType= BLOB /
result property= generated column= GENERATED_ jdbcType= BOOLEAN /
result property= switchboard column= SWITCHBOARD_ jdbcType= VARCHAR /
result property= sys column= SYS_ jdbcType= BOOLEAN /
/resultMap
!-- RESOURCE SELECT --
select id= selectResourceNamesByDeploymentId parameterType= org.activiti.engine.impl.db.ListQueryParameterObject resultType= string
select NAME_ from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{parameter} order by NAME_ asc
/select
select id= selectResourceByDeploymentIdAndResourceName parameterType= map resultMap= resourceResultMap
select * from ${prefix}ACT_GE_BYTEARRAY
where DEPLOYMENT_ID_ = #{deploymentId}
AND NAME_ = #{resourceName}
/select
select id= selectResourcesByDeploymentId parameterType= org.activiti.engine.impl.db.ListQueryParameterObject resultMap= resourceResultMap
select * from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{parameter} order by NAME_ asc
/select
!-- postgresql specific --
resultMap id= resourceResultMap_postgres type= org.activiti.engine.impl.persistence.entity.ResourceEntity
id property= id column= ID_ jdbcType= VARCHAR /
result property= name column= NAME_ jdbcType= VARCHAR /
result property= bytes column= BYTES_ jdbcType= BINARY /
result property= generated column= GENERATED_ jdbcType= BOOLEAN /
result property= switchboard column= SWITCHBOARD_ jdbcType= VARCHAR /
result property= sys column= SYS_ jdbcType= BOOLEAN /
/resultMap
!-- postgresql specific --
select id= selectResourceByDeploymentIdAndResourceName_postgres parameterType= map resultMap= resourceResultMap_postgres
select * from ${prefix}ACT_GE_BYTEARRAY
where DEPLOYMENT_ID_ = #{deploymentId}
AND NAME_ = #{resourceName}
/select
!-- postgresql specific --
select id= selectResourcesByDeploymentId_postgres parameterType= org.activiti.engine.impl.db.ListQueryParameterObject resultMap= resourceResultMap_postgres
select * from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{parameter} order by NAME_ asc
/select
/mapper
主要就是修改 insert resultMap 的内容
步骤 3:加载数据文件时候,设置 SYS_,SWITCHBOARD_的值
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
DeploymentBuilderImpl deploymentBuilder =(DeploymentBuilderImpl) repositoryService.createDeployment()
.addClasspathResource( activiti/leave.bpmn
DeploymentEntity deploymentEntity = deploymentBuilder.getDeployment();
ResourceEntity resourceEntity = deploymentEntity.getResource( activiti/leave.bpmn
resourceEntity.setSwitchboard(getSwitchboard());
resourceEntity.setSys(true);
deploymentEntity.addResource(resourceEntity);
deploymentBuilder.deploy();
注:这里主要是通过 ResourceEntity resourceEntity = deploymentEntity.getResource(activiti/leave.bpmn 获得数据库获得对应的实体,然后设置我们新添加的字段的值。
测试结果:
看到上面的数据,SYS_,SWITCHBOARD_字段都有值了,activiti/leave.bpmn 是正确的,但是 activiti/leave.leave.png 对应的值是不正确的。因为这 2 个添加的值因该是一样的。
下面继续修改:
步骤 4:org.activiti.engine.impl.bpmn.deployer.BpmnDeployer, 加载 activiti/leave.bpmn 中的图片
public void deploy(DeploymentEntity deployment) { createResource(resourceName,diagramResourceName, diagramBytes, deployment);
protected void createResource(String resourceName ,String name, byte[] bytes, DeploymentEntity deploymentEntity) { ResourceEntity resource = new ResourceEntity();
resource.setName(name);
resource.setBytes(bytes);
resource.setDeploymentId(deploymentEntity.getId());
ResourceEntity resourceEntity = deploymentEntity.getResource(resourceName);
if(resourceEntity!=null){ resource.setSwitchboard(resourceEntity.getSwitchboard());
resource.setSys(resourceEntity.isSys());
}
// Mark the resource as generated
resource.setGenerated(true);
Context
.getCommandContext()
.getDbSqlSession()
.insert(resource);
}
有人要问,问什么要这么修改,没办法,我是一步一步 debug,一步一步看源码。
步骤 5:文件 org.activiti.db.mapping.entity.VariableInstance.xml,添加 SYS_,SWITCHBOARD_字段:
!-- BYTE ARRAY INSERT --
insert id= insertByteArray parameterType= org.activiti.engine.impl.persistence.entity.ByteArrayEntity
insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_,SWITCHBOARD_,SYS_)
values ( #{id, jdbcType=VARCHAR},
1,
#{name, jdbcType=VARCHAR},
#{bytes, jdbcType=BLOB},
#{deploymentId, jdbcType=VARCHAR},
#{switchboard, jdbcType=VARCHAR},
#{sys, jdbcType=BOOLEAN}
)
/insert
然后测试结果:
到此,关于“activiti 原表怎么增加新字段”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!