使用DdlUtils和Ant进行不同数据库之间schema和data的转换

声明:使用DdlUtils需要基本的ant知识。

 

在开发过程中常常要用到多种数据库类型,比如项目可能需要支持Oracle, MySQL, MSSQL Server, Derby等等。已经有很多各种各样的小工具可以帮助开发人员进行数据库之间的schema、data转换与同步,DdlUtils就是其中之一。它可以结合Ant或者程序代码进行数据库schema/data和xml文件的相互转换。

例如,项目开发支持的是MySQL,而为了单元测试的整洁和方便,开发人员决定使用Derby来支持单元测试。DdlUtils就可以把schema和数据从MySQL中导出,变成xml文件,然后根据需要导入到Derby中;反之亦可。显然,这些导出的xml文件也是对于数据库的一个很好的备份。

下面是apache ddlutils官网给出的一个xml的例子,table和column标签里面支持的child和attribute远不止示例中的这些。

 

<?xml version="1.0"?> <!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database.dtd"> <database name="testdb"> <table name="author"> <column name="author_id" type="INTEGER" primaryKey="true" required="true"/> <column name="name" type="VARCHAR" size="50" required="true"/> <column name="organisation" type="VARCHAR" size="50" required="false"/> </table> <table name="book"> <column name="book_id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true"/> <column name="isbn" type="VARCHAR" size="15" required="true"/> <column name="author_id" type="INTEGER" required="true"/> <column name="title" type="VARCHAR" size="255" defaultValue="N/A" required="true"/> <foreign-key foreignTable="author"> <reference local="author_id" foreign="author_id"/> </foreign-key> <index name="book_isbn"> <index-column name="isbn"/> </index> </table> </database>

 

DdlUtils包含了3个Ant Task,分别是databaseToDdl, ddlToDatabase以及dumpMetadata。

 

使用ddlToDatabase的示例:

这个Ant任务创建了一个PostgreSQL数据库,把xml中定义的schema和data导入到数据库中。

 

<path id="runtime-classpath"> <fileset dir="lib"> <include name="**/*.jar"/> <include name="**/*.zip"/> </fileset> </path> <target name="database-setup" description="Creates the database structure and inserts data into the database"> <taskdef name="ddlToDatabase" classname="org.apache.ddlutils.task.DdlToDatabaseTask"> <classpath refid="runtime-classpath"/> </taskdef> <ddlToDatabase> <database url="jdbc:postgresql://localhost/test" driverClassName="org.postgresql.Driver" username="someuser" password="somepassword"/> <fileset dir="src/schema"> <include name="project-schema.xml"/> </fileset> <createDatabase failοnerrοr="false"/> <writeSchemaToDatabase/> <writeDataToDatabase datafile="src/data/data.xml"/> </ddlToDatabase> </target>

使用databaseToDdl的示例:

这个Ant任务把derby数据库中的schema和data导出到xml文件中。

<path id="runtime-classpath"> <fileset dir="lib"> <include name="**/*.jar"/> <include name="**/*.zip"/> </fileset> </path> <target name="database-dump" description="Dumps the database structure"> <taskdef name="databaseToDdl" classname="org.apache.ddlutils.task.DatabaseToDdlTask"> <classpath refid="runtime-classpath"/> </taskdef> <databaseToDdl modelName="MyModel"> <database url="jdbc:derby:ddlutils" driverClassName="org.apache.derby.jdbc.EmbeddedDriver" username="" password=""/> <writeSchemaToFile outputFile="db-schema.xml"/> <writeDataToFile outputFile="data.xml"/> </databaseToDdl> </target>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值