准备Activiti环境(2)
4.初始化数据库
方法一:
1 @Test 2 public void createTable() { 3 //1.创建Activiti配置对象的实例 4 ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); 5 //2.设置数据库连接信息 6 // 设置数据库地址 7 configuration.setJdbcUrl("jdbc:mysql://localhost:3306/testactiviti?createDatabaseIfNotExist&useUnicode=true&characterEncoding=utf8"); 8 // 数据库驱动 9 configuration.setJdbcDriver("com.mysql.jdbc.Driver"); 10 // 用户名 11 configuration.setJdbcUsername("root"); 12 // 密码 13 configuration.setJdbcPassword("root"); 14 // 设置数据库建表策略 15 /** 16 * DB_SCHEMA_UPDATE_TRUE:如果不存在表就创建表,存在就直接使用 17 * DB_SCHEMA_UPDATE_FALSE:如果不存在表就抛出异常 18 * DB_SCHEMA_UPDATE_CREATE_DROP:每次都先删除表,再创建新的表 19 */ 20 configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); 21 //3.使用配置对象创建流程引擎实例(检查数据库连接等环境信息是否正确) 22 ProcessEngine processEngine = configuration.buildProcessEngine(); 23 System.out.println(processEngine); 24 }
方法二:执行sql脚本文件activiti-5.13\database\create\activiti.mysql.create.*.sql文件
方法三:在类路径下创建Activiti.cfg.xml。
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 6 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 7 </beans>
1 <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 2 <!-- 数据库连接配置 --> 3 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testactiviti?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8"></property> 4 <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property> 5 <property name="jdbcUsername" value="root"></property> 6 <property name="jdbcPassword" value="root"></property> 7 <!-- 建表策略 --> 8 <property name="databaseSchemaUpdate" value="true"></property> 9 </bean>
1 @Test 2 public void testDBByProperties() throws Exception { 3 // 1。 加载classpath下名为activiti.cfg.xml文件,创建核心流程引擎对象 4 ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine(); 5 System.out.println(processEngine); 6 }