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.

Nexaweb

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:

LibraryDescriptionWhere to Obtain
commons-collections-2.1.1.jarApache 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.jarApache commons library for DB. 
commons-logging-1.0.4.jarApache commons for logging. 
commons-pool-1.3.jarApache commons for DB connection pooling. 
derby.jarJavaDB embedded database.Either:
ibatis-2.3.3.720.jariBATIS library Apache iBATIS
spring-webmvc.jarSpring WebMVC, needed for MultiActionController.For all Spring libraries:
Spring
spring.jarCore Spring classes library.
 
 

 

Files

Nexaweb

Figure 1: Database Sample Application Architecture

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

FileDescriptionLocation in Application Project
web.xmlJ2EE configuration file.

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

WebContent/WEB-INF/
applicationContext.xmlSpring configuration/bootstrapping file.WebContent/WEB-INF/
dbSampleController-servlet.xmlThe 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.xmlBootstraps and loads the iBATIS mappings.WebContent/WEB-INF/
Contact.xmlContains the CRUD SQL commands to interact with the DB.src/com/nexaweb/samples/
db/dao/ibatis/
IbatisContactDAO.javaUtilizes the Contact.xml file to interact with iBATIS, in turn, to interact with the embedded JavaDB instance.src/com/nexaweb/samples/
db/dao/
JavaDBServiceImpl.javaThis constitutes the service layer and calls the DAO methods to manage data from the DB.src/com/nexaweb/samples/
db/service/
DBSampleController.javaThis 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.javaServer-side utility class to send and receive objects to and from the client, respectively.src/com/nexaweb/services/
server/util/
DBSampleMco.javaThe 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.javaClient-side utility class to send and receives object to and from the server, respectively.src-client/com/nexaweb/samples/
db/util/
Contact.javaThe data class representing a Contact.src-client/com/nexaweb/samples/
db/domain/
java-index.xalA single UI files where the application elements are declared.WebContent/

 

Application Flow

Nexaweb

Figure 2: Database Sample Application Process Flow

StepDescription
1UI submits DB action (CRUD).
2DBSampleMCO.java creates contact.java object to hold data, passes it to MCOUtils.java for HTTP request encapsulation.
3DBSampleMCO.java sends HTTP request to ServerSideUtils.java on the server.
4ServerSideUtils.java unloads HTTP packet and passes contact.java object to DBSampleController.java.
5DBSampleController.java passes contact.java to JavaDBServiceImpl.java.
6JavaDBServiceImpl.java.passes contact.java to IBATISContactDAO.java.
7IBATISContactDAO.java gets appropriate SQL statement for this action from Contact.XML.
8IBATISContactDAO.java sends SQL statement to DB. Receives response from DB and sends it in Contact.java object back through server components to client. 

 

Browse Subversion (browse)