Tuesday, October 12, 2010

DB2 pureXML a portfolio of dynamic programming: iBatis + BeanUtils + JiBX



DB2 pureXML-based application development model

This paper describes the development model to be the main variable for the needs of application development, using a variety of tools and techniques to enhance the flexibility of the framework. With the XML description of the capacity and flexibility to be a growing recognition that many XML-related technologies emerged, more and more applications using XML technology. An XML-based application development often can be divided into several levels: data storage layer, data access layer, business logic and interface presentation layer.

DB2 pureXML technology to mature for these XML-based application development provides a strong support. pureXML technology provides efficient XML storage options, while supporting standard for XML Query Language, allows application programs can easily and effectively organize XML data in the data provided Linghuo Xing Cun Chuceng. The pureXML technology in order to better integrate into the entire application development, the need to examine other aspects of support to technology.

DB2 pure XML-based application development, data access layer using iBatis would be very appropriate. iBatis is an open source persistence framework, which supports POJO class and the mapping between database tables for the application of upper provides data access abstraction. Compared to other ORM framework, iBatis DB2 pure XML-based application development has its own advantages. First of all, iBatis mapping provides a flexible definition of way, developers can use SQL / XQUERY statements describe the logical organization of database data, XML data, with the introduction of the definition and implementation of such a mapping system to show a good applicability, and XQUERY other ORM framework support is not very good. Secondly, iBatis mapping file allows the development SQL / XQUERY statement, to provide developers greater flexibility, you can use efficient SQL / XQUERY statements to access data.

In the business logic layer, using JiBX and BeanUtils, JiBX provides support for data binding, DeanUtils was provided to support dynamic class.

Complex business logic, first of all necessary documents to complete POJO and XML data binding. JiBX is a data binding framework that can achieve the XML data binding between Java objects, to meet the application installed between the two exchange (Marshal / Unmarshal) requirements. Comparison of other data binding frameworks, JiBX uses byte code enhancement, and XPP (XML Pull Parsing) technology, with excellent performance (see References [5]). DB2 pure XML-based application development, for more frequent data and Java objects into XML, JiBX is a good choice.

As used in business logic POJO corresponding XML data representation, and the need to dynamically change the POJO to adapt to change. BeanUtils tools to support dynamic changes to Java Bean, you can dynamically POJO, have adapted to meet the needs of some of the data structure changes. Provide the tools to use BeanUtils Another advantage is that you can improve development efficiency, developers can easily use less code to achieve some of the operations of the Bean.

Displayed in the interface layer, the introduction of XSLT technology. XSLT is a transformation language based on XML document can be defined XML style sheet in accordance with the conversion. XSLT allows XML data to its form of decoupling, the same XML data, you can apply a different style sheet, so as to achieve different forms. Changes in demand for the show floor, you can just modify the style sheet. This form of XML data and their loose coupling between the front-end for the XML application development lead to higher scalability, improved the flexibility of XML application development.

The layers of the test, DB2 pureXML-based application development provides a useful model. This article will briefly introduce the next development in the technology tools they need to give readers a better understanding of this development model.

Background

pureXML

XML is self-descriptive (comprehensibility), flexibility, platform independence and the advantages of standardization, the best technology is widely applied to various fields. DB2 9 provides a good for the XML data storage and management mechanisms, IBM will be the technology known as "pureXML". XML for DB2 V9 on the original ecology of efficient storage, and supports SQL and XQuery query methods. Moreover, all the relational mode of operation and database tools can be used on XML, such as indexing mechanism, import and export, related queries, high-speed bulk loading, system optimization and so on. PureXML features in the support, DB2 V9 as a support both relational data and also supports XML-primitive twin-engine database worthy of the name. With the strong support of the XML data, DB2 pureXML in the enterprise value is also increasingly widespread attention.

iBatis

IBatis is a "semi-automatic" in the ORM framework. Here, "semi-automatic" is a relative Hibernate provides a comprehensive database such as encapsulation mechanism of "fully automated" ORM implementation, the programmer is often simply defined the POJO mapping to the database table, SQL can automatically generate and implement , through Hibernate or OJB provides the procedure for the persistence operation. The iBatis's focus, then lies between POJO and SQL mapping. In other words, iBatis and programmers will not automatically generated at run SQL / XQuery implementation of the specific SQL / XQuery require programmers to write, and then through the mapping configuration file, the SQL / XQuery required parameters, and return the results field is mapped to the specified POJO. This semi-automatic into the developer to provide some space for free play.

The following work on the machine on the iBatis make a brief introduction, the reader can provide reference for further access to this article.

