Why MyBatis Is Called a Semi‑ORM Mapping Tool
MyBatis, a widely used Java persistence framework, blends SQL mapping with traditional JDBC, requiring developers to write SQL manually, offering greater flexibility and performance than full ORM solutions like Hibernate, while providing only persistence operations without encapsulating complex business logic.
MyBatis is a popular Java persistence framework that combines SQL mapping with traditional JDBC operations. Because developers must still write the SQL statements themselves, it does not fit the definition of a full ORM and is therefore described as a “semi‑ORM” tool.
1. SQL Mapping and Custom SQL
MyBatis allows developers to define custom SQL in XML or annotation files while mapping results to Java objects. The framework does not generate SQL automatically; the SQL is written explicitly by the developer.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>The corresponding mapper interface looks like:
public interface UserMapper {
User selectUserById(int id);
}And the XML mapping file for the method is:
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectUserById" resultType="com.example.model.User">
SELECT id, name, email FROM users WHERE id = #{id}
</select>
</mapper>2. Persistence Only, No Business Logic
MyBatis focuses on mapping database rows to Java objects and handling CRUD operations. It does not provide built‑in transaction management, caching strategies, or other complex business‑logic features; developers must implement those aspects themselves, unlike full ORM frameworks that encapsulate them.
3. Flexibility and Performance Benefits
Because developers write the SQL, they can fine‑tune queries for specific database characteristics and achieve higher performance, especially for large‑scale data operations. For example, a complex join can be expressed directly:
<select id="selectUserAndOrders" resultMap="userOrderMap">
SELECT u.id, u.name, o.id AS order_id, o.total
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.id = #{userId}
</select>4. Differences from Full ORM Frameworks
Typical full ORM tools such as Hibernate automatically generate SQL, provide an abstract query language (HQL), and handle the complete mapping from tables to classes without developer‑written SQL. MyBatis, by contrast, requires explicit SQL, does not auto‑generate statements, and offers a flexible mapping approach that lets developers customize queries and updates.
In summary, MyBatis inherits some ORM advantages—simplified persistence and reduced boilerplate—while retaining full control over SQL, making it a “semi‑ORM” solution suited for projects that need both flexibility and performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
java1234
Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
