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 ...
随机推荐
- iOS开发常用代码块(第二弹)
GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); dispat ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- TortoiseSvn的安装过程详解
运行TortoiseSVN-1.6.6.17493-win32-svn-1.6.6.msi程序, 开始安装 点击Next, 下一步 选择 I accept 接受, 点击Next, 下一步 选择安装路径 ...
- dubbox 的各种管理和监管
dubbo官方自带了dubbo-admin及dubbo-simple/dubbo-monitor-simple二个子项目用于服务治理及服务监控. 一.dubbo-admin的部署 这个比较简单,编译打 ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- c#连接关闭了,事务并没有关闭
用的是mysql引擎是InnoDB,用到了连接池. 连接还没关闭,但是事务开启,并执行了更新id=14的操作,这是把这一行锁住了,可以查询,但不能更新和删除,必需等锁释放,提交换回滚事务时锁被释放.直 ...
- 【BZOJ 3754】Tree之最小方差树
http://www.lydsy.com/JudgeOnline/problem.php?id=3754 核心思想:暴力枚举所有可能的平均数,对每个平均数排序后Kruskal. 正确的答案一定是最小的 ...
- Wpf usercontrol dispose
窗口关闭时组件"析构": public UserControl() { InitializeComponent(); ...
- 北京培训记day1
数学什么的....简直是丧心病狂啊好不好 引入:Q1:前n个数中最多能取几个,使得没有一个数是另一个的倍数 答案:(n/2)上取整 p.s.取后n/2个就好了 Q2:在Q1条件下,和最小为多少 答 ...