2011-02-11 29 views
5

Dưới đây là pom.xml tập tin của tôi:Làm thế nào để tải dữ liệu vào cơ sở dữ liệu sử dụng DBUnit trong Maven

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

tôi chạy mvn dbunit:operation trên dòng lệnh.

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 

Nó nói xây dựng thành công. Nhưng không có dữ liệu trong cơ sở dữ liệu.

Trả lời

3

Tìm thấy câu trả lời here

Nếu tập tin dữ liệu của bạn ở định dạng phẳng (FlatXmlDataSet), thêm phẳng sẽ tiến hành cập nhật.

Ví dụ: này không hoạt động:

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

Nhưng công trình này (ít nhất là nó đã làm cho tôi):

 <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. Tạo cơ sở dữ liệu (lược đồ) trong người dùng tương ứng.
  2. Bạn phải có các tệp perperties thích hợp ở dạng XML trong dự án.
  3. Chuyển đến dự án và nhập lệnh, ví dụ: c:>project_name>mvn dbunit:operation. Điều này sẽ thêm tất cả các thuộc tính
Các vấn đề liên quan