activiti(一):idea整合spring和avtiviti
目录:activiti 目录
一、 创建maven项目
1.1 创建项目
- 新建maven项目:
一路next得到:
- pom包:
- spring包(根据需要导入)
- activiti包
- test包(我这里都在测试中执行,所以导入这个包)
- mysql包
<!-- Test Dependency Begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Test Dependency End -->
<!-- Spring Dependency Begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- Spring Dependency End -->
<!--Activiti Dependency Begin -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>6.0.0</version>
</dependency>
<!--Activiti Dependency End -->
<!-- Mysql Begin-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- Mysql End -->
1.2 安装activiti插件
1.3 创建activiti流程图
-
我这里创建文件名:apply
-
画图
我们创建一个审批流程,将右侧的图标拖到中间,在图标中间起始连接到下一个节点生成连线!
-
开始节点
-
任务节点:报销申请
- 从apply开始节点拉一条线到下一个节点
- 从apply开始节点拉一条线到下一个节点
-
任务节点:财务审批
-
任务节点:经理审批
-
任务节点:总经理审批
-
结束节点
-
导出流程图片,idea对activiti的支持不算很好,导出图片不方便。
- 画图之后会生成apply.bpmn文件,将该文件改名为:apply.xml
-
这时候如果出现乱码,别慌,这是由于支持不行导致的(最好使用eclipse,我见同事用eclipse画流程图贼方便)。处理方法
- 找到idea的bin目录
- 修改启动环境变量文件
- 最后一行追加:-Dfile.encoding=UTF-8
- 重启idea
-
导出图片
-
先打开xml的diagrams
-
导出png为apply.png
-
将导出的png文件跟apply.xml放到一起,然后将apply.xml名字改回apply.bpmn
-
1.4 配置文件
-
创建配置文件:activiti.cfg.xml。
这是它的默认文件名,不要瞎改...,看源码:
-
文件内容:配置数据库名为activitidemo(可以随自己起名,但是要先在数据库创建它)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="false">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activitidemo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8"></property>
<property name="jdbcUsername" value="root"></property>
<property name="jdbcPassword" value="root"></property>
<property name="databaseSchemaUpdate" value="true"></property>
</bean>
</beans>
- 创建数据库
1.5 单元测试类
-
初始化数据库:
/** * 初始化数据库 * @author caodahuan * @date 2019/9/3 * @return void */ @Test public void createTable(){ final ProcessEngine processEngine = ProcessEngineConfiguration .createProcessEngineConfigurationFromResource("activiti.cfg.xml") .buildProcessEngine(); } }
这里报错:org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table 'activiti.act_ge_property' doesn't exist
The error may exist in org/activiti/db/mapping/entity/Property.xml
The error may involve org.activiti.engine.impl.persistence.entity.PropertyEntityImpl.selectProperty-Inline
The error occurred while setting parameters
SQL: select * from ACT_GE_PROPERTY where NAME_ = ?
Cause: java.sql.SQLSyntaxErrorException: Table 'activiti.act_ge_property' doesn't exist
郁闷得很...仔细检查了多次为什么不行。最后发现,原来我曾经创建过一次activititest库,在里面执行过一次创建。这次用的库名为activitidemo,我本来以为完全独立,不会有问题,但是报错。删掉activititest库,则可以执行成功。
原因:
我们策略设置了“如果不存在则创建表”,而我新建库,必定会新建表,但是没有新建,反而报错。于是,开始源码追踪,最后发现,去数据库判断是否存在表时调用了ResultSet getTables,我传入参数activitidemo,指定查此库,但最后返回结果依然能得到表。这是为什么呢....
这个方法应该是不能查到表的(该数据库是我新建的,里面没有表,根据策略,没表就要创建表),但是偏偏查到了表,于是我百度了下这个方法得到:
解决:
我将数据库连接配置初始化参数加上:nullCatalogMeansCurrent=true。再次模拟情况,问题解决!
变为:
<property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activitidemo?nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8"></property> <property name="jdbcUsername" value="root"></property> <property name="jdbcPassword" value="root"></property> <!-- 如果表不存在,则创建 --> <property name="databaseSchemaUpdate" value="true"></property>
-
总结:
在经过上述过程后,数据库创建表,如果按照pom里面版本,应该是25张表。到此已经将spring和activiti整合起来,并创建了一个流程。但是流程的操作,还没有开始...这将是一个复杂的过程。
-
参考:
https://blog.csdn.net/cs_hnu_scw/article/details/79059965
activiti6.x源码