ant 执行一个dbunit任务

用的时候把build.properties文件中的相关目录设置一下

lib 目录中现有
dbunit-2.4.7.jar  slf4j-api-1.6.0.jar

创建数据库时编码尽量用utf-8,否则中文乱码问题很头痛.
CREATE DATABASE /*!32312 IF NOT EXISTS*/`side-study` /*!40100 DEFAULT CHARACTER SET utf8 */;

build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant_test" default="init-db">
    <property file="build.properties" />
  
    <path id="project.lib">
        <fileset file="$(dir.lib)" includes="**/*.jar"/>
    </path>
    <taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="project.lib"/>

    <target name="init-db">  
        <sql driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}" src="${dbunit.sql}/schema.sql" onerror="continue">  
            <classpath refid="project.lib" />  
        </sql>  
      
        <dbunit driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}">  
            <dbconfig>  
                <property name="datatypeFactory" value="${dbunit.datatype}" />  
            </dbconfig>  
            <classpath refid="project.lib" />  
            <operation type="CLEAN_INSERT" src="${dbunit.data}/default-data.xml" format="flat" />  
        </dbunit>  
    </target>
    
</project>

build.properties
#ant build setting
dir.src=src
dir.report=report
dir.classes=classes
dir.lib=lib
dir.dist=dist
dir.doc=doc
main-class=zhch.Test

#dbunit setting
dbunit.sql=sql
dbunit.data=data
dbunit.sqltype=mysql
dbunit.datatype=org.dbunit.ext.mysql.MySqlDataTypeFactory
#dbunit.sqltype=h2  
#dbunit.datatype=org.dbunit.ext.h2.H2DataTypeFactory  
#dbunit.sqltype=oracle  
#dbunit.datatype=org.dbunit.ext.oracle.Oracle10DataTypeFactory  

#mysql version database setting  
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/side-study?useUnicode=true&characterEncoding=utf-8
jdbc.username=discuz
jdbc.password=discuz
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect


schema.sql  from springside3.3 mini-web

    alter table acct_user_role 
        drop foreign key FKFE85CB3E16A0ABF1;

    alter table acct_user_role 
        drop foreign key FKFE85CB3E7175E811;

    drop table if exists acct_role;

    drop table if exists acct_user;

    drop table if exists acct_user_role;

    create table acct_role (
        id bigint not null auto_increment,
        name varchar(255) not null unique,
        primary key (id)
    ) ENGINE=InnoDB;

    create table acct_user (
        id bigint not null auto_increment,
        email varchar(255),
        login_name varchar(255) not null unique,
        name varchar(255),
        password varchar(255),
        primary key (id)
    ) ENGINE=InnoDB;

    create table acct_user_role (
        user_id bigint not null,
        role_id bigint not null
    ) ENGINE=InnoDB;

    alter table acct_user_role 
        add constraint FKFE85CB3E16A0ABF1 
        foreign key (user_id) 
        references acct_user (id);

    alter table acct_user_role 
        add constraint FKFE85CB3E7175E811 
        foreign key (role_id) 
        references acct_role (id);


default-data.xml
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <acct_role ID="1" NAME="管理员"/>
  <acct_role ID="2" NAME="用户"/>
  <acct_user ID="1" EMAIL="[email protected]" LOGIN_NAME="admin" NAME="Admin" PASSWORD="admin"/>
  <acct_user ID="2" EMAIL="[email protected]" LOGIN_NAME="user" NAME="User" PASSWORD="user"/>
  <acct_user ID="3" EMAIL="[email protected]" LOGIN_NAME="user2" NAME="Jack" PASSWORD="user2"/>
  <acct_user ID="4" EMAIL="[email protected]" LOGIN_NAME="user3" NAME="Kate" PASSWORD="user3"/>
  <acct_user ID="5" EMAIL="[email protected]" LOGIN_NAME="user4" NAME="Sawyer" PASSWORD="user4"/>
  <acct_user ID="6" EMAIL="[email protected]" LOGIN_NAME="user5" NAME="Ben" PASSWORD="user5"/>
  <acct_user_role USER_ID="1" ROLE_ID="1"/>
  <acct_user_role USER_ID="1" ROLE_ID="2"/>
  <acct_user_role USER_ID="2" ROLE_ID="2"/>
  <acct_user_role USER_ID="3" ROLE_ID="2"/>
  <acct_user_role USER_ID="4" ROLE_ID="2"/>
  <acct_user_role USER_ID="5" ROLE_ID="2"/>
  <acct_user_role USER_ID="6" ROLE_ID="2"/>
</dataset>


你可能感兴趣的:(sql,mysql,ant,jdbc,ext)