HOME

TheInfoList



OR:

MyBatis is a
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
persistence framework Persistence or Persist may refer to: Math and computers * Image persistence, in LCD monitors * Persistence (computer science), the characteristic of data that outlives the execution of the program that created it * Persistence of a number, a ma ...
that couples objects with
stored procedure A stored procedure (also termed prc, proc, storp, sproc, StoPro, StoredProc, StoreProc, sp, or SP) is a subroutine available to applications that access a relational database management system (RDBMS). Such procedures are stored in the database d ...
s or
SQL Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel") is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
statements using an
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
descriptor or annotations. MyBatis is
free software Free software, libre software, libreware sometimes known as freedom-respecting software is computer software distributed open-source license, under terms that allow users to run the software for any purpose as well as to study, change, distribut ...
that is distributed under the
Apache License The Apache License is a permissive free software license written by the Apache Software Foundation (ASF). It allows users to use the software for any purpose, to distribute it, to modify it, and to distribute modified versions of the software ...
2.0. MyBatis is a fork of
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 (Plain Old Java Objects). The mappings are decoupled from the application logic by ...
3.0 and is maintained by a team that includes the original creators of
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 (Plain Old Java Objects). The mappings are decoupled from the application logic by ...
.


Feature summary

Unlike ORM frameworks, MyBatis does not map
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
objects to
database In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
tables but Java methods to
SQL Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel") is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
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 Java Database Connectivity (JDBC) is an application programming interface (API) for the Java (programming language), Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java ...
. 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 Apache Velocity first released in April 2001, is a Java-based template engine that provides a template language to reference objects defined in Java code. It aims to ensure clean separation between the presentation tier and business tiers in a W ...
using the Velocity integration plugin. MyBatis integrates with Spring Framework and
Google Guice Google Guice (pronounced like "juice") is an open-source software framework for the Java platform developed by Bob Lee and Kevin Bourrillion at Google and released under the Apache License The Apache License is a permissive free software l ...
. 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 Ehcache ( ) is an open source Java distributed cache for general-purpose caching, Java EE and . Ehcache is available under an Apache open source license. Ehcache was developed by Greg Luck starting in 2003. In 2009, the project was purchased b ...
,
Hazelcast In computing, Hazelcast is a unified real-time data platform implemented in Java that combines a fast data store with stream processing. It is also the name of the company that develops the product. The Hazelcast company is funded by venture ca ...
and
Memcached Memcached (pronounced variously /mɛmkæʃˈdiː/ ''mem-cash-dee'' or /ˈmɛmkæʃt/ ''mem-cashed'') is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and object ...
. It provides an
API An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
to plug other cache implementations.


Usage

SQL statements are stored in
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
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(BlogMapper.class); Blog blog = mapper.selectBlog(101); 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("org.mybatis.example.BlogMapper.selectBlog", 101); For details, please refer to the User Guide available at MyBatis site. See external links.


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 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 queries on the fly.


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 In computer programming, create, read, update, and delete (CRUD) are the four basic operations (actions) of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing info ...
operations (Create, Retrieve, Update, Delete). An
Eclipse An eclipse is an astronomical event which occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ...
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 (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

MyBatis project is a subsidiary of
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 (Plain Old Java Objects). The mappings are decoupled from the application logic by ...
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. Bye Google Code welcome Github
/ref>


See also

*
Hibernate Hibernation is a state of minimal activity and metabolic reduction entered by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It is most ...
* Speedment *
Java Database Connectivity Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is ...
(JDBC) *
Java Persistence API Jakarta Persistence, also known as JPA (abbreviated from the former name Java Persistence API) is a Jakarta EE application programming interface specification that describes the management of relational data in enterprise Java applications. Per ...
*
EclipseLink EclipseLink 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, ...
* Ebean * jOOQ * Apache Cayenne * IBM PureQuery *nHydrate *
Apache OpenJPA OpenJPA is an open source software, open source implementation of the Java Persistence API specification. It is an object-relational mapping (ORM) solution for the Java (programming language), Java language, which simplifies storing objects in dat ...
* ActiveJPA


References


External links

*{{Official website Object–relational mapping Persistence frameworks Articles with example Java code