Activiti学习之二Activiti API
Posted cac2020
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti学习之二Activiti API相关的知识,希望对你有一定的参考价值。
环境
JDK 1.8
mysql 5.6
Tomcat 7
Eclipse-Luna
activiti 6.0
一、Activiti数据查询
准备数据:
package com.wjy.act; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class SaveGroup public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); for(int i = 0; i < 10; i++) Group group = is.newGroup(String.valueOf(i)); group.setName("Group_" + i); group.setType("TYPE_" + i); is.saveGroup(group);//保存到表act_id_group engine.close(); System.exit(0);
list 查询多条和count 数量统计:
package com.wjy.act; import java.util.List; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class TestList public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); //测试list查询 List<Group> groups = is.createGroupQuery().list(); for(Group g : groups) System.out.println(g.getId() + "---" + g.getName() + "---" + g.getType()); //测试 count long size = is.createGroupQuery().count(); System.out.println(size);
listPage 分页查询:
package com.wjy.act; import java.util.List; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class TestListPage public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); //分页查询 listPage List<Group> groups = is.createGroupQuery().listPage(1, 5); for(Group g : groups) System.out.println(g.getId() + "---" + g.getName() + "---" + g.getType());
singleResult 查询单条
package com.wjy.act; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class TestSingle public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); //测试singleResult Group g = is.createGroupQuery().groupName("Group_0").singleResult(); System.out.println(g.getId());
asc 升序和desc 降序:
package com.wjy.act; import java.util.List; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class TestSort public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); //测试降序 升序 需要根据字段orderby //多字段排序 orderby之后都要跟上asc() 或desc() //is.createGroupQuery().orderByGroupId().asc().orderByGroupName().asc().list(); List<Group> groups = is.createGroupQuery().orderByGroupId().desc().orderByGroupName().asc().list(); for(Group g : groups) System.out.println(g.getId());
按照字段条件查询:
package com.wjy.act; import java.util.List; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class FieldQuery public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); //按照字段条件查询 List<Group> groups = is.createGroupQuery().groupName("Group_1").groupType("TYPE_1").list(); for(Group g : groups) System.out.println(g.getId());
支持原生sql查询:
package com.wjy.act; import java.util.List; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.identity.Group; public class NativeQuery public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); IdentityService is = engine.getIdentityService(); //支持原生sql语句查询 List<Group> groups = is.createNativeGroupQuery() .sql("SELECT * FROM ACT_ID_GROUP where NAME_ = #name") .parameter("name", "Group_2").list(); for (Group g : groups) System.out.println(g.getId());
二、流程文件部署
DeploymentBuilder对象
方法addClasspathResource:
package com.wjy.deploy; import java.io.InputStream; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.DeploymentBuilder; public class TextQuery public static void main(String[] args) throws Exception ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); DeploymentBuilder builder = rs.createDeployment(); builder.addClasspathResource("my_text.txt"); Deployment dep = builder.deploy(); // 数据查询 InputStream is = rs.getResourceAsStream(dep.getId(), "my_text.txt"); int count = is.available(); byte[] contents = new byte[count]; is.read(contents); String result = new String(contents); //输入结果 System.out.println(result);
方法addInputStream:
package com.wjy.deploy; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import javax.imageio.ImageIO; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; public class ImageQuery public static void main(String[] args) throws Exception // 创建流程引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 得到流程存储服务对象 RepositoryService repositoryService = engine.getRepositoryService(); // 部署一份流程文件与相应的流程图文件 Deployment dep = repositoryService.createDeployment().addClasspathResource("gen.bpmn").deploy(); // 查询流程定义 ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult(); // 查询资源文件 InputStream is = repositoryService.getProcessDiagram(def.getId()); // 将输入流转换为图片对象 BufferedImage image = ImageIO.read(is); // 保存为图片文件 File file = new File("resource/result.png"); if (!file.exists()) file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); ImageIO.write(image, "png", fos); fos.close(); is.close();
方法addZipInputStream:
package com.wjy.deploy; import java.io.File; import java.io.FileInputStream; import java.util.zip.ZipInputStream; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.DeploymentBuilder; public class ZipTest public static void main(String[] args) throws Exception ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); DeploymentBuilder builder = rs.createDeployment(); FileInputStream fis = new FileInputStream(new File("resource/datas.zip")); ZipInputStream zis = new ZipInputStream(fis); builder.addZipInputStream(zis); builder.deploy();
方法addBpmnModel:
package com.wjy.deploy; import org.activiti.bpmn.model.BpmnModel; import org.activiti.bpmn.model.EndEvent; import org.activiti.bpmn.model.SequenceFlow; import org.activiti.bpmn.model.StartEvent; import org.activiti.bpmn.model.UserTask; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.DeploymentBuilder; public class BpmnTest public static void main(String[] args) ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); DeploymentBuilder builder = rs.createDeployment(); builder.addBpmnModel("My Process", createProcessModel()); builder.deploy(); private static BpmnModel createProcessModel() // 创建BPMN模型对象 BpmnModel model = new BpmnModel(); // 创建一个流程定义 org.activiti.bpmn.model.Process process = new org.activiti.bpmn.model.Process(); model.addProcess(process); process.setId("myProcess"); process.setName("My Process"); // 开始事件 StartEvent startEvent = new StartEvent(); startEvent.setId("startEvent"); process.addFlowElement(startEvent); // 用户任务 UserTask userTask = new UserTask(); userTask.setName("User Task"); userTask.setId("userTask"); process.addFlowElement(userTask); // 结束事件 EndEvent endEvent = new EndEvent(); endEvent.setId("endEvent"); process.addFlowElement(endEvent); // 添加流程顺序 process.addFlowElement(new SequenceFlow("startEvent", "userTask")); process.addFlowElement(new SequenceFlow("userTask", "endEvent")); return model;
方法addString和addBytes:略
上面的方法是加载流程方法,另外方法deploy是将加载的流程实现部署,返回对象Deployment,部署记录记录到表表:act_re_deployment,流程则记录到表:act_ge_bytearray。
以上是关于Activiti学习之二Activiti API的主要内容,如果未能解决你的问题,请参考以下文章