---
title: "jBPM(五): 部署jBPM所用表到数据库_2"
author: "rmn190"
date: "2009-03-21T06:51:03Z"
tags: ["Ant", "Hibernate", "JBPM", "MySQL", "XML"]
categories: ["企业架构"]
slug: "jbpm-jbpm-2"
draft: false
lastmod: "2009-03-31T10:02:21Z"
id: 352550
url: "https://rmn190.iteye.com/blog/352550"
---

在[jBPM(四)](/blog/351271) 中, 笔者介绍了如何通过ant命令来生成建表语句并手工地往数据库里建表.这篇接着来看两种方式: 如何利用ant命令来自动建表,通过Java代码自动建表.

        先看 如何利用ant命令来自动建表. 应该说这方式在实际中更方便,也最常用. 具体步骤如下:  
        1, 在JBPM\_HOME \\jbpm\\lib下新建mysql文件夹, 并将Mysql的驱动包拷到这里.  
        2, 在JBPM\_HOME \\jbpm\\src\\resources下新建mysqldb文件夹. 并将 JBPM\_HOME \\jbpm \\src\\resources\\hsqldb下的两个文件"create.db.hibernate.properties" 和"identity.db.xml"拷到新建的mysqldb文件夹下. 修改create.db.hibernate.properties文件,内容如下:

```
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
            hibernate.connection.driver_class= com.mysql.jdbc.Driver
            hibernate.connection.url=jdbc:mysql://localhost:3306/jbpm
            hibernate.connection.username=root
            hibernate.connection.password=yourmysqlrootpw
            hibernate.show_sql=false

            hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
```

        3, 修改 JBPM\_HOME\\build.deploy.xml文件. 新增一个target,内容如下:

```
<target name="create.mysql.db" depends="declare.jbpm.tasks, db.clean" 
  description="creates a hypersonic database with the jbpm tables and loads the processes in there">
    <jbpmschema actions="create"
                cfg="${basedir}/src/config.files/hibernate.cfg.xml"
                properties="${basedir}/src/resources/mysqldb/create.db.hibernate.properties"/>
    <loadidentities file="${basedir}/src/resources/mysqldb/identity.db.xml"
                cfg="${basedir}/src/config.files/hibernate.cfg.xml"
                properties="${basedir}/src/resources/mysqldb/create.db.hibernate.properties"/>
    <ant antfile="build.xml" target="build.processes" inheritall="false" />
    <deployprocess cfg="${basedir}/src/config.files/hibernate.cfg.xml"
                   properties="${basedir}/src/resources/mysqldb/create.db.hibernate.properties">
      <fileset dir="build" includes="*.process" />
    </deployprocess>
  </target>

```

        4, 启动MySQL数据库,在JBPM\_HOME \\jbpm 目录下运行"ant create.mysql.db -buildfile build.deploy.xml"命令. 这样又是一段更长的"唰唰唰唰...."后,进到jbpm这个database下, 再show tables,跟上篇介绍的方法一样, 又看到33个表建成了.

        我们来简短地看下最后一个方法: 利用Java代码自动建表. 由于这个很不常用, 我也就偷下懒, 大家如有兴趣,可借鉴下"jbpm\\src\\java.examples\\org\\jbpm\\tutorial\\db"目录下 HelloWorldDbTest.java类中的方法"jbpmConfiguration.createSchema();". 更好的方式是采用[jBPM(三)](/blog/350663 "jBPM(三)") 里搭建好的Eclipse环境,直接运行HelloWorldDbTest.java.