Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Wake Up to iBATIS, the Hibernate Alternative for Spring : Page 3

As great as Hibernate is, a lesser-known Java persistence solution called iBATIS actually may be a superior technology for your Spring development in certain situations. Find out which.


advertisement

Configuring the Environment

Before you can dive into a full-fledged example, you must lay some groundwork. Configuring your environment to work with Spring and iBATIS is a little complicated initially (although much easier than a Spring/Hibernate configuration), but once you've completed your first application with the framework, it is really quite simple.

First, you need to get the iBATIS library files. The entire iBATIS framework can be downloaded from the iBATIS homepage. The ibatis-common-2.jar and ibatis-sqlmap-2.jar archives should be copied and made available at runtime via the application's classpath.

When working with Spring and iBATIS, you will have a minimum of three XML configuration files, and you will often have even more. It is important that you become familiar with these config files, as they are the key to the flexibility and loose coupling provided by this architecture.



Next, look at both the standalone and Web-based configuration sets.

Standalone Configuration (Three or more XML files)
You should place the two iBATIS JAR files (ibatis-common-2.jar and ibatis-sqlmap-2.jar) in the application's classpath (via environment variable, command-line parameter, or JRE ext directory).

At minimum, you must define and place the following three configuration files in the classpath:

  • Spring config (applicationContext.xml)—This file defines the database connection parameters, the location of the SQL Map config file, and one or more Spring beans for use within the application. For the example application, you will want to define at least one DAO bean.
  • SQL Map config (sqlMapConfig.xml)—This file defines any iBATIS-specific configuration settings that you may need and declares the location for any SQL Map files that should be accessible through this config file (which in turn is tied to a particular DAO in the Spring config file).
  • SQL Map(s) (domainObject.xml)—One or more SQL Map files are declared in the SQL Map config and typically mapped to a single business entity within the application (often represented by a single Java class). The file may, in fact, map the business entity to one or more domain entities (tables, views, etc.) within the database.

Web-based Configuration (Four or more XML files)
You should place the two iBATIS JAR files (ibatis-common-2.jar and ibatis-sqlmap-2.jar) in the application's classpath, either in the Web app's lib directory or in the server's classpath (/common/lib, /shared/lib, or equivalent).

At minimum, you must define and place the following four configuration files accordingly within the Web app:

  • Spring config (see above for details)—Place this file in the WEB-INF directory.
  • SQL Map config (see above for details)—Place this file in the WEB-INF directory.
  • SQL Map(s) (see above for details)—Place these files in a sub-directory under the WEB-INF/classes directory.
  • Web config (web.xml)—This is your standard Web configuration file. In addition to the typical configuration settings, you will need to define two elements to Spring-enable your application:
    1. Define a context parameter (contextConfigLocation) that points to the Spring config file.
    2. Define a Spring servlet of type org.springframework.web.context.ContextLoaderServlet and set it to load on startup.

Now that you know the basic configuration steps, you just need to look at a working example with API calls to see how everything pulls together.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap