Unlike ORM frameworks, MyBatis does not map Java objects to databasetables but Java methods to SQL statements. MyBatis lets you use all your database functionality like stored procedures, views, queries of any complexity and vendor proprietary features. It is often a good choice for legacy or de-normalized databases or to obtain full control of SQL execution. It simplifies coding compared to JDBC. SQL statements are executed with a single line. MyBatis provides a mapping engine that maps SQL results to object trees in a declarative way. SQL statements can be built dynamically by using a built-in language with XML-like syntax or with Apache Velocity using the Velocity integration plugin. MyBatis integrates with Spring Framework and Google Guice. This feature allows one to build business code free of dependencies. MyBatis supports declarative data caching. A statement can be marked as cacheable so any data retrieved from the database will be stored in a cache and future executions of that statement will retrieve the cached data instead hitting the database. MyBatis provides a default cache implementation based on a Java HashMap and default connectors for integrating with: OSCache, Ehcache, Hazelcast and Memcached. It provides an API to plug other cache implementations.
Usage
SQL statements are stored in XML files or annotations. Below depicts a MyBatis mapper, that consists of a Java interface with some MyBatis annotations: package org.mybatis.example; public interface BlogMapper
The sentence is executed as follows. BlogMapper mapper = session.getMapper; Blog blog = mapper.selectBlog;
SQL statements and mappings can also be externalized to an XML file as follows.
Statements can also be executed using the MyBatis API. Blog blog = session.selectOne;
MyBatis integrates with Spring Framework. This module allows MyBatis to participate in Spring transactions. It will also build MyBatis mappers and sessions and inject them into other beans. The following sample shows a basic XML configuration that sets up a mapper and injects it into a "BlogService" bean.
Calling MyBatis is now just calling a bean: public class BlogServiceImpl implements BlogService
Velocity language
The Velocity language driver lets you use Apache Velocity to generate your dynamic SQL querieson the fly.
MyBatis Generator
MyBatis provides a code generator. MyBatis Generator will introspect a database table and generate MyBatis artifacts needed to perform CRUD operations. An Eclipse plugin is available. It will preserve any custom code in case of regeneration but only if you use the Eclipse plugin.
MyBatis Migrations
MyBatis Migrations is a Java command line tool that keeps track of database schema changes managing DDL files. Migrations allows to query the current status of the database, apply schema changes and also undo them. It also helps to detect and solve concurrent database schema changes made by different developers.
History
MyBatis project is a subsidiary of iBATIS 3.0 and maintained by a team which includes the original creators of iBATIS. The project was created on May 19, 2010 when Apache iBATIS 3.0 was published and the team announced that the development will continue under a new name and a new home at Google Code.