SQL Zone is brought to you in partnership with:

Loiane Groner, Brazilian, works as a Java/ Sencha evangelist. She has 7+ years of experience in web development. She is the ESJUG (Espirito Santo Java Users Group) and CampinasJUG (Campinas Java Users Group) leader and coordinator. Loiane is passionate about technology and programming. Also author of ExtJS 4 First Look book. Loiane is a DZone MVB and is not an employee of DZone and has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

Introduction to iBatis (MyBatis), An alternative to Hibernate and JDBC

02.09.2011
| 18638 views |
  • submit to reddit

I started to write a new article series about iBatis/MyBatis. This is the first article and it will walk you through what is iBatis/MyBatis and why you should use it.

For those who does not know iBatis/MyBatis yet, it is a persistence framework – an alternative to JDBC and Hibernate, available for Java and .NET platforms. I’ve been working with it for almost two years, and I am enjoying it!

The first thing you may notice in this and following articles about iBatis/MyBatis is that I am using both iBatis and Mybatis terms. Why? Until June 2010, iBatis was under Apache license and since then, the framework founders decided to move it to Google Code and they renamed it to MyBatis. The framework is still the same though,  it just has a different name now.

I gathered some resources, so I am just going to quote them:

What is MyBatis/iBatis?

The MyBatis data mapper framework makes it easier to use a relational database with object-oriented applications. MyBatis couples objects with stored procedures or SQL statements using a XML descriptor. Simplicity is the biggest advantage of the MyBatis data mapper over object relational mapping tools.To use the MyBatis data mapper, you rely on your own objects, XML, and SQL. There is little to learn that you don’t already know. With the MyBatis Data Mapper, you have the full power of both SQL and stored procedures at your fingertips.
(www.mybatis.org)

iBATIS is based on the idea that there is value in relational databases and SQL, and that it is a good idea to embrace the industrywide investment in SQL. We have experiences whereby the database and even the SQL itself have outlived the application source code, and even multiple versions of the source code. In some cases we have seen that an application was rewritten in a different language, but the SQL and database remained largely unchanged.

It is for such reasons that iBATIS does not attempt to hide SQL or avoid SQL. It is a persistence layer framework that instead embraces SQL by making it easier to work with and easier to integrate into modern object-oriented software. These days, there are rumors that databases and SQL threaten our object models, but that does not have to be the case. iBATIS can help to ensure that it is not.

(iBatis in Action book)

So…

What is iBatis ?

 

  • A JDBC Framework
  • Developers write SQL, iBATIS executes it using JDBC.
  • No more try/catch/finally/try/catch.
  • An SQL Mapper
  • Automatically maps object properties to prepared statement parameters.
  • Automatically maps result sets to objects.
  • Support for getting rid of N+1 queries.
  • A Transaction Manager
  • iBATIS will provide transaction management for database operations if no other transaction manager is available.
  • iBATIS will use external transaction management (Spring, EJB CMT, etc.) if available.
  • Great integration with Spring, but can also be used without Spring (the Spring folks were early supporters of iBATIS).

What isn’t iBATIS ?

  • An ORM
  • Does not generate SQL
  • Does not have a proprietary query language
  • Does not know about object identity
  • Does not transparently persist objects
  • Does not build an object cache

Essentially, iBatis is a very lightweight persistence solution that gives you most of the semantics of an O/R Mapping toolkit, without all the drama. In other words ,iBATIS strives to ease the development of data-driven applications by abstracting the low-level details involved in database communication (loading a database driver, obtaining and managing connections, managing transaction semantics, etc.), as well as providing higher-level ORM capabilities (automated and configurable mapping of objects to SQL calls, data type conversion management, support for static queries as well as dynamic queries based upon an object’s state, mapping of complex joins to complex object graphs, etc.). iBATIS simply maps JavaBeans to SQL statements using a very simple XML descriptor. Simplicity is the key advantage of iBATIS over other frameworks and object relational mapping tools.(http://www.developersbook.com )

Who is using iBatis/MyBatis?

See the list in this link: http://www.apachebookstore.com/confluence/oss/pages/viewpage.action?pageId=25

I think the biggest case is MySpace, with millions of users. Very nice!

This was just an introduction, so in next articles I will show how to create an application using iBatis/Mybatis – step-by-step.

Enjoy! :)

