zoukankan      html  css  js  c++  java
  • 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);
    }
    View Code

    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);
    }
    View Code

    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);
    }
    View Code

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

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

    Reference

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

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

  • 相关阅读:
    Android Studio Tips -- 布局预览
    weibo sdk Multiple dex files define Lcom/sina/weibo/sdk/BuildConfig
    Android Studio下添加assets目录
    Android中bitmap图片透明度的处理(以撕美女衣服为例)
    Android中使用ViewPage进行广告轮播测试代码
    比赛现场打分管理平台概述
    排行榜每页默认为8行,如何把默认行数改成12行或其他行数?
    复习重点补充-
    字典读写训练
    Python爬网常见方法:find_all与re的结合使用
  • 原文地址:https://www.cnblogs.com/qingwen/p/5553565.html
Copyright © 2011-2022 走看看