Saturday, 26 January 2013

MyBatis is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files.

MyBatis comes with the following design philosophies:

  • Simplicity: MyBatis is widely regarded as being one of the simplest persistence frameworks available today.
  • Fast Development: MyBatis's philosophy is to do all it can to facilitate hyper-fast development.
  • Portability: MyBatis can be implemented for nearly any language or platform like Java, Ruby, and C# for Microsoft .NET.
  • Independent Interfaces: MyBatis provides database-independent interfaces and APIs that help the rest of the application remain independent of any persistence-related resources.
  • Open source: MyBatis is free and an open source software.



This class can be instantiated, used and thrown away. There is no need to keep it around once you've created your SqlSessionFactory. Therefore the best scope for instances of SqlSessionFactoryBuilder is method scope (i.e. a local method variable). You can reuse the SqlSessionFactoryBuilder to build multiple SqlSessionFactory instances, but it's still best not to keep it around to ensure that all of the XML parsing resources are freed up for more important things.


Once created, the SqlSessionFactory should exist for the duration of your application execution. There should be little or no reason to ever dispose of it or recreate it. It's a best practice to not rebuild the SqlSessionFactory multiple times in an application run. Doing so should be considered a “bad smell”. Therefore the best scope of SqlSessionFactory is application scope. This can be achieved a number of ways. The simplest is to use a Singleton pattern or Static Singleton pattern.


Each thread should have its own instance of SqlSession. Instances of SqlSession are not to be shared and are not thread safe. Therefore the best scope is request or method scope. Never keep references to a SqlSession instance in a static field or even an instance field of a class. Never keep references to a SqlSession in any sort of managed scope, such as HttpSession of of the Servlet framework. If you're using a web framework of any sort, consider the SqlSession to follow a similar scope to that of an HTTP request. In other words, upon receiving an HTTP request, you can open a SqlSession, then upon returning the response, you can close it. Closing the session is very important. You should always ensure that it's closed within a finally block. The following is the standard pattern for ensuring that SqlSessions are closed:

SqlSession session = sqlSessionFactory.openSession();
try {
 // do work
} finally {

Using this pattern consistently throughout your code will ensure that all database resources are properly closed.

Mapper Instances

Mappers are interfaces that you create to bind to your mapped statements. Instances of the mapper interfaces are acquired from the SqlSession. As such, technically the broadest scope of any mapper instance is the same as the SqlSession from which they were requested. However, the best scope for mapper instances is method scope. That is, they should be requested within the method that they are used, and then be discarded. They do not need to be closed explicitly. While it's not a problem to keep them around throughout a request, similar to the SqlSession, you might find that managing too many resources at this level will quickly get out of hand. Keep it simple, keep Mappers in the method scope. The following example demonstrates this practice.

SqlSession session = sqlSessionFactory.openSession();
try {
 BlogMapper mapper = session.getMapper(BlogMapper.class);
 // do work
} finally {

Advantages of MyBatis

Here are few advantages of using MyBatis:

  • Supports Stored procedures: MyBatis encapsulates SQL in the form of stored procedures so that business logic is kept out of the database, and the application is easier to deploy and test, and is more portable.
  • Supports Inline SQL: No precompiler is needed, and you have full access to all of the features of SQL.
  • Supports Dynamic SQL: MyBatis provides features for dynamically building SQL queries based on parameters.
  • Supports ORM: MyBatis supports many of the same features as an O/RM tool, such as lazy loading, join fetching, caching, runtime code generation, and inheritance

MyBatis vs Hibernate

There are major differences between MyBatis and Hibernate but both the solutions work well, given their specific domain. Personally I would suggest you should use MyBatis if:
  • You want to create your own SQL's and are willing to maintain them.
  • your environment is driven by relational data model.
  • you have to work existing and complex schema's.
And simply use Hibernate if:
  • Your environment is driven by object model and wants generates SQL automatically.
To count there are few differences:
  • MyBatis is:    
    • Simpler
    • Faster development time
    • Flexible
    • Much smaller in package size

  • Hibernate:
    • Generates SQL for you which means you don't spend time on SQL
    • Provides much more advance cache
    • Highly scalable
Other difference is that MyBatis makes use of SQL which could be database dependent whereas Hibernate makes use of HQL which is relatively independent of databases and it is easier to change db in Hibernate.

Hibernate maps your Java POJO objects to the Database tables where as MyBatis maps the ResultSet from JDBC API to your POJO Objects.

If you are using stored procedures, well you can do it in Hibernate but it is little difficult in comparison of MyBatis. As an alternative solution MyBatis maps results sets to objects, so no need to care about table structures. This works very well for stored procedures, works very well for reporting applications, etc

Finally, Hibernate and MyBatis both are open source Object Relational Mapping(ORM) tools available in the industry. Use of each of these tools depends on the context you are using them. Hibernate and MyBatis both also have good support from SPRING framework so it should not be a problem to chose one of them.


Post a Comment

Find me on Facebook! Follow me on Twitter!