From http://loianegroner.com/2011/02/introduction-to-ibatis-mybatis-an-alternative-to-hibernate-and-jdbc/

Published at DZone with permission of Loiane Groner, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Josh Marotti replied on Wed, 2011/02/09 - 11:31am

I have been meaning to learn iBatis, and you have a great intro.  Just dying to see some code to look at.  Hopefully your next article is coming out soon??

Jacek Furmankiewicz replied on Wed, 2011/02/09 - 2:36pm

The amount of XML config required by mybatis is what always pushed me away from it. Embedding all of this CDATA in XML tags is just painful. It's time they discovered YAML.

Balázs Bessenyei replied on Thu, 2011/02/10 - 1:36am

Not necessarily, it is possible to write the SQL-s without CDATA tags. Mostly replacing the "<" ">" with their appropriate XML escaped variants. Would be enough to go CDATA free in most cases.

Also the latest version supports @Annotations based and programatic SQL definition/generation as well.

Clinton Begin replied on Thu, 2011/02/10 - 10:41am

Thanks for the article Loiane.

Some clarification:

- MyBatis is still under an Apache 2.0 license, we are just no longer an ASF project, preferring self management within Google Code, as you pointed out.

- MySpace specifically uses MyBatis.NET, which we are very proud of. But it's a different team of people responsible for the .NET port, so I want to ensure they get the credit for that.

@Jacek: I'm an experienced Rails developer and have been heavily influenced by it (see MyBatis Schema Migrations). I love YAML too, but for MyBatis SQL Maps, I don't see a huge improvement vs. XML -- for every CDATA section you avoid, you'll get swallowed up by an incorrect space or leading special character. Furthermore, you can use Annotations for all but the most complex maps. I wish you could use annotations for everything, but Java Annotations aren't powerful enough (C# Attributes are). If you're adventurous, try Groovy with MyBatis -- which supports multiline strings and a host of other powerful text processing features that come in handy when working with SQL statements (MyBatis is also known to work with Scala). A specific Groovy API for MyBatis is what I would love to see. We also have some helper classes if you want to build your SQL dynamically with Java code (but they aren't as powerful as the Dynamic SQL XML tags. Barring all of that, MyBatis has a pretty straightforward configuration API that you can use to create your own mapping configuration -- even a YAML one if you want.

In addition to all of this, we have support for Guice and Spring as well, to simplify integration with your favorite DI container. Check out JPetStore 6 for an example of using various features of MyBatis 3.

Edvin Syse replied on Thu, 2011/02/10 - 11:29am in response to: Jacek Furmankiewicz

Conceptually I would agree with you, but I've been using iBATIS for eight years, and I can tell you that you very seldom need to write CDATA blocks, even for complex statements. If this is really your reason for not trying it, you have been missing out for no good reason :) IDE XML editing also has much better support than YAML afaik, and a good IDE makes editing statements really enjoyable.

Loiane Groner replied on Thu, 2011/02/10 - 2:57pm

Thanks guys! The next article will be published next Monday on my blog. I'm writing a complete tutorial, with lots of sample code. I use iBatis 2.5 on my work, and I decided to try Mybatis 3, and there is some things that are different now.

Thomas Kern replied on Thu, 2012/09/06 - 10:48am

Thanks for this introduction. I want to learn more about this framework. I followed your examples from your repository on GitHub and learned the basic stuff. I still try to figure out how to integrate this framework with Spring MVC for ExtJS web application.

http://www.java-tips.org 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.