IBatis design patterns is a core part of the SQL Map, it needs to provide an XML file to get the database connection and simple Java bean mapping with the SQL statement. And not just a SQL Map XML file that actually a design pattern. In the implementation of the mapping statement, SQL Map will create a JDBC PreparedStatement instance of using the objects described in XML as a PreparedStatement instance parameters, the implementation of PreparedStatement and the results from the Result object is created, the implementation of the process in Figure 1, said:

Figure 1. iBatis implementation of the flowchart






JiBX

JiBX is a very good framework for XML data binding, JiBX conversion efficiency is a lot of open source projects can not be compared. It is bound by a flexible mapping of data objects and file conversion between XML documents, the text block is the connection between XML data and Java objects bridge.

A simple example of binding shown in Figure 2:

Figure 2. The definition of a simple binding






In this case, JiBX binding file defined in the XML sub-elements and members of the mapping Java classes to XML elements to Java object properties with the corresponding link. JiBX binding definitions provide a very flexible, interested readers can learn more from the Resources.

Using JiBX binding XML documents and Java objects are divided into two steps:

The first step is to compile the bindings, that is your definition of practical completion of the binding compiler into class files. JiBX provides a binding for this purpose compiler. Can run the compiler, one or more binding definition file path as an argument to run org.jibx.binding.Compile program. For example: If the JiBX installation path C:, you can define the binding path where the file binding.xml run the following command:

java-jar C: jibxjibx-bind.jar binding.xml

If you use the Ant build tool to compile the work completed, some details see References [4].

The second step is running bind, XML documents and to achieve the mutual conversion between Java instances, use the JiBX runtime to the document data marshalling (marshal) and data compiled from (unmarshal). Data marshalling Java objects generated by the process of XML text block, the data compiled from the text based XML Java object the establishment of the process block.

BeanUtils

Apache Jakarta Commons BeanUtils project is a powerful component. Mainly focused on Java reflection and introspection API, package, its aim is to use reflection on the JavaBean properties for processing. BeanUtils advantage is the ability to achieve a dynamic Bean, that is not pre-define a standard JavaBean class, but based on business need to add properties dynamically at any time and can be used as a JavaBean, which can operate with the BeanUtils methods other JavaBean operate the same.

BeanUtils was divided into four packages:

org.apache.commons.beanutils

org.apache.commons.beanutils.converters

org.apache.commons.beanutils.locale

org.apache.commons.beanutils.locale.converters

The first two of which are BeanUtils the default implementation, it does not have any treatment for localized, this can improve the efficiency. But if your application for the localization of a requirement, then it is safer to use the latter two packages.

Here are some typical BeanUtils methods:

(1) LazyDynaBean: it implements a dynamic Bean, directly entered, and joined the property, and the same use as a JavaBean. For example:

/ * Used here LazyDynaMap, it is a lightweight implementation LazyBean backup / bin / conf / data / log / maint / svn / tmp /

LazyDynaMap dynaBean = new LazyDynaMap ();

dynaBean.set ("name", "Jone"); / / set simple type

dynaBean.set ("address", "stress", "HaiDian"); / / set map type

dynaBean.set ("phone", 0, "82502987"); / / set index type

Map myMap = dynaBean.getMap (); / / retrieve the Map

(2) copies of property: the method can clone directly between Bean (clone), such as:

BeanUtils.copyProperties (c1, c2);

But this copy is shallow copy, copy and two Bean may have the same object reference to be careful when using this.

XSLT

XSLT is an XML document will be converted to other forms of documentation of the language, is the W3C's recommendation. Have mentioned that XSLT XSL (eXtensible Stylesheet Language), can Extensible Stylesheet Language, is also the recommendation of W3C standards. XSL is the XML style sheet, it is similar as the HTML style sheet CSS, but it is also much more complex than CSS, it actually consists of three languages: the language XML transformation XSLT, XML part of the definition of the language or mode of XPath, display the definition of XML languages - XSL formatting objects.

XSLT is the XSL standard, the most important part, it used to be an XML document into another XML document or another type of document. The XML into HTML is one of the main functions of XSLT. XSLT can be added to the output file a new element, or remove some elements. It can be re-arranged to classify these elements and elements to test and determine which elements and so on show.

Describe the conversion process of a common argument is: XSL with XSLT to an XML source tree into another XML result tree (or an XML source document into another XML result document), Figure 3 shows that the process:

Figure 3. XSLT input and output of the tree structure






Scenarios and solutions

