MyBatis
Encyclopedia
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
.
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.
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
.
This sentence is executed as follows.
Mapper interfaces are a new MyBatis feature. They help to avoid the use of literal strings and enable IDE
code completion.
Mappers are like DAO
s (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.
For more detail, please refer to the User Guide available at MyBatis site. See external links.
It will preserve any custom code in case of regeneration.
An Eclipse
plugin is available.
The following sample shows a basic xml configuration that sets a mapper up and injects it into a "BlogService" bean.
Calling MyBatis is now just calling a bean:
. 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.
Injecting mappers:
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.
was Secrets , 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 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 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 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 . 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
. 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 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.
Application framework
In computer programming, an application framework consists of a software framework used by software developers to implement the standard structure of an application for a specific development environment ....
available for Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
and .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
that couples objects with stored procedures or SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
statements using an XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
descriptor or annotations.
MyBatis is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...
that is distributed under the Apache License
Apache License
The Apache License is a copyfree free software license authored by the Apache Software Foundation . The Apache License requires preservation of the copyright notice and disclaimer....
2.0.
MyBatis was formerly known as iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
.
Feature Summary
Unlike ORMObject-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...
frameworks MyBatis does not try to map an object model to a relational database
Database
A database is an organized collection of data for one or more purposes, usually in digital form. The data are typically organized to model relevant aspects of reality , in a way that supports processes requiring this information...
, but instead maps SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
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
SQL
SQL is a programming language designed for managing data in relational database management systems ....
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
SQL
SQL is a programming language designed for managing data in relational database management systems ....
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
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
and SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
.
MyBatis not only maps beans to database data but can also map SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
statements to interface methods (known as mappers). This, combined with a dependency injection
Dependency injection
Dependency injection is a design pattern in object-oriented computer programming whose purpose is to improve testability of, and simplify deployment of components in very large software systems....
framework
Framework
Framework may refer to:*Software framework, a reusable set of libraries or classes for a software system .**Application framework, a software framework used to implement the standard structure of an application for a specific operating system....
, 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
Google Guice
Google Guice is an open source software framework for the Java platform released by Google under an Apache license. It provides support for dependency injection using annotations to configure Java objects...
.
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
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
mapping file. This way MyBatis will try to get the results from a cache
Cache
In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere...
before hitting the database. MyBatis integrates with: OSCache
OSCache
OSCache is "a Java framework" developed by OpenSymphony that makes it easy to cache content in Web applications.According to OpenSymphony, OSCache...
, EHcache
EHcache
Ehcache is a widely used open source Java distributed cache for general purpose caching, Java EE and light-weight containers. It features memory and disk stores, replicate by copy and invalidate, listeners, cache loaders, cache extensions, cache exception handlers, a gzip caching servlet filter,...
and Hazelcast and supports custom integration with other cache
Cache
In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere...
tools.
Getting Started
SQLSQL
SQL is a programming language designed for managing data in relational database management systems ....
statements are stored in XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
files or annotations. The following sample shows the definition of a select statement that returns a "Blog" bean using XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
.
This sentence is executed as follows.
Mapper interfaces are a new MyBatis feature. They help to avoid the use of literal strings and enable IDE
Integrated development environment
An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development...
code completion.
Mappers are like DAO
DAO
DAO may refer to:* D-amino acid oxidase, a peroxisomal enzyme.* Data access object, a design pattern used in object-oriented software engineering* De-asphalted oil, a crude oil refinery process stream...
s (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.
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
Eclipse
An eclipse is an astronomical event that occurs when an astronomical object is temporarily obscured, either by passing into the shadow of another body or by having another body pass between it and the viewer...
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.
Calling MyBatis is now just calling a bean:
Google Guice Integration
MyBatis integrates with Google GuiceGoogle Guice
Google Guice is an open source software framework for the Java platform released by Google under an Apache license. It provides support for dependency injection using annotations to configure Java objects...
. 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.
Injecting mappers:
MyBatis Migrations
MyBatis Migrations is a JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
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 iBATISIBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
was Secrets , 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
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
published a paper to demonstrate that its recent .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
1.0 framework was more productive than Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
. 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
Java BluePrints
Java BluePrints is Sun Microsystems' best practices for Enterprise Java development. This is Sun's official programming model for Java Platform, Enterprise Edition Software Development Kit . It began with Java Pet Store, the original reference application for the Java EE platform...
). Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
claimed that .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
was 10 times faster and 4 times more productive than Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
.
In 2002 Clinton developed an application called JPetStore to demonstrate that Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
could be more productive than .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
and could also do so while achieving a better architecture than was used in the Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
implementation.
JPetStore 1.0 had a big impact 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
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
DAO and iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
SQL Maps.
iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
2.0 was released in June 2004 . It was a complete redesign while keeping the same features. Clinton donated the iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
name and code to Apache Software Foundation
Apache Software Foundation
The Apache Software Foundation is a non-profit corporation to support Apache software projects, including the Apache HTTP Server. The ASF was formed from the Apache Group and incorporated in Delaware, U.S., in June 1999.The Apache Software Foundation is a decentralized community of developers...
and the project stayed in the ASF for six years.
Eventually iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
DAO was deprecated, considering that better dao frameworks was available like Spring Framework.
On May 19, 2010 iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
3.0 was published and simultaneously the development team decided to continue the development of the framework at Google Code
Google Code
Google Code is Google's site for developer tools, APIs and technical resources. The site contains documentation on using Google developer tools and APIs—including discussion groups and blogs for developers using Google's developer products....
. Given the iBATIS
IBATIS
iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs . The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files...
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 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
- HibernateHibernate (Java)Hibernate is an object-relational mapping library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database...
- JDBC
- Java Persistence APIJava Persistence APIThe Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
- EclipseLinkEclipseLinkEclipseLink is the open source Eclipse Persistence Services Project from the Eclipse Foundation. The software provides an extensible framework that allows Java developers to interact with various data services, including databases, web services, Object XML mapping , and Enterprise Information Systems...
- Apache CayenneApache CayenneApache Cayenne is an open source persistence framework licensed under the Apache License, providing object-relational mapping and remoting services. Cayenne binds one or more database schemas directly to Java objects, managing atomic commit and rollbacks, SQL generation, joins, sequences, and more...
- pureQuery
- NHydrateNhydratenHydrate is an object-relational mapping solution for the Microsoft .NET platform providing a framework for a relational database to be mapped to .NET objects...
- OpenJPA
- Spring Framework
- Google GuiceGoogle GuiceGoogle Guice is an open source software framework for the Java platform released by Google under an Apache license. It provides support for dependency injection using annotations to configure Java objects...
- O/R Broker, similar framework for Scala