Apache DdlUtils入门
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入门的更多相关文章
- Apache Maven 入门篇 ( 上 )
作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法. 这个入门篇分上下两篇.本文着重动手,用 mav ...
- [转]Apache Maven 入门篇 ( 上 )
原文地址:Apache Maven 入门篇 ( 上 ) 作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这 ...
- [转]Apache Maven 入门篇(下)
原文地址: Apache Maven 入门篇(下) 作者:George Ma 第一篇文章大概的介绍了一下Apache Maven以及它的下载和安装,并且运行了一个简单的示例.那么在对maven有了一点 ...
- Apache Maven 入门
Apache Maven 入门篇 ( 上 ) Apache Maven 入门篇 ( 下 ) ~$mvn archetype:generate -DgroupId=com.mycompany.hello ...
- JAVAEE——BOS物流项目07:WebService入门、apache CXF入门、基于CXF发布CRM服务
1 学习计划 1.WebService入门 n 什么是WebService n 调用网络上的WebService服务 n SOAP和WSDL概念 n 基于JDK1.7发布一个简单的WebService ...
- Apache Curator入门实战
Apache Curator入门实战 Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeep ...
- Apache Solr入门教程(初学者之旅)
Apache Solr入门教程(初学者之旅) 写在前面:本文涉及solr入门的各方面,建议边思考边实践,相信能帮助你对solr有个清晰全面的了解并能简单实用. 在Apache Solr初学者教程的这个 ...
- [Maven]Apache Maven 入门篇
作者:George Ma 上 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 ma ...
- 【Tools】Apache Maven 入门篇 ( 上 )
作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 mave ...
随机推荐
- kubernetes部署Fluentd+Elasticsearch+kibana 日志收集系统
一.介绍 1. Fluentd 是一个开源收集事件和日志系统,用与各node节点日志数据的收集.处理等等.详细介绍移步-->官方地址:http://fluentd.org/ 2. Elastic ...
- MFC快速入门 - 菜单
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6231104.html 打开VS2010,依次打开File – New – Proje ...
- C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器
必须JobManager初始化 方式1: public void Start() { JobManager.AddJob(() => FetchingDa ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- csv表格处理(下)--纯JS解析导入csv
多日前的上篇介绍了csv表格,以及JS结合后端PHP解析表格填充表单的方法.其中csv转换成二维数组的时候逻辑比较复杂多坑,幸好PHP有丰富的库函数来处理,而现在用JS解析的话就没有那么幸运了,一切都 ...
- IOS比较常用的第三方组件及应用源代码(持续更新中)
把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新~ 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...
- css 的一些基本操作
日常基本使用的一些操作,持续完善中: 设置按钮圆角:border-radius:5px; 设置高度:height: 30px; 设置宽度:width: 64px; 使用span标签内容过长自动换行解决 ...
- Ubuntu下运行Shell脚本
将一个脚本保存为带有扩展名的文件,如test.sh, 在终端上输入: chmod +x test.sh 然后输入: test.sh 终端显示: test.sh 找不到命令(后来看了鸟个私房菜,发现这样 ...
- JavaScript零基础学习系列五
定时器 1.定时器:设定时间,在指定的时间之后执行函数或者是程序 a.反复性定时器:var dingshiqi=Window.setInterval("函数名()",时间n[毫 ...