数据库设计与模型映射

913 阅读2分钟

数据库模型设计

image-20210217075204046.png

数据库建表语句

Mysql建表语句

  • 核心引擎 activiti.mysql.create.engine.sql (必选)
  • 历史数据 activiti.mysql.create.engine.sql (可选)
  • 身份信息 activit.mysql.create.identity.sql(可选)

Mysql删除语句

  • 核心引擎 activiti.mysql.drop.engine.sql
  • 历史数据 activiti.mysql.drop.history.sql
  • 身份信息 activiti.mysql.drop.identity.sql

通用数据库

image-20210217124028113.png

image-20210217124109699.png

image-20210217124123948.png

  • 查看数据库基本信息

    • java 代码

        private static final Logger LOGGER = LoggerFactory.getLogger(DbConfigTests.class);
      
      
          @Test
          public void testDbConfig() {
              ProcessEngineConfiguration resourceDefault = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
              ProcessEngine processEngine = resourceDefault.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("tableName = {}", tableName);
              }
              LOGGER.info("tableName.size() = {}", tableNames.size());
      
      
          }
      
    • 控制台输出结果为

image-20210217130748089.png

  • 删除表结构java代码

        @Test
        public void dropTable() {
            ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                    .createProcessEngineConfigurationFromResource("activiti-mysql.cfg.xml");
            ProcessEngine processEngine = configuration.buildProcessEngine();
            ManagementService managementService = processEngine.getManagementService();
            Object executeCommand = managementService.executeCommand((CommandContext commandContext) -> {
                commandContext.getDbSqlSession().dbSchemaDrop();
                LOGGER.info("删除表结构");
                return null;
            });
    
        }
    
  • 测试部署java代码

    public class DbGeTest {
    
    
        @Rule
        public ActivitiRule activitiRule = new ActivitiRule("activiti-mysql.cfg.xml");
    
        @Test
        public void testByteArray() {
            activitiRule.getRepositoryService().createDeployment()
                    .name("测试部署")
                    .addClasspathResource("my-process.bpmn20.xml")
                    .deploy();
        }
    
        @Test
        public void testByteArrayInsert() {
            ManagementService managementService = activitiRule.getManagementService();
            managementService.executeCommand((CommandContext commandContext) -> {
                ByteArrayEntityImpl byteArrayEntity = new ByteArrayEntityImpl();
                byteArrayEntity.setName("test");
                byteArrayEntity.setBytes("test message".getBytes());
                commandContext.getByteArrayEntityManager().insert(byteArrayEntity);
                return null;
            });
        }
    }
    

流程定义存储表

image-20210217150206602.png

ACT_RE_DEPLOYMENT【DeploymentEntityImpl】

image-20210217165422195.png image-20210217165309081.png

ACT_RE_PROCDEF【ProcessDefinitionEntityImpl】

  • 数据库表里面的信息如下

image-20210217165609661.png

  • 部署文件资源代码
    @Test
    public void testDepoly() {
        activitiRule.getRepositoryService()
                .createDeployment()
                .name("二次审批流程")
                .addClasspathResource("second_approve.bpmn20.xml")
                .deploy();
    }
  • 挂起代码如下
 @Test
    public void testSuspend() {
        RepositoryService repositoryService = activitiRule.getRepositoryService();
        repositoryService.suspendProcessDefinitionById("second_approve:2:7504");
        boolean suspended = repositoryService
                .isProcessDefinitionSuspended("second_approve:2:7504");
        LOGGER.info("suspended =  {}", suspended);
    }
  • 结果如下

image-20210217171314857.png

身份数据表设计

身份数据表(identity)

image-20210217172120371.png

ACT_ID_USER(UserEntityImpl)

image-20210217172302976.png

ACT_ID_INFO(IdentityInfoEntityImpl)

image-20210217172456044.png

ACT_ID_GROUP(GroupEntityImpl)

image-20210217172745516.png

ACT_ID_MEMBERSHIP(MemberShipEntityImpl)

image-20210217172858687.png

创建用户信息表
  • Java代码
public class DbIdentityTest {
    @Rule
    public ActivitiRule activitiRule = new ActivitiRule("activiti-mysql.cfg.xml");

