Introduction
MyBatis is a persistence framework that couples SQL statements with Java objects through XML descriptors or annotations. Unlike full ORM solutions, MyBatis lets developers write their own SQL while automating the mapping of result sets to POJOs and the setting of parameters.
What MyBatis Does
- Maps SQL statements to Java interface methods via XML or annotations
- Automatically maps result sets to Java objects using column-to-property conventions or explicit mappings
- Supports dynamic SQL construction with if, choose, foreach, and trim tags
- Provides first-class support for stored procedures and batch operations
- Integrates with Spring Boot via mybatis-spring-boot-starter for zero-configuration setup
Architecture Overview
MyBatis centers on the SqlSessionFactory, built from XML or Java configuration, which produces SqlSession instances for executing mapped statements. Each mapped statement is parsed into a MappedStatement object with its SQL source, parameter mappings, and result maps. The executor layer handles caching (local session cache and optional second-level cache) and batching, while TypeHandlers convert between JDBC types and Java types.
Self-Hosting & Configuration
- Add via Maven: org.mybatis:mybatis:3.5.x or use mybatis-spring-boot-starter for Spring Boot
- Define mappers as XML files or annotated Java interfaces
- Configure data source and transaction manager in mybatis-config.xml or application.yml
- Enable the second-level cache per mapper namespace for read-heavy workloads
- Use MyBatis Generator (MBG) to auto-generate mappers from existing database schemas
Key Features
- Full SQL control with no hidden query generation or magic behind the scenes
- Dynamic SQL tags eliminate string concatenation and reduce injection risk
- Pluggable architecture with interceptors for auditing, pagination, and custom logic
- Association and collection mappings handle one-to-one and one-to-many relationships
- Lazy loading support for deferred fetching of associated objects
Comparison with Similar Tools
- Hibernate/JPA — full ORM with automatic schema management; MyBatis gives more SQL control at the cost of manual mapping
- jOOQ — type-safe SQL DSL in Java; better compile-time safety but requires code generation
- Spring JDBC Template — lightweight SQL execution; lacks MyBatis's result mapping and dynamic SQL
- JDBI — fluent SQL API for Java; simpler but less feature-rich for complex mappings
FAQ
Q: When should I use MyBatis over Hibernate? A: Choose MyBatis when you need fine-grained control over SQL queries, work with legacy databases, or prefer writing SQL directly.
Q: Does MyBatis prevent SQL injection? A: Yes, when using #{} parameter syntax. Avoid ${} for user input as it performs direct string substitution.
Q: Can MyBatis work with Spring Boot? A: Yes. The mybatis-spring-boot-starter provides auto-configuration for DataSource, SqlSessionFactory, and mapper scanning.
Q: Does MyBatis support Kotlin? A: Yes. MyBatis works with Kotlin data classes and has community extensions for Kotlin DSL mappers.