如何使用Maven中的dbunit将数据加载到数据库中

时间:2011-02-11 07:04:51

标签: maven dbunit

以下是我的pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
<build>
  <plugins>
    <plugin>
       <groupId>org.codehaus.mojo</groupId>
    <artifactId>dbunit-maven-plugin</artifactId>
    <version>1.0-beta-3</version>
    <configuration>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>jdbc:mysql://localhost:3306/test</url>
    <username>usernamet</username>
    <password>password</password>
    <dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
    <metadataHandlerName>org.dbunit.ext.mysql.MySqlMetadataHandler</metadataHandlerName>
    <encoding>utf-8</encoding>
    <src>target/dbunit/export.xml</src><!--compare 和 operation 要用到它 -->
    <type>CLEAN_INSERT</type><!--operation 要用到它-->
    </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> 
    <dependencies>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.13</version>
    </dependency>
    </dependencies>
    </plugin>
    </plugins>
    </build>
</project>

我在命令行上运行mvn dbunit:operation

Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app
[INFO]    task-segment: [dbunit:operation]
[INFO] ------------------------------------------------------------------------
[INFO] [dbunit:operation {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Thu Feb 10 23:02:41 PST 2011
[INFO] Final Memory: 6M/81M

它说建立成功。但是数据库中没有数据。

2 个答案:

答案 0 :(得分:4)

找到答案here

如果您的数据文件采用平面格式(FlatXmlDataSet),则添加flat将执行更新。

例如: 这不起作用:

      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>dbunit-maven-plugin</artifactId>
                    ...
            <executions>
                <execution>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>operation</goal>
                    </goals>
                    <configuration>
                        <type>CLEAN_INSERT</type>
                    <src>src/main/resources/opc1.xml</src>
                    </configuration>
                </execution>
            </executions>
        </plugin>

但这有效(至少它对我有用):

      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>dbunit-maven-plugin</artifactId>
            <version>1.0-beta-3</version>
            <dependencies>
                <dependency>
                    <groupId>com.sybase</groupId>
                    <artifactId>jConnect</artifactId>
                    ...
            <executions>
                <execution>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>operation</goal>
                    </goals>
                    <configuration>
                            <format>flat</format>
                        <type>CLEAN_INSERT</type>
                    <src>src/main/resources/opc1.xml</src>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 1 :(得分:-1)

  1. 在各自的用户中创建数据库(架构)。
  2. 您应该在项目中以XML格式拥有正确的属性文件。
  3. 转到项目并输入命令,例如 c:>project_name>mvn dbunit:operation。这将添加所有属性