localVariable 与 Variable 区别:
Variable:
1、普通变量(或叫global变量),关联流程实例,流程未完结前都可查询得到
2、从数据库看,表act_ru_variable或表act_hi_varinst中的TASK_ID_为空
3、同一流程实例,同名Variable只存在一个,再次赋值时覆盖前一个值,版本号加一
localVariable:1、任务变量,关联流程任务,任务完成前可查看,任务完结后即使流程未完结也无法查到
2、从数据库看,表act_ru_variable或表act_hi_varinst中的TASK_ID_关联任务ID
3、localVariable在同一流程实例内可重名,但需关联不同task,否则类似再次赋值。
2、绑定了变量的任务结束后,要再次访问localVariable需要查询历史变量记录。
相同点:任何时候都可在act_hi_varinst表中查看
实现Serializable接口的类可作为变量值存储,因为实现序列化,所以需要填写序列化版本号保证存储前后类定义不变。
public class Loan implements Serializable { private static final long serialVersionUID = -8809463095791864892L; private int id; private double money; private Date time; private String reason; public Loan() { super(); } public Loan(int id, double money, Date time, String reason) { super(); this.id = id; this.money = money; this.time = time; this.reason = reason; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } }
一、启动流程时设置变量
Map<String,Object> map = new LinkedHashMap<String,Object>(); map.put("变量名", "变量值"); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ProcessKey,map);
二、完成任务时设置变量
Map<String,Object> map = new LinkedHashMap<String,Object>(); map.put("变量名", "变量值"); taskService.complete(taskId, map);
三、设置任务变量
PS、也可调用taskService.setVariablesLocal(taskId,map);将变量与任务绑定,
@Test public void setTaskVarible(){ TaskService taskService = processEngine.getTaskService(); String taskId = "2504";//任务ID taskService.setVariable(taskId, "债权人", "A");//1、通过任务ID,指定添加变量名,变量值 Map<String,Object> map = new LinkedHashMap<String,Object>(); map.put("债务人", "B");//集合中key为变量名,value为变量值 taskService.setVariables(taskId, map);//2、通过任务ID,通过map集合添加 Loan loan = new Loan(1,1000,new Date(),"流动资金不足");//添加实现Serializable接口的对象作为变量值 taskService.setVariable(taskId, "借款详细", loan);//3、添加对象 }
四、设置流程变量
PS、也可调用runtimeService.setVariableLocal();,默认关联流程当前任务
@Test public void setInstanceVarible(){ String executionId = "2501"; RuntimeService runtimeService = processEngine.getRuntimeService(); runtimeService.setVariable(executionId, "债权人", "A");//1、通过流程ID,指定添加变量名,变量值
Map<String,Object> map = new LinkedHashMap<String,Object>(); map.put("债务人", "B");//集合中key为变量名,value为变量值 runtimeService.setVariables(executionId, map);//2、通过流程ID,通过map集合添加 Loan loan = new Loan(1,1000,new Date(),"流动资金不足");//添加实现Serializable接口的对象作为变量值 runtimeService.setVariable(executionId, "借款详细", loan);//3、添加对象 }
五、获取流程变量
@Test public void getRuntimeVarible(){ RuntimeService runtimeService = processEngine.getRuntimeService(); String executionId = "2501"; String varValue = (String)runtimeService.getVariable(executionId, "债权人");//1、通过变量名取值 Map<String,Object> map = runtimeService.getVariables(executionId);//2、通过执行对象ID获取所有 变量值 List<String> list = new ArrayList<String>(); list.add("债务人");//集合保存变量名 runtimeService.getVariables(executionId, list);//3、通过执行对象ID获取特定名字的变量值 }
六、查询历史变量
@Test public void getHistoryProcessVariable(){ HistoryService historyService = processEngine.getHistoryService(); List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().variableName("债权人").list(); }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/7782.html