使用maven自动创建数据库,导入导出数据需要用到两个插件hibernate3-maven-plugin,dbunit-maven-plugin

hibernate3-maven-plugin,有7大功能,(这里我们只使用hbm2ddl,自动生成数据库)
The Maven Hibernate3 Plugin has seven goals.

hibernate3:hbm2cfgxml: Generates hibernate.cfg.xml
hibernate3:hbm2ddl: Generates database schema.
hibernate3:hbm2doc: Generates HTML documentation for the database schema.
hibernate3:hbm2hbmxml: Generates a set of hbm.xml files
hibernate3:hbm2java: Generates Java classes from set of *.hbm.xml files
hibernate3:schema-export: Creates SQL DDL file and generates the database schema from set of *.hbm.xml files
hibernate3:schema-update: Updates the database schema based on the set of *.hbm.xml files

dbunit-maven-plugin,可以实现数据库中数据的导入导出

dbunit:operation: Execute a database operation using an external dataset file.
dbunit:export: Export database tables into a dataset file.
dbunit:compare: Compare a dataset with database.


具体配置,(MYSQL为例)

在pom.xml中添加下面的代码

<build>

  <plugins>

 

     <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.0-alpha-1</version>
        <dependencies>
           <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>5.0.4</version>
          </dependency>
        </dependencies>
        <configuration>
          <components>
            <component>
              <name>hbm2ddl</name>
              <implementation>
                 annotationconfiguration
              </implementation>
            </component>
          </components>
          <componentProperties>
            <drop>true</drop>
            <jdk5>true</jdk5>
            <propertyfile>
               /src/main/resources/jdbc.properties
            </propertyfile>
            <configurationfile>
              /src/main/resources/hibernate.cfg.xml
            </configurationfile>
          </componentProperties>
        </configuration>
        <executions>
          <execution>
            <phase>process-test-resources</phase>
            <goals>
               <goal>hbm2ddl</goal>
            </goals>
         </execution>
       </executions>
   </plugin>
 
    <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>dbunit-maven-plugin</artifactId>
       <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.4</version>
          </dependency>
        </dependencies>
        <configuration>
          <dataTypeFactoryName>
              org.dbunit.dataset.datatype.DefaultDataTypeFactory
          </dataTypeFactoryName>
          <driver>com.mysql.jdbc.Driver</driver>
          <url>
             jdbc:mysql://localhost/projectStencil?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&sessionVariables=FOREIGN_KEY_CHECKS=0
         </url>
         <username>root</username>
         <password></password>
         <src>src/test/resources/sample-data.xml</src>
         <type>CLEAN_INSERT</type>
       </configuration>
       <executions>
          <execution>
             <id>test-compile</id>
             <phase>test-compile</phase>
             <goals>
               <goal>operation</goal>
             </goals>
          </execution>
          <execution>
             <id>test</id>
             <phase>test</phase>
             <goals>
                <goal>operation</goal>
            </goals>
          </execution>
       </executions>
   </plugin>
  
 

</plugins>

</build>



其中jdbc.properties 内容为

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.username=root
hibernate.connection.password=
hibernate.connection.url=jdbc:mysql://localhost/projectStencil?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
hibernate.connection.driver_class=com.mysql.jdbc.Driver



hibernate.cfg.xml 举例如下


<hibernate-configuration>
 <session-factory>
       <mapping class="com...dao.model.User" />

        ...

  </session-factory>
</hibernate-configuration>



sample-data.xml 为要导入数据库的数据

可以使用 mvn dbunit:export -Ddest=sample-data.xml 将数据库的数据导出来
评论
kucawa 2008-01-17
,兄弟,我个你up!!
发表评论

您还没有登录,请登录后发表评论