Object/Relation Mapping ORM

ORM stands for Object/Relation Mapping: an abbreviation for object-relation mapping. ORM completes the mapping between object-oriented programming language DAOs and relational databases. Once the ORM framework completes the mapping, programmers can leverage both the simplicity of object-oriented programming languages and the technical advantages of relational databases.

ORM wraps relational databases as object-oriented models, serving as an intermediate solution when object-oriented design languages and relational databases develop out of sync.

Differences Between Objects and Relational Databases

  • Objects: In object-oriented programming, objects typically contain properties and methods, representing real-world entities
  • Relational Databases: Data in databases is stored in tables, where rows represent data records and columns represent attributes

Basic ORM Working Principles

ORM maps object fields to relational database table fields through a mapping mechanism, converting object operations into SQL statement execution. Typically, ORM performs the following operations:

  • Create: Inserts an object into a database table
  • Read: Queries the database based on conditions and returns corresponding objects
  • Update: Modifies object properties and updates corresponding records in the database
  • Delete: Removes objects and deletes corresponding records from the database

ORM Mapping Relationships

  • One-to-One: One object corresponds to one row in a database table
  • One-to-Many: One object corresponds to multiple rows in the database
  • Many-to-Many: Multiple objects can have multiple relationships, typically requiring a join table

Common ORM Frameworks

  • Hibernate: A very popular ORM framework in the Java ecosystem
  • JPA (Java Persistence API): ORM specification defined in Java EE standards
  • MyBatis: Semi-automatic lightweight persistence layer framework allowing developers to write SQL statements
  • Entity Framework: ORM tool in the .NET framework
  • Django ORM: ORM for Django framework in Python
  • SQLAlchemy: Most popular ORM framework in Python

MyBatis Introduction

MyBatis is an excellent semi-automatic lightweight persistence layer framework based on ORM, supporting customized SQL, stored procedures, and advanced mappings. MyBatis avoids almost all JDBC code and manual parameter setting as well as result set retrieval.

MyBatis can use simple XML or annotations to configure and map native types, interfaces, and Java POJOs (Plain Old Java Objects) to database records.


MyBatis History

Origins and Early History (2002 - 2010)

MyBatis originated from a project called iBatis. The iBatis project can be traced back to 2002, initiated by Clinton Begin and other developers, initially as a lightweight JDBC framework to simplify interactions between Java programs and databases.

The goal of iBatis was to provide a simple way to map SQL queries to Java objects, reducing tedious JDBC code and providing more flexible database operations.

Renaming and Birth of MyBatis (2010)

In 2010, iBatis developers decided to perform a major refactoring and rebranding of the project. After several versions, the development team believed the framework had matured enough to rename it MyBatis.

MyBatis’s name comes from “My” (representing developer freedom and flexibility) and “Batis” (representing SQL statement mapping).

Release of MyBatis 3.x

  • Annotation support: Introduced annotation-based configuration
  • Enhanced dynamic SQL functionality
  • Better plugin support

MyBatis Popularity and Maturity (2011 - Present)

After the release of MyBatis 3.x, because it simplified Java-database interaction while allowing developers to write native SQL, MyBatis quickly gained popularity among Java developers.

MyBatis 4.x and Beyond (2015 - Present)

  • Java 8 support: MyBatis began fully supporting Java 8 features
  • More flexible mapping: Continued to enhance support for complex mappings
  • MyBatis Generator: Official code generation tool
  • MyBatis Plus: Community-developed enhanced version providing auto CRUD operations, pagination queries, etc.

MyBatis Advantages

High Flexibility with Native SQL Support

MyBatis allows developers to write native SQL in mapping files, unlike ORM frameworks that completely encapsulate SQL generation. This means developers can maintain high flexibility while controlling the execution logic of SQL queries.

Avoids ORM Performance Overhead

ORM frameworks (like Hibernate) typically introduce some performance overhead. MyBatis provides manual control over SQL writing, allowing developers to optimize queries at a finer granularity.

Dynamic SQL Support

MyBatis provides very powerful dynamic SQL functionality, generating SQL statements dynamically based on conditions. Through tags like <if>, <where>, <foreach>, <set>, you can flexibly construct SQL in XML configuration files.

Concise Mapping Mechanism

MyBatis provides a concise mapping approach, where developers only need to bind SQL queries to Java methods through XML or annotations.

Supports Complex Queries

MyBatis can easily support very complex SQL queries, including multi-table joins, subqueries, nested queries, etc.

Simplifies Database Transaction Management

MyBatis provides built-in transaction management support, and when combined with transaction management from frameworks like Spring, it can easily implement control over database transactions.

Cross-Database Support

MyBatis provides extensive database support and is compatible with most relational databases (such as MySQL, PostgreSQL, Oracle, SQL Server, etc.).