    @Test
    public void testIdentity() {
        IdentityService identityService = activitiRule.getIdentityService();
        User user1 = identityService.newUser("user1");
        user1.setFirstName("firstName");
        user1.setLastName("lastName");
        user1.setEmail("1233@qq.com");
        user1.setPassword("pwd");
        identityService.saveUser(user1);
        User user2 = identityService.newUser("user2");
        identityService.saveUser(user2);

        Group group1 = identityService.newGroup("group1");
        group1.setName("for test");
        identityService.saveGroup(group1);
        identityService.createMembership(user1.getId(), group1.getId());
        identityService.createMembership(user2.getId(), group1.getId());

        identityService.setUserInfo(user1.getId(),"age","18");
        identityService.setUserInfo(user1.getId(),"address","Bejing");
    }

  • 执行结果为

image-20210217181842382.png

image-20210217181902924.png

image-20210217181815807.png

运行时流程数据表

数据表分类

image-20210217184718664.png

ACT_ID_EXECUTUION(ExecutionEntityImpl)

image-20210217185402344.png

image-20210217185141788.png

ACT_ID_TASK(TaskEntityImpl)

image-20210217185641825.png

image-20210217185742404.png

ACT_RU_VARIABLE(VariableInstanceEntityImpl)

image-20210217185911674.png

ACT_RU_IDENITYLINK(IdentityLinkedEntityImpl)

image-20210217190145210.png

ACT_RU_EVENT_SUBSCR(EventSubscriptionEntityImpl)

image-20210217190437505.png

ACT_RU_JOB(JobEntityImpl)

image-20210217202731658.png

image-20210217190539760.png

  • Java执行代码

    public class DbRepostitoryTests {
        private static final Logger LOGGER = LoggerFactory.getLogger(DbRepostitoryTests.class);
    
        @Rule
        public ActivitiRule activitiRule = new ActivitiRule("activiti-mysql.cfg.xml");
    
        @Test
        public void testDepoly() {
            activitiRule.getRepositoryService()
                    .createDeployment()
                    .name("二次审批流程")
                    .addClasspathResource("second_approve.bpmn20.xml")
                    .deploy();
        }
    
        @Test
        public void testSuspend() {
            RepositoryService repositoryService = activitiRule.getRepositoryService();
            repositoryService.suspendProcessDefinitionById("second_approve:2:7504");
            boolean suspended = repositoryService
                    .isProcessDefinitionSuspended("second_approve:2:7504");
            LOGGER.info("suspended =  {}", suspended);
        }
    }
    
private static final Logger LOGGER = LoggerFactory.getLogger(DbRunTimeTests.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule("activiti-mysql.cfg.xml");

    @Test
    public void testDepoly() {
        activitiRule.getRepositoryService()
                .createDeployment()
                .name("二次审批流程")
                .addClasspathResource("second_approve.bpmn20.xml")
                .deploy();
        Map<String, Object> variables = Maps.newHashMap();
        variables.put("key1", "values1");
        ProcessInstance processInstance = activitiRule.getRuntimeService()
                .startProcessInstanceByKey("second_approve", variables);
    }

    @Test
    public void testSetOwner() {
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey("second_approve")
                .singleResult();
        taskService.setOwner(task.getId(), "user1");
    }

    @Test
    public void testMessage() {
        Deployment deployment = activitiRule.getRepositoryService()
                .createDeployment().addClasspathResource("my-process-message.bpmn20.xml")
                .deploy();
    }

    @Test
    public void testMessageReceived() throws InterruptedException {
        Deployment deployment = activitiRule.getRepositoryService()
                .createDeployment().addClasspathResource("my-process-message-received.bpmn20.xml")
                .deploy();
        ProcessInstance processInstance = activitiRule.getRuntimeService()
                .startProcessInstanceByKey("my-process");
        Thread.sleep(1000 * 30L);
    }

历史流程数据表

image-20210217211722201.png

ACT_HI_PROCINST(HistoricProcessInstanceEntityImpl)

image-20210217212324856.png

image-20210217212431473.png

ACT_HI_LOG(EventLogEntryEntryImpl)

image-20210217212644757.png

image-20210217212615522.png