- MyBatis
-
MyBatis Developer(s) The MyBatis team Stable release 3.0.6 / October 9, 2011 Development status Active Written in Java and .NET Operating system Cross-platform Type Persistence Framework License Apache License 2.0 Website http://www.mybatis.org MyBatis is a persistence framework available for Java and .NET that couples objects with stored procedures or SQL statements using an XML descriptor or annotations.
MyBatis is free software that is distributed under the Apache License 2.0.
MyBatis was formerly known as iBATIS.
Contents
Feature Summary
Unlike ORM frameworks MyBatis does not try to map an object model to a relational database, but instead maps SQL statements to objects and methods. This makes MyBatis an easy to use tool and also a good choice for legacy or denormalized databases or just to have full control of SQL execution.
MyBatis makes database access code much easier than for JDBC. Usually one line of code is all that is needed to execute a SQL statement. This saves time and prevents common mistakes like leaving a connection opened, coding a wrong data mapping, exceeding the limits of a result set or getting more than one result when just one was expected.
The main feature of MyBatis is its simplicity because it is based on plain objects, XML and SQL.
MyBatis not only maps beans to database data but can also map SQL statements to interface methods (known as mappers). This, combined with a dependency injection framework, allows to build business code free of dependencies and even without any call to MyBatis API. For this purpose, MyBatis integrates with Spring Framework and Google Guice.
MyBatis supports declarative data caching. That means that it is possible to enable caching of one or more statements adding a setting to the corresponding XML mapping file. This way MyBatis will try to get the results from a cache before hitting the database. MyBatis integrates with: OSCache, EHcache and Hazelcast and supports custom integration with other cache tools.
Getting Started
SQL statements are stored in XML files or annotations. The following sample shows the definition of a select statement that returns a "Blog" bean using XML.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.mybatis.example.BlogMapper"> <select id="selectBlog" parameterType="int" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper>
This sentence is executed as follows.
Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
Mapper interfaces are a new MyBatis feature. They help to avoid the use of literal strings and enable IDE code completion.
Mappers are like DAOs (Data Access Objects) but unlike them, that consist on an interface and at least one implementation, mappers are just interfaces with one method for each statement. No implementation is needed because MyBatis will create a dynamic proxy at runtime for each mapper.
BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = mapper.selectBlog(101);
For more detail, please refer to the User Guide available at MyBatis site. See external links.
MyBatis Generator
MyBatis provides a code generator. MyBatis Generator will introspect a database table (or many tables) and generate MyBatis artifacts needed to perform CRUD operations (Create, Retrieve, Update, Delete).
It will preserve any custom code in case of regeneration.
An Eclipse plugin is available.
Spring Integration
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 a mapper up and injects it into a "BlogService" bean.
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface" value="org.mybatis.example.BlogMapper" /> </bean> <bean id="blogService" class="org.mybatis.example.BlogServiceImpl"> <property name="blogMapper" ref="blogMapper" /> </bean>
Calling MyBatis is now just calling a bean:
public class BlogServiceImpl implements BlogService { private BlogMapper blogMapper; public void setBlogMapper(BlogMapper blogMapper) { this.blogMapper = blogMapper; } public void doSomethingWithABlog(int blogId) { Blog blog = blogMapper.selectBlog(blogId); ... } }
Google Guice Integration
MyBatis integrates with Google Guice. This module provides a set of Modules and Providers that will build MyBatis mappers and sessions and will inject them into other beans. It also adds transactional support based on @Transactional annotation.
Building a MyBatis module.
Injector injector = Guice.createInjector( new MyBatisModule.Builder() .addMapperClasses(org.mybatis.example.BlogMapper.class) .create();
Injecting mappers:
@Singleton public class BlogServiceImpl implements BlogService { @Inject private BlogMapper blogMapper; @Transactional public void doSomethingWithABlog(int blogId) { Blog blog = mapper.selectBlog(blogId); ... } }
MyBatis Migrations
MyBatis Migrations is a Java command line tool that keeps track of database schema changes managing DDL files (known as migrations).
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
In 2001 a project called iBATIS was started by Clinton Begin. Originally the focus was on the development of cryptographic software solutions. The first product to be released by iBATIS was Secrets [1], a personal data encryption and signing tool much like PGP. Secrets was written entirely in Java and was released under an open source license.
That year Microsoft published a paper [2] to demonstrate that its recent .NET 1.0 framework was more productive than Java. For that purpose Microsoft built its own version of Sun's Web "Pet Store", a Web project that Sun had used to show Java best practices (Java BluePrints). Microsoft claimed that .NET was 10 times faster and 4 times more productive than Java.
In 2002 Clinton developed an application called JPetStore[3] to demonstrate that Java could be more productive than .NET and could also do so while achieving a better architecture than was used in the Microsoft implementation.
JPetStore 1.0 had a big impact [4] and the database layer that Clinton used attracted the attention of the community. Soon, iBATIS Database Layer 1.0 project started, composed by two components: iBATIS DAO and iBATIS SQL Maps.
iBATIS 2.0 was released in June 2004 [5]. It was a complete redesign while keeping the same features. Clinton donated the iBATIS name and code to Apache Software Foundation and the project stayed in the ASF for six years.
Eventually iBATIS DAO was deprecated, considering that better dao frameworks was available like Spring Framework.
On May 19, 2010 iBATIS 3.0 was published and simultaneously the development team decided to continue the development of the framework at Google Code [6]. Given the iBATIS name was donated to the ASF the project acquired its new name MyBatis. It was the first time a TLP (Top Level Project) moved from Apache.
MyBatis 3.0 [7] is a complete redesign with some clear objectives: test driven development, code cleanliness over performance, simple design over flexible design, one JAR file, no required 3rd party dependencies and better plug-in support.
See also
- Hibernate
- JDBC
- Java Persistence API
- EclipseLink
- Apache Cayenne
- pureQuery
- NHydrate
- OpenJPA
- Spring Framework
- Google Guice
- O/R Broker, similar framework for Scala
References
External links
Categories:- Object-relational mapping
- Persistence frameworks
Wikimedia Foundation. 2010.