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. Linux下面安装MySQL

  2. .net core ClaimsPrincipal Class

    hClaimsPrincipal Class ttps://msdn.microsoft.com/en-us/library/system.identitymodel.services.claimsp ...

  3. Java并发基础总结

    并发是一种能并行运行多个程序或并行运行一个程序中多个部分的能力.如果程序中一个耗时的任务能以异步或并行的方式运行,那么整个程序的吞吐量和可 交互性将大大改善.现代的PC都有多个CPU或一个CPU中有多 ...

  4. 结对项目:代码复审+PSP

    一.代码复审        首先我从代码风格规范和程序修改两方面进行审查. (一)代码风格规范修改 1 . 代码的部分未缩进:在用markdown粘贴代码时,需要后期tab,无形中加大工作量. 2 . ...

  5. python学习6

    1.map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. eg: 2. reduce把一个函数作用在一个序 ...

  6. 轻量级C#编辑器RoslynPad

    简介 RoslynPad是一个Apache 2.0协议开源的轻量级C#编辑器.支持自动完成,语法提示,修改建议等功能.很适合平时随手写个C#程序看看运行结果. 目前版本:0.10.1,无需保存也可以运 ...

  7. Django之Model操作

    Django之Model操作 本节内容 字段 字段参数 元信息 多表关系及参数 ORM操作 1. 字段 字段列表 AutoField(Field) - int自增列,必须填入参数 primary_ke ...

  8. 常见web攻击以及防御

    xss攻击: 跨站脚本攻击,攻击者在网页中嵌入恶意代码,当用户打开网页,脚本程序便开始在客户端的浏览器上执行,以盗取客户端cookie,用户名密码,下载执行病毒木马程序,甚至是获取客户端admin权限 ...

  9. 较为完整的meta

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. C语言中struct位域的定义和使用

    位域的定义和使用 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又 ...