您的位置:首页 > 运维架构 > Apache

Apache DdlUtils入门

2016-06-02 16:35 316 查看

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格式文件同样用在 TorqueOJB 中。

DdlUtils APIs

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

Reading from XML

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"));
}
}


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

Reference

https://db.apache.org/ddlutils/api-usage.html
https://db.apache.org/ddlutils/databases/mysql.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: