一、用Ant的任务来实现

  1、DataSelt.xml内容如下:

<?xml version="1.0" encoding="GBK"?>
<dataset>
 <Personinfo id="9001" name="YuLimin" sex="0" mobile="13800138000" address="中国福建莆田" memo="Ant DBUnit Test"/>
 <Personinfo id="9002" name="俞Limin" sex="1" mobile="13800138000" address="中华人民共和国" memo="Ant DBUnit Test"/>
 <Personinfo id="9003" name="俞黎敏" sex="0" mobile="13800138000" address="中华人民共和国福建" memo="Ant DBUnit Test"/>
</dataset>
2、Ant 的 build.xml内容如下,请进行相应的改动:

<?xml version="1.0" encoding="GBK"?>
<project name="DbUnitTest" basedir="." default="initdb">
 <!-- 定义DbUnit需要的包,包括DbUnit包和jdbc包。 -->
 <property name="lib.dir" value="E:\OpenSource"/>
 <property name="dbunit.jar" value="${lib.dir}\DBUnit\dbunit-2.1.jar"/>
 <property name="jdbc.jar" value="${lib.dir}\MySQL\lib\mysql-connector-java-3.1.7-bin.jar"/>
 <property name="junit.jar" value="${lib.dir}\JUnit\junit.jar"/>
 <property name="junitdoclet.jar" value="${lib.dir}\JUnitDoclet\JUnitDoclet.jar"/>
 <property name="classes.dir" value="./classes"/>
 <!-- 在全局属性定义中定义数据库连接的url,driver,userid,password,进行多个操作可以达到重用 -->
 <property name="dburl" value="jdbc:mysql://localhost/Pagination"/>
 <property name="dbdriver" value="com.mysql.jdbc.Driver"/>
 <property name="dbuserid" value="root"/>
 <property name="dbpwd" value="password"/>
 <path id="DbUnit.classpath">
  <pathelement location="${dbunit.jar}"/>
  <pathelement location="${jdbc.jar}"/>
  <pathelement location="${junit.jar}"/>
  <pathelement location="${junitdoclet.jar}"/>
 </path>
 <target name="init">
  <!-- 定义DbUnit的Ant任务类 -->
  <taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="DbUnit.classpath"/>
 </target>
 <target name="initdb" depends="init">
  <dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
   <!-- operation的类型有:UPDATE 、INSERT、DELETE 、DELETE_ALL 、TRUNCATE 、REFRESH(MSSQL_REFRESH)、CLEAN_INSERT(MSSQL_CLEAN_INSERT)、NONE -->
   <!-- (注意:MSSQLServer中在CLEAN_INSERT和REFRESH中要使用后面的MSSQL_REFRESH和MSSQL_CLEAN_INSERT) -->
   <!-- INSERNT仅插入数据 -->
   <!-- CLEAN_INSERT清除并插入数据 -->
   <operation type="INSERT" src="DataSet.xml"/>
  </dbunit>
 </target>
 <target name="export" depends="init">
  <!-- 导出全部包含有数据的表的数据出来 -->
  <dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
   <export dest="Export.xml"/>
  </dbunit>
 </target>
 <target name="select" depends="init">
  <!-- 导出一个由查询语句产生的数据集和指定的一个表的数据 -->
  <dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
   <export dest="Select.xml">
    <query name="Personinfo" sql="Select * From Personinfo Where name='YuLimin'"/>
    <table name="Personinfo"/>
   </export>
  </dbunit>
 </target>
</project>

3、运行:ant或ant initdb或ant export或ant select不同的任务

二、直接写DbUnit的测试代码实现,DbUnitTest.java的代码如下:

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;

public class DbUnitTest extends DatabaseTestCase
{
    public DbUnitTest(String name)
    {
        super(name);
    }

    /**
     *
     * @see org.dbunit.DatabaseTestCase#getConnection()
     * @throws Exception
     * @return IDatabaseConnection
     */
    protected IDatabaseConnection getConnection() throws Exception
    {
        Class driverClass = Class.forName("com.mysql.jdbc.Driver");
        Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/Pagination","root","password");
        return new DatabaseConnection(jdbcConnection);
    }

    /**
     *
     * @see org.dbunit.DatabaseTestCase#getDataSet()
     * @throws Exception
     * @return IDataSet
     */
    protected IDataSet getDataSet() throws Exception
    {
        return new FlatXmlDataSet(new FileInputStream("DataSet.xml"));
    }

    /**
     * getSetUpOperation
     *
     * @return DatabaseOperation
     * @throws Exception
     */
    protected DatabaseOperation getSetUpOperation() throws Exception
    {
        return DatabaseOperation.REFRESH;
    }

    /**
     * getTearDownOperation
     *
     * @return DatabaseOperation
     * @throws Exception
     */
    protected DatabaseOperation getTearDownOperation() throws Exception
    {
        return DatabaseOperation.NONE;
    }

    /**
     * 运行DbUnit测试将数据导入数据库。
     */
    public void testDbUnit()
    {
        System.out.println("DbUnit Testing...");
    }
}