The first part of this article on the pureXML, iBatis, JiBX, BeanUtils, XSLT and other technologies were introduced in detail, then how in the development of practical applications with pureXML storage flexibility, iBatis data access flexibility, BeanUtils and dynamic class JiBX generation, and XSLT to bring the interface to show flexibility in order to achieve a high degree of flexibility in the software framework, which is the focus of this article.

The following application on a specific scenario, for example, a brief introduction to the scene after the business process, will give a detailed program design ideas.

Scenarios

This application scenario is a resource exchange platform to provide resources primarily for users to upload and download capabilities, users can also edit existing resource information, such as adding some descriptive information, resources, links to related resources, or upload . Because the wide variety of different users to upload resources, descriptions quite abundant, resource information using XML as the description is undoubtedly the best choice.

The scenarios of the business process framework shown in Figure 4:

Figure 4. Business Process Framework Chart






If a user uploads a resource of new, which was necessary following the descriptions of new resources are added to enough free space to users, platforms, a dynamic model customization ways to let users define the information input of Mo Ban Xian Huozhexuanze others off the definition of template, and then re-input information based on the template. This benefit is not only the free space left to the user, while enabling the system to capitalize on these rich XML data.

If users want to edit an existing resource descriptions, the platform of the first display of personalized existing descriptions and receive user to add new information, taking into account some of the module interfaces Xuyao to Java objects in the form as a parameter to deal with these descriptions, the platform should can provide the edited information Java object, or directly to the database.

Solutions

This data exchange platform is characterized by the largest data processing flexibility, and with the growing volume of business data have gradually become complex, making the rigid structure of ER model can not adapt to the complexity of data, flexibility, level sex, and individual differences, and since XML is self-describing features, a good deal of information can be complexity, understandability, flexibility and scalability challenges, so using XML as an information carrier understandable.

DB2 V9.5 for pureXML provides efficient management of XML data, features, and for the application to provide simple and convenient access method, the programmer just follow W3C developed XML / SQL standard can easily operate on XML data, therefore, choose pureXML no doubt as to the entire back-end database platform to run and lay a solid foundation; Batis allow free allocation XMLQUERY, these XMLQUERY in some cases can be reused, while the results it can be mapped to Java objects, using the framework can simplify application development ; to display personalized consider the use of XML, XSLT transformation where, as a descriptive message each have corresponding template, XML schema, so XSLT can transform XML schema and structure of individual shows; when the user needs have been described Add a new description of the information, you can use BeanUtils get from the interface description of a number of new information added to the JiBX generated by the dynamic JavaBean in the JavaBean can be obtained for other modules to use, or use the marshall JiBX mapping function to XML and stored in the database.

Figure 5. System Architecture






Convenience, this taking the "Edit descriptions" the business process as a case, combined with specific procedures designed to provide readers a clearer understanding of the flexibility of the solution.

First, the user reads the descriptions, and editing. This step requires the sql Map iBatis configuration file to configure the appropriate sql statement and execute, and then will be the descriptions of XML with XSLT personalize the display.

1. Configure sql:

Listing 1. sql Map of the configuration in iBatis

Parameters and query results of which will be mapped to pre-defined types of Java Bean: Description, implementation of the Java Bean is a database table DISCRIPTION the field to a simple Java object property mapping, the specific content of the list 2:

Listing 2. POJO class

/ * Mapping from table DISCRIPTION * /

public class Description (

private int id;

private String info;

public int getId () (

return id;

)

public void setId (int id) (

this.id = id;

)

public String getInfo () (

return info;

)

public void setInfo (String info) (

this.info = info;

)

)

2. Implementation of the query:

Listing 3. Check the description specified

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import com.ibm.db.Database;

public String selectInfoByID (Serializable id) (

try (

reader = Resources.getResourceAsReader (resource);

SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader);

Description param = new Description ();

Description result = new Description ();

param.setId ((Integer) id). intValue ());

result = (Description) sqlMap.queryForObject ("selectInfo", param);

if (result! = null) (

return result.getInfo ();

)

else

return null;

)

catch (Exception e) (

e.printStackTrace ();

)

return null;

)

3. Using XSLT for display:

As the XML document is read out from the meeting a predetermined definition of the template (the template customization technology, refer to the References resources [1]), so, just follow pre-defined template structure Zhan Shi on it, Of course, developing who can model their own characteristics, the preparation of different XSLT transformation style, making a more personal show, given the following XSLT is the most common form:

Listing 4. XSLT

xmlns: xsl = "http://www.w3.org/1999/XSL/Transform"

xmlns: xsd = "http://www.w3.org/2001/XMLSch

Then, the user can display the page description of the information, to edit the descriptions, assuming he chose to add some descriptions and save, then platform can use BeanUtils to the description of newly added dynamically to the original descriptions corresponding Java Bean, its premise is the need to read out the original XML document into Java Bean in the form of JiBX binding framework used here, because here is the dynamically generated Java Bean, in order to achieve this kind of dynamic binding, using the pureXML to manage the JiBX binding documents, namely: a description of each message corresponds to a bound document in the XML document required form and format conversion between Java Bean, and in order to maintain this mapping, when one changes any form, requires the binding simultaneously to change the corresponding file and save to the database. With pureXML support, the management mechanism is very convenient to achieve, given the length of the causes of this article is no longer discussed.

Next, using JiBX will read out the description of the previous XML document into Java Bean, and use BeanUtils to the user to edit the properties of dynamically added to this Java Bean to other modules in use.

4. JiBX will be converted to XML using Java Bean:

Suppose read from the database such as XML document in Listing 5:

Listing 5. Query the database by the XML

Gone With the Wind

Eleven

200MB

It''s so go

Corresponding JiBX binding documents such as Listing 6:

Listing 6 JiBX binding document

This function can be achieved JiBX of Unmarshal Java Bean from XML to the automatic conversion of specific conversion processes such as Listing 7:

Listing 7. The XML document is converted to Java Bean

public Content getClassFromXML (String info) (

try (

InputStream xmlStream = new InputStream (info);

IBindingFactory bfact = BindingDirectory.getFactory (Content.class);

IUnmarshallingContext uctx = bfact.createUnmarshallingContext ();

/ * Unmarshal description infomation selected from database * /

Object bean = uctx.unmarshalDocument (xmlStream, null);

return (Content) bean;

)

catch (JiBXException e) (

e.printStackTrace ();

return null;

)

)

According to JiBX mapping mechanism, the converted Java Bean should have a list of 8 in the form of:

Listing 8. Conversion by the Java Bean JiBX

public class Content (

public String name;

public String section;

public String size;

public String discription;

public String getDiscription () (

return discription;

)

public void setDiscription (String discription) (

this.discription = discription;

)

public String getName () (

return name;

)

public void setName (String name) (

this.name = name;

)

public String getSection () (

return section;

)

public void setSection (String section) (

this.section = section;

)

public String getSize () (

return size;

)

public void setSize (String size) (

this.size = size;

)

public void setSize (String size) (

this.size = size;

)

)

Of course, JiBX mapping mechanism is very flexible, and it not only supports these elements and attributes of one to one mapping, Huan support some map, and the mapping itself great flexibility, developers can 璁捐 based on actual needs.

5. Dynamically add attributes:

Add a description of the user of new information generated dynamically added to the Java Bean, then add the dynamic processes such as the list of 9:

Listing 9. BeanUtils dynamically add attributes

public Content addAttribute (Content content,

HashMap attributeNames, HashMap attributeValues) throws Exception (

try (

BeanUtils.setProperty (content,

(String) attributeNames.get ("URL"), attributeValues.get ("URL"));

BeanUtils.setProperty (content,

(String) attributeNames.get ("Role"), attributeValues.get ("Role"));

) Catch (InvocationTargetException e) (

e.printStackTrace ();

)

return content;

)

Then add the property to be dynamic after the new Java Bean, can follow the demand, using JiBX the Marshal mechanism to convert XML documents stored in pureXML, or for other mold

Summary

The emergence of DB2 pureXML for XML storage and use provide a good support. This paper focuses on how pureXML the popular combination of tools and frameworks, and presents a development model. With pureXML storage flexibility, iBatis data access flexibility, BeanUtils and JiBX dynamic class generation, and XSLT to bring the interface to show flexibility to achieve a high degree of flexibility in the application framework purposes.







相关链接:



VOB To SWF



Easy to use File Compression



best MPEG4 avc to wmv



Photoimpact6 One Big Impression On The Text Easy To Do Special Effects (2)



FLV TO Zune



3GP To MPEG



Wonderful Books Excerpts (6): "Didi" One Instance Of Production



All RAIDERS Athena Sword



Second-hand computers Xi'an China Merchants Plaza, more than 60 illegal businesses operating wit



21st Century Business Herald: reversal eBay



No Trace Of Her It Just So To Find The Original 1000 Baidu



Zhou Hongyi 1 billion investment is not money, is lonely



Best Cataloging



CHARACTERISTICS of GIS



No comments:

Post a Comment