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

发布于 2024-10-16 16:21:09 字数 3260 浏览 7 评论 0原文

下面是我的 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

就说构建成功了。但数据库中没有数据。

Below is my pom.xml file:

<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>

I run mvn dbunit:operation on command line.

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

It says build successful. But there is no data in the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-10-23 16:21:09

此处找到了答案

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

例如:
这不起作用:

<前><代码> <插件>
org.codehaus.mojo;
dbunit-maven-plugin;
...
<处决>
<执行>
<阶段>测试编译
<目标>
<目标>操作

<配置>
<类型>CLEAN_INSERT
src/main/resources/opc1.xml



但这是有效的(至少对我来说是这样):

<前><代码> <插件>
org.codehaus.mojo;
dbunit-maven-plugin;
<版本>1.0-beta-3
<依赖关系>
<依赖关系>
com.sybase;
jConnect;
...
<处决>
<执行>
<阶段>测试编译
<目标>
<目标>操作

<配置>
<格式>平面
<类型>CLEAN_INSERT
src/main/resources/opc1.xml



Found the answer here

If your data file is in flat format (FlatXmlDataSet), adding flat will perform the update.

For example:
This doesn't work:

      <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>

But this works (at least it did for me):

      <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>
从此见与不见 2024-10-23 16:21:09
  1. 在各个用户中创建数据库(架构)。
  2. 您应该在项目中拥有 XML 形式的正确属性文件。
  3. 转到项目并输入命令,例如
    c:>project_name>mvn dbunit:operation。这将添加所有属性
  1. Create the database(schema) in respective user.
  2. You should have the proper perperties files in XML form in the project.
  3. Go to the project for and type the command e.g.
    c:>project_name>mvn dbunit:operation. This will add all the properties
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文