Spring-iBATIS-Nexaweb

Spring-iBATIS-Nexaweb

Using Spring and iBATIS in a Nexaweb Java Application

This document describes the sample Nexaweb Java application with an embedded database and provides general guidelines on how to use an embedded database in a Nexaweb Java application. This sample Nexaweb Java application demonstrates Create, Read, Update, Delete (CRUD) functions of a database embedded through the use of the Spring Framework and iBATIS.

About Spring Framework

The Spring Framework allows you to manage applications declaratively using an Iversion of Control (IoC) design pattern. An IoC design pattern allows you to manage applications through the use of configuration files. Spring enables you to build applications using Plain Old Java Objects (POJOs), containing only your business logic. Then IoC moves the responsibility for making things happen into the framework, and away from application code. Whereas your code calls a traditional class library, an IoC framework calls your code. Click Spring Framework for more information on it.

About iBATIS

The iBATIS Data Mapper framework provides Object Relational Mapping (ORM) which couples objects with stored procedures or SQL statements using an XML descriptor. SQLMaps provide a simple framework to provide much of the Java Database Connection (JDBC) functionality required without your having to write this code repetitively throughout the application.

To use the SQLMaps framework, you create a XML file that lists all of the SQL queries that you wish to execute through your application. For each SQL query, you specify with which Java class the query will exchange parameters and ResultSets.

Inside of your Java code, when you want to execute a particular query, you create an object to pass query parameters and necessary conditions, and then pass this object and name of the query to be executed to SQLMaps. Once the query is executed, SQLMaps will create an instance of the class you have specified to receive query results, and populate it with values from the ResultSet returned by the database.

In addition, iBATIS Data Access Objects (DAOs) provides an abstraction layer that hides the details of your persistence solution and provides a common API to the rest of your application. DAOs allow you to create simple components that provide access to your data without revealing the specifics of the implementation to the rest of your application. Using DAOs, you can dynamically configure your application to use different persistence mechanisms.  For Java users, the DAOs framework is bundled as part of the iBATIS Database Layer, which includes the SQL Maps Framework. However, the DAO Framework can be used without SQL Maps.

About the Sample Application

This application performs CRUD functionality with an emdedded database.

Application Architecture

The sample application uses the Spring Framework and iBATIS to create an application architecture with an embedded database. The application uses interfaces for the database service (DBService.java) and the DAO (ContactDAO.java) that allows you to implement these components using a range of technologies. In addition, the Spring controller configuration file (dbSampleController-servlet.xml) allows the application to be controlled using an IoC design pattern.

Libraries

This application requires the following libraries located in the application's WebContent/WEB-INF/lib directory:

Library

Description

Where to Obtain

commons-collections-2.1.1.jar

Apache commons for collection handling.

For all Apache Commons libraries either:

  • Use JARs distributed with Spring
    (in $SPRING_HOME/lib/jakarta-commons)

  • Download latest version from Apache Commons.

commons-dbcp-1.2.1.jar

Apache commons library for DB.

 

commons-logging-1.0.4.jar

Apache commons for logging.

 

commons-pool-1.3.jar

Apache commons for DB connection pooling.

 

derby.jar

JavaDB embedded database.

Either:

ibatis-2.3.3.720.jar

iBATIS library 

Apache iBATIS

spring-webmvc.jar

Spring WebMVC, needed for MultiActionController.

For all Spring libraries:
Spring

spring.jar

Core Spring classes library.
 

 

 

Files

Figure 1: Database Sample Application Architecture

The application uses the files shown in Figure 1 and described in the following table:

File

Description

Location in Application Project

web.xml

J2EE configuration file.

Creates the hooks to load Spring framework. dbSampleController-servlet provides the Spring DispatcherServlet.

WebContent/WEB-INF/

applicationContext.xml

Spring configuration/bootstrapping file.

WebContent/WEB-INF/

dbSampleController-servlet.xml

The web configuration file for Spring.

  • Specifies the URL mapping to the controller(MultiActionController) class controller.ds. Maps controller.ds to DBSampleController, which has MethodNameResolver configured 'action' as the URL parameter to send the method name.

  • Creates instances of:

    - The service class(JavaDBServiceImpl)

    - DataAccessObject (DAO) class(IbatisContactDAO), declares the datasource to the embedded JavaDB instance, the iBATIS adapter(sqlMap)
     

  • Injects reference of:

    - The iBATIS adapter into the DAO

    - The DAO object into the Service class

    - The Service class into the controller.

WebContent/WEB-INF/

SqlMapConfig.xml

Bootstraps and loads the iBATIS mappings.

WebContent/WEB-INF/

Contact.xml

Contains the CRUD SQL commands to interact with the DB.

src/com/nexaweb/samples/
db/dao/ibatis/

IbatisContactDAO.java

Utilizes the Contact.xml file to interact with iBATIS, in turn, to interact with the embedded JavaDB instance.

src/com/nexaweb/samples/
db/dao/

JavaDBServiceImpl.java

This constitutes the service layer and calls the DAO methods to manage data from the DB.

src/com/nexaweb/samples/
db/service/

DBSampleController.java

This is the primary point of contact from the client side. The controller requests the service layer (JavaDBServiceImpl.java) to get/manage data.

src/com/nexaweb/samples/
db/controller/

ServerSideUtils.java

Server-side utility class to send and receive objects to and from the client, respectively.

src/com/nexaweb/services/
server/util/

DBSampleMco.java

The client side management object that manages the event handling in the application and communication with the server.

src-client/com/nexaweb/samples/
db/mco/

McoUtils.java

Client-side utility class to send and receives object to and from the server, respectively.

src-client/com/nexaweb/samples/
db/util/

Contact.java

The data class representing a Contact.

src-shared/com/nexaweb/samples/
db/domain/

java-index.xal

A single UI files where the application elements are declared.

WebContent/

 

Application Flow

Figure 2: Database Sample Application Process Flow

Step

Description

1

UI submits DB action (CRUD).

2

DBSampleMCO.java creates contact.java object to hold data, passes it to MCOUtils.java for HTTP request encapsulation.

3

DBSampleMCO.java sends HTTP request to ServerSideUtils.java on the server.

4

ServerSideUtils.java unloads HTTP packet and passes contact.java object to DBSampleController.java.

5

DBSampleController.java passes contact.java to JavaDBServiceImpl.java.

6

JavaDBServiceImpl.java.passes contact.java to IBATISContactDAO.java.

7

IBATISContactDAO.java gets appropriate SQL statement for this action from Contact.XML.

8

IBATISContactDAO.java sends SQL statement to DB. Receives response from DB and sends it in Contact.java object back through server components to client. 

 

Links

Browse Subversion (browse)