MyBatis

from Wikipedia, the free encyclopedia
MyBatis
Basic data

developer Apache Software Foundation
Current  version 3.4.6
( March 11, 2018 )
operating system cross-platform
programming language Java and .NET
category Persistence - Framework
License Apache License 2.0
mybatis.org

Mybatis (formerly iBATIS composed of "internet" and English abatis , barricade ') is an open-source - Persistence - Framework for Java and .NET . A ported version called RBatis is available for Ruby and Ruby on Rails .

The main task of MyBatis is to separate the database access code from the rest of the application code. For this purpose, data access objects (DAOs for short) are made available to the application and the SQL statements are stored in XML files, so-called SQL maps. This decouples the assignment of tables to classes from the business logic.

MyBatis is not an Object Relational Mapping framework (ORM for short), the mapping between object-oriented classes and relational tables must be done by the developers themselves, as well as the writing of the SQL statements for the queries. The automatic creation of a database schema from the class hierarchy is therefore also not possible.

history

ibatis logo

The iBATIS project was founded by Clinton Begin in 2001. The original aim of the project was the development of cryptographic software, which is also responsible for the part "batis" in the project name ("batis" stands for "abatis" - English for " Verau ", a military defense system). The first of Mybatis finished software was "Secrets", an open-source - Encryption - and Signierungstool in Java.

In early 2002, a Microsoft article appeared that claimed .NET was 10 times faster and 4 times more productive than J2EE. This prompted the iBATIS project to write the sample application "JPetStore" (first version July 1, 2002) and thus refute the claims of the article. The persistence layer used, the SQL maps and data access objects, attracted the attention of the open source community. This resulted in the iBATIS framework, which simply combines the two parts.

During 2010 iBATIS moved from the Apache Software Foundation to Google Code. This move was justified with the availability of new technologies in the field of social networks , version management and open source infrastructure. IBATIS was also renamed MyBatis. The project is still under the Apache license .

Today MyBatis is only the persistence framework, still consisting of the two main components SQL maps and data access objects. JPetStore acts as the official example of the typical use of MyBatis.

The framework is currently available in different versions for the programming languages ​​Java and .NET. There is a port for Ruby called RBatis. The jBati project, an ORM mapper for JavaScript, is inspired by MyBatis. The Apache project iBator offers a tool for MyBatis - it generates the iBATIS mapping files from databases.

Functionality

The main functionality of MyBatis is the separation of the database access code from the rest of the application code. The most important components for this are the data access objects, via which the application communicates with the persistence layer, as well as the SQL maps, which decouple the database access.

Like most persistence frameworks, MyBatis offers a number of functions that go beyond pure persistence. These are z. B. support for transactions , both local and global (i.e. cross-database) via JTA and various performance optimizations such as lazy loading , join fetching or caching .

MyBatis generator

MyBatis includes a code generator "MyBatis Generator". MyBatis Generator queries the database tables and generates "MyBatis artifacts" with which CRUD operations (Create, Retrieve, Update, Delete) can be carried out.

An Eclipse plugin is available.

example

Let there be a table CUSTOMER that was created with the following SQL statement:

CREATE TABLE KUNDE (
  K_ID   INTEGER NOT NULL PRIMARY KEY,
  K_NAME VARCHAR(128),
  K_STR  VARCHAR(128),
  K_ORT  VARCHAR(128),
  K_PLZ  INTEGER)

The following POJO also exists :

package beispiel;

public class Kunde {
  private int id;
  private String name;
  private String ort;
  private String strasse;
  private int plz;

  //Getter- und Setter-Methoden folgen
}

To be able to execute a query, an XML descriptor file must be available:

  <select id="getKunde" parameterClass="java.lang.Integer" resultClass="beispiel.Kunde">
    SELECT K_ID AS id,
      K_NAME AS name,
      K_STR AS strasse,
      K_ORT AS ort,
      K_PLZ AS plz
    FROM KUNDE
    WHERE K_ID = #value#
  </select>

The specification #value#refers to the integer value that must be given in the query. An object or a map can also be used as a parameter, whereby their attributes can also #be integrated using the -notation.

The query is written in Java as follows:

  SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(Resources.getResourceAsReader("path-to-my-sqlMapConfig.xml"));
  Kunde kunde = (Kunde) sqlMap.queryForObject("getKunde", 1);
  System.out.println(kunde);

Without MyBatis you would have to write the following code to functionally do the same in Java:

   Connection conn = null;
   PreparedStatement ps = null;
   ResultSet rs = null;
   String sql = "SELECT K_ID AS id, K_NAME AS name, K_STR AS strasse, K_ORT AS ort, K_PLZ AS plz FROM KUNDE WHERE K_ID = ?";
   Kunde kunde;
   conn = dataSource.getConnection();
   ps = conn.preparedStatement(sql);
   ps.setInt(1, id);
   rs = ps.executeQuery();
   rs.next();
   kunde = new Kunde();
   kunde.setId(rs.getInt("id"));
   kunde.setName(rs.getString("name"));
   kunde.setStrasse(rs.getString("strasse"));
   kunde.setOrt(rs.getString("ort"));
   kunde.setPlz(rs.getString("plz"));
   rs.close();
   conn.close();
   System.out.println(kunde);

This code would not only be significantly longer, more complex, more error-prone and more difficult to maintain, but would also be less efficient, since none of the performance optimizations built into MyBatis, such as connection pooling , have yet been implemented.

See also

literature

Web links

Individual evidence

  1. RDoc documentation for RBatis
  2. Clinton Begin - Implementing the Microsoft® .NET Pet Shop using Java (PDF; 220 kB)
  3. Announcement to switch to Google Code on the iBATIS homepage
  4. iBATIS history on the iBATIS website
  5. JBati homepage
  6. Ibator page on the iBATIS site
  7. MyBatis Generator