Introduction 

DdlUtils is a small, easy-to-use component for working with Database Definition (DDL) files. These are XML files that contain the definition of a database schema, e.g. tables and columns. These files can be fed into DdlUtils via its Ant task or programmatically in order to create the corresponding database or alter it so that it corresponds to the DDL. Likewise, DdlUtils can generate a DDL file for an existing database.

DdlUtils既可以根据定义数据库schema的XML文件(DDL文件)创建或者修改数据库,也可以为现有的数据库生成一个DDL文件。

简单来讲,DdlUtils既是一个library,也是操作数据库schema的Ant tasks。它可以创建和删除数据库,创建、修改和删除表。此外,还可以将XML中定义的数据插入数据库,或者将数据库中的数据读到XML文件中。

DdlUtils追求数据库独立性。schema文件是Turbine XML格式。这种XML格式文件同样用在 Torque 和 OJB 中。

DdlUtils APIs

DdlUtils的核心是定义在org.apache.ddlutils.model中的数据库model,它由表示数据库的schema的各种类组成。

Reading from XML

import org.apache.ddlutils.io.DatabaseIO;
import org.apache.ddlutils.model.Database; ... public Database readDatabaseFromXML(String fileName)
{
return new DatabaseIO().read(fileName);
}

Writing to XML

import org.apache.ddlutils.io.DatabaseIO;
import org.apache.ddlutils.model.Database; ... public void writeDatabaseToXML(Database db, String fileName)
{
new DatabaseIO().write(db, fileName);
}

Reading the model from a live database

import javax.sql.DataSource;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.model.Database; ... public Database readDatabase(DataSource dataSource)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource); return platform.readModelFromDatabase("model");
}

Changing a database

修改数据库有两种方式:将数据库重置为model定义样子;修改数据库为model定义的样子。可以理解成对数据库schema的修改。

import javax.sql.DataSource;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.model.Database; ... public void changeDatabase(DataSource dataSource,
Database targetModel,
boolean alterDb)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource); if (alterDb)
{
platform.alterTables(targetModel, false);
}
else
{
platform.createTables(targetModel, true, false);
}
}

Inserting data into database

DdlUtils使用 DynaBeans 插入数据。对于数据库model定义的每个table,a so-called dyna class is created that represents the table with its columns.

import javax.sql.DataSource;
import org.apache.commons.beanutils.DynaBean;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.model.Database; ... public void insertData(DataSource dataSource,
Database database)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource); // "author" is a table of the model
DynaBean author = database.createDynaBeanFor("author", false); // "name" and "whatever" are columns of table "author"
author.set("name", "James");
author.set("whatever", new Integer(1234)); platform.insert(database, author);
}

Getting data from a database

同插数据一样,DdlUtils使用 dyna beans 来获取数据库放回的数据。

import java.util.ArrayList;
import java.util.Iterator;
import javax.sql.DataSource;
import org.apache.commons.beanutils.DynaBean;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.Table; ... public void dumpBooks(DataSource dataSource,
Database database)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
ArrayList params = new ArrayList(); params.add("Some title"); Iterator it = platform.query(database,
"select * from book where title = ?",
params,
new Table[] { database.findTable("book") }); while (it.hasNext())
{
DynaBean book = (DynaBean)it.next(); System.out.println(book.get("title"));
}
}

Note: 以上只是样例APIs,仅供参考。

Reference

https://db.apache.org/ddlutils/api-usage.html

https://db.apache.org/ddlutils/databases/mysql.html

Apache DdlUtils入门的更多相关文章

  1. Apache Maven 入门篇 ( 上 )

    作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法. 这个入门篇分上下两篇.本文着重动手,用 mav ...

  2. [转]Apache Maven 入门篇 ( 上 )

    原文地址:Apache Maven 入门篇 ( 上 ) 作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这 ...

  3. [转]Apache Maven 入门篇(下)

    原文地址: Apache Maven 入门篇(下) 作者:George Ma 第一篇文章大概的介绍了一下Apache Maven以及它的下载和安装,并且运行了一个简单的示例.那么在对maven有了一点 ...

  4. Apache Maven 入门

    Apache Maven 入门篇 ( 上 ) Apache Maven 入门篇 ( 下 ) ~$mvn archetype:generate -DgroupId=com.mycompany.hello ...

  5. JAVAEE——BOS物流项目07:WebService入门、apache CXF入门、基于CXF发布CRM服务

    1 学习计划 1.WebService入门 n 什么是WebService n 调用网络上的WebService服务 n SOAP和WSDL概念 n 基于JDK1.7发布一个简单的WebService ...

  6. Apache Curator入门实战

    Apache Curator入门实战 Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeep ...

  7. Apache Solr入门教程(初学者之旅)

    Apache Solr入门教程(初学者之旅) 写在前面:本文涉及solr入门的各方面,建议边思考边实践,相信能帮助你对solr有个清晰全面的了解并能简单实用. 在Apache Solr初学者教程的这个 ...

  8. [Maven]Apache Maven 入门篇

    作者:George Ma 上 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 ma ...

  9. 【Tools】Apache Maven 入门篇 ( 上 )

    作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 mave ...

随机推荐

  1. jni操作jobject

    一. 注册JNI函数 1.         静态方法 一般使用javah进行编译,生成很长的文件名和函数名字,这个书写不方便,影响运行效率. 2.         动态注册 使用JNINativeMe ...

  2. 地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了

    地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了 四叉树对于区域查询,效率比较高. 原理图

  3. hdu----(1849)Rabbit and Grass(简单的尼姆博弈)

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. BZOJ4300绝世好(傻)题

    Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...

  5. Spring框架值注解

    注解配置概括 Spring可以按指定的包路径扫描内部的组件,当发现组件类定义前有一下的注解标记,会将该组件纳入Spring容器中. 1)@Component(其他组件) 2)@Controller(A ...

  6. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  7. 大众点评cat系统的搭建笔记

    项目地址:https://github.com/dianping/cat 编译步骤: 这个项目比较另类,把编译需要的jar包,单独放在git分支mvn-repo里了,而且官方文档里给了一个错误的命令提 ...

  8. go channel

    channel 是go语言中不同goroutine之间通信一种方法 //定义一个channel c := make(chan bool) //向channel中写入 c <- true //读取 ...

  9. javascipt的【函数表达式】

    函数表达式 在编程时,我们可以看到不管是什么类库,jquery也好,zepto也好,都会用到大量的命名函数和匿名函数表达式,本节点就是为了弄懂为何会有这些函数表达式,以及在什么情况下会使用到这些表达式 ...

  10. Twisted随笔

    学习了socket后决定尝试使用框架,目标锁定了Twisted. 什么是Twisted? twisted是一个用python语言写的事件驱动的网络框架,他支持很多种协议,包括UDP,TCP,TLS和其 ...