用activiti 工作流 实现简单的请假 附带源码.docx
《用activiti 工作流 实现简单的请假 附带源码.docx》由会员分享,可在线阅读,更多相关《用activiti 工作流 实现简单的请假 附带源码.docx(18页珍藏版)》请在冰豆网上搜索。
用activiti工作流实现简单的请假附带源码
用activiti工作流实现简单的请假附带源码
用activiti工作流实现简单的请假附带源码
新建一个Maven项目
项目结构
pom.xml
[html]viewplaincopy
1.projectxmlns="http:
//maven.apache.org/POM/4.0.0"xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
2.xsi:
schemaLocation="http:
//maven.apache.org/POM/4.0.0 http:
//maven.apache.org/xsd/maven-4.0.0.xsd"<
3.modelVersion<4.0.0>modelVersion<
4.
5.groupIdgroupId<
6.artifactIdartifactId<
7.version<0.0.1-SNAPSHOT>version<
8.packagingpackaging<
9.namename<
10.descriptiondescription<
11.
12.properties<
13.project.build.sourceEncodingproject.build.sourceEncoding<
14.>properties<
15.
16.dependencies<
17.dependency<
18.groupIdgroupId<
19.artifactIdartifactId<
20.version<0.10.0>version<
21.typetype<
22.>dependency<
23.
24.dependency<
25.groupIdgroupId<
26.artifactIdartifactId<
27.version<5.15.1>version<
28.>dependency<
29.dependency<
30.groupIdgroupId<
31.artifactIdartifactId<
32.version<5.15.1>version<
33.>dependency<
34.dependency<
35.groupIdgroupId<
36.artifactIdartifactId<
37.version<5.15.1>version<
38.>dependency<
39.
40.dependency<
41.groupIdgroupId<
42.artifactIdartifactId<
43.version<1.4>version<
44.>dependency<
45.dependency<
46.groupIdgroupId<
47.artifactIdartifactId<
48.version<5.1.29>version<
49.>dependency<
50.
51.dependency<
52.groupIdgroupId<
53.artifactIdartifactId<
54.version<4.0.6.RELEASE>version<
55.>dependency<
56.dependency<
57.groupIdgroupId<
58.artifactIdartifactId<
59.version<4.0.6.RELEASE>version<
60.>dependency<
61.dependency<
62.groupIdgroupId<
63.artifactIdartifactId<
64.version<4.0.6.RELEASE>version<
65.>dependency<
66.dependency<
67.groupIdgroupId<
68.artifactIdartifactId<
69.version<4.0.6.RELEASE>version<
70.>dependency<
71.dependency<
72.groupIdgroupId<
73.artifactIdartifactId<
74.version<4.0.6.RELEASE>version<
75.>dependency<
76.dependency<
77.groupIdgroupId<
78.artifactIdartifactId<
79.version<4.0.6.RELEASE>version<
80.>dependency<
81.dependency<
82.groupIdgroupId<
83.artifactIdartifactId<
84.version<4.0.6.RELEASE>version<
85.>dependency<
86.
87.dependency<
88.groupIdgroupId<
89.artifactIdartifactId<
90.version<3.2.5>version<
91.>dependency<
92.dependency<
93.groupIdgroupId<
94.artifactIdartifactId<
95.version<1.2.2>version<
96.>dependency<
97.
98.dependency<
99.groupIdgroupId<
100.artifactIdartifactId<
101.version<4.11>version<
102.>dependency<
103.
104.dependency<
105.groupIdgroupId<
106.artifactIdartifactId<
107.version<1.2.4>version<
108.>dependency<
109.dependency<
110.groupIdgroupId<
111.artifactIdartifactId<
112.version<1.7.6>version<
113.>dependency<
114.dependency<
115.groupIdgroupId<
116.artifactIdartifactId<
117.version<1.7.6>version<
118.>dependency<
119.dependency<
120.groupIdgroupId<
121.artifactIdartifactId<
122.version<1.7.6>version<
123.>dependency<
124.
125.dependency<
126.groupIdgroupId<
127.artifactIdartifactId<
128.version<3.1>version<
129.>dependency<
130.dependency<
131.groupIdgroupId<
132.artifactIdartifactId<
133.version<2.4>version<
134.>dependency<
135.dependency<
136.groupIdgroupId<
137.artifactIdartifactId<
138.version<1.2.2>version<
139.>dependency<
140.
141.dependency<
142.groupIdgroupId<
143.artifactIdartifactId<
144.version<1.2>version<
145.>dependency<
146.dependency<
147.groupIdgroupId<
148.artifactIdartifactId<
149.version<2.1>version<
150.>dependency<
151.
152.>dependencies<
153.>project<
本人使用的是开源中国的库,dcharts-widget 路径不同,所以要复制到本地的私服库,注意名字不能错。
activiti.cfg.xml:
[html]viewplaincopy
1.xmlversion="1.0"encoding="UTF-8"?
<
2.beansxmlns="http:
//www.springframework.org/schema/beans"
3.xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
4.xsi:
schemaLocation="http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans.xsd"<
5.
6.beanid="processEngineConfiguration"class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"<
7.propertyname="jdbcUrl"value="jdbc:
mysql:
///myactiviti"/<
8.propertyname="jdbcDriver"value="com.mysql.jdbc.Driver"/<
9.propertyname="jdbcUsername"value="root"/<
10.propertyname="jdbcPassword"value="123456"/<
11.propertyname="databaseSchemaUpdate"value="true"/<
12.>bean<
13.>beans<
在resource 下新建ActivitiDiagram
可以通过这个地址安装插件 《ActivitiBPMN2.0designer- http:
//activiti.org/designer/update/ 》(如果是公司需要代理才能连接外网,请注意设置代理)
将UserTask分别修改成入下图所示
修改好ID 和NAME然后在修改Main config中的Assignee,员工请假修改为张三,老板审批修改为老板
点击Window---将Create process definition image when saving the diagram勾选
然后保存bpmn文件的时候会自动帮你截图
通过ProcessEngines.getDefaultProcessEngine获取流程引擎
注:
请先在MySql建立数据库myactiviti,使用默认设置即可,然后运行App.java中的main函数,当获取到的processEngine对象不为空时 就会在数据库创建关于activiti的23张表,要保证数据库连接正常。
[java]viewplaincopy
1.publicclass App {
2.publicstaticvoid main(String[] arg){
3. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
4. }
5.}
通过源码可以看到 getDefaultProcessEngine方法会通过默认的activiti.cfg.xml文件名或者是activiti-context.xml文件名读取xml文件
具体作用如下:
跟详细的情况可以去下面这个地址了解:
此时你可以在数据库中看到act_ge_property表中插入了3条数据
将MyLeave.bpmn和MyLeave.png打包成myleave.zip文件(一定要是zip别压缩成rar)
执行以下方法,发布请假流程:
MyTest.java
附录的项目的代码,建议执行一个函数看下数据库再执行下一个,能比较快速上手数据库。
注:
保证数据库链接正常,否则processEngine会报空指针。
[java]viewplaincopy
1./**
2. * 发布流程
3. * 发布流程后,流程文件会保存到数据库中
4. */
5.@Test
6.publicvoid deployFlow(){
7. RepositoryService repositoryService = processEngine.getRepositoryService();
8.
9.//获取在classpath下的流程文件
10. InputStream in = this.getClass().getClassLoader().getResourceAsStream("myleave.zip");
11. ZipInputStream zipInputStream = new ZipInputStream(in);
12.//使用deploy方法发布流程
13. repositoryService.createDeployment()
14. .addZipInputStream(zipInputStream)
15. .name("Myleave")
16. .deploy();
17.}
查看数据库 发布的流程文件信息会保存在下面的三张表中:
我们可以通过RepositoryService获取详细的流程定义信息
[html]viewplaincopy
1.@Test
2.public void queryProcdef(){
3. RepositoryService repositoryService = processEngine.getRepositoryService();
4. //创建查询对象
5. ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
6. //添加查询条件
7. query.processDefinitionKey("myProcess");//通过key获取
8. // .processDefinitionName("My process")//通过name获取
9. // .orderByProcessDefinitionId()//根据ID排序
10. //执行查询获取流程定义明细
11. ListProcessDefinition12. for (ProcessDefinition pd :
pds) {
13. System.out.println("ID:
"+pd.getId()+",NAME:
"+pd.getName()+",KEY:
"+pd.getKey()+",VERSION:
"+pd.getVersion()+",RESOURCE_NAME:
"+pd.getResourceName()+",DGRM_RESOURCE_NAME:
"+pd.getDiagramResourceName());
14. }
15.}
key 和 name 就是我们原先画bpmn时候声明的:
使用流程定义的Key发布一个请假流程:
[java]viewplaincopy
1./**
2. * 发布流程
3. */
4.@Test
5.publicvoid startFlow(){
6.
7. RuntimeService runtimeService = processEngine.getRuntimeService();
8./**
9. * 启动请假单流程 并获取流程实例
10. * 因为该请假单流程可以会启动多个所以每启动一个请假单流程都会在数据库中插入一条新版本的流程数据
11. * 通过key启动的流程就是当前key下最新版本的流程
12. *
13. */
14. ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
15. System.out.println("id:
"+processInstance.getId()+",activitiId:
"+processInstance.getActivityId());
16. }
流程发布后在 act_ru_task ,act_ru_execution,act_ru_identitylink 表中插入流程数据
接下来就可以通过用户ID去查看该用户的任务了
[java]viewplaincopy
1./**
2. * 查看任务
3. */
4.@Test
5.publicvoid queryTask(){
6.//获取任务服务对象
7. TaskService taskService = processEngine.getTaskService();
8.//根据接受人获取该用户的任务
9. List tasks = taskService.createTaskQuery()
10. .taskAssignee("张三")
11. .list();
12.for (Task task :
tasks) {
13. System.out.println("ID:
"+task.ge