MyBatis Articles
15 articles about MyBatis. Explore Spring Boot implementation, design, and operations across related topics.
-
How to Unit Test Mappers in Spring Boot + MyBatis - Why @MybatisTest Doesn't Exist and Alternative Configurations with H2/Testcontainers
MyBatis Mapper has no dedicated slice test equivalent to @DataJpaTest. This article explains why, based on the coupling with SqlSession, and compares four patterns—minimal @SpringBootTest configuration, lightweight Java Config, H2, and Testcontainers—with a comparison table and working code.
-
How to Implement a Second-Level Cache with @CacheNamespace in Spring Boot + MyBatis - cache Tag and Redis Integration
This article explains how to use MyBatis's built-in second-level cache mechanism to reduce DB load for read-heavy Mappers. It covers the basic behavior of the cache tag and PerpetualCache, defining configuration via Java Config with @CacheNamespace, automatic flushing on INSERT/UPDATE, sharing caches across multiple Mappers with @CacheNamespaceRef, and the steps to swap in a custom Cache implementation backed by Redis, along with configuration examples in Spring Boot.
-
Spring Boot + MyBatis Polymorphic Mapping with discriminator - resultMap Implementation to Branch a Single Table by Type
This article explains how to use MyBatis's discriminator tag and case elements to map multiple types (such as notification types or payment methods) mixed within a single table or JOIN result to different DTOs/subclasses using only resultMap, without any branching logic on the Java side.
-
How to Implement Optimistic Locking in Spring Boot + MyBatis
Explains how to build optimistic locking in MyBatis equivalent to JPA's @Version. Covers WHERE clause design for UPDATE statements using a version column, conflict detection via zero affected rows, exception classes extending Spring's standard exceptions, and retry strategies aware of transaction boundaries, with code examples using both Mapper XML and annotations.
-
Spring Boot + MyBatis: How to Build Dynamic SQL with Java Code Only Using SqlProvider
For those tired of MyBatis XML dynamic SQL, this article explains how to build dynamic SQL using only Java code with @SelectProvider/@InsertProvider/@UpdateProvider. It covers implementation examples using SqlBuilder and string concatenation, passing multiple arguments, common pitfalls, and criteria for choosing between this approach and the XML method, all from a practical standpoint.
-
How to Retrieve the Auto-Generated ID (AUTO_INCREMENT) After INSERT with Spring Boot + MyBatis
This article explains how to reflect the AUTO_INCREMENT/IDENTITY value generated by the DB after an INSERT in MyBatis back into your Java entity, covering both the annotation-based approach and the XML-based approach. It also introduces an alternative implementation using selectKey, and how to diagnose the typical causes when the ID comes back as 0/null.
-
How to Reuse SQL in Spring Boot + MyBatis Using sql and include Tags - Eliminating Query Duplication Across Mappers
Are you copy-pasting the same WHERE conditions or SELECT column lists across multiple Mapper XML files? This article explains how to define SQL fragments with MyBatis's sql tag and reuse them within the same Mapper or across different Mapper XML files using the include tag, how to pass parameters with property, and how to combine this with dynamic SQL tags like foreach.
-
What is MyBatis? Understanding the Differences from JPA and a Minimal Spring Boot Setup
This article organizes what kind of O/R mapper MyBatis is by comparing its philosophy with JPA/Hibernate, and explains the criteria for choosing between them along with a minimal setup using mybatis-spring-boot-starter.
-
How to Implement Transaction Management with @Transactional in Spring Boot + MyBatis - The Mechanism of SqlSession and DataSourceTransactionManager
MyBatis has no persistence context, so @Transactional only controls the commit/rollback timing of SqlSession. This article verifies with working code the thread binding of SqlSessionTemplate, visualization through logs, combined use with JdbcTemplate, and the pitfall of checked exceptions.
-
Spring Boot + MyBatis: Causes and Fixes for "Invalid bound statement (not found)" and "Parameter 'xxx' not found"
A step-by-step guide to diagnosing and fixing BindingException (Invalid bound statement (not found) / Parameter 'name' not found) and missing Mapper bean errors in Spring Boot 3.x + MyBatis, working through mapper-locations, namespace, @Param, -parameters, and @MapperScan in order.
-
Dynamic SQL in Spring Boot + MyBatis: How to Write where, set, trim, choose, bind, and foreach, and How to Fix "Leading AND Left Behind", "Trailing Comma", and "Empty IN Clause Crashes"
A walkthrough of Spring Boot + MyBatis dynamic SQL tags (where/set/trim/choose/bind/foreach), showing the XML you write side by side with the SQL actually issued. Covers a search Mapper that doesn't break even when every condition is empty, partial updates that never leave a trailing comma, and defensive patterns so foreach doesn't fail when given an empty list or null, organized as fixes by error type.
-
How to Map One-to-Many and Many-to-One Join Results with Spring Boot + MyBatis - resultMap association/collection and N+1 Countermeasures
Explains how to load parent-child table JOIN results into hierarchical DTOs with Spring Boot 3 + MyBatis. Covers resultMap association (many-to-one) and collection (one-to-many), avoiding column collisions with columnPrefix, a comparison of the number of SQL statements issued by nested select vs. nested resultMap (measured N+1), and the pitfall of mismatched row counts when combined with paging along with workarounds, with copy-paste-ready code in both XML and annotation styles.
-
How to Implement Pagination with Spring Boot + MyBatis - Choosing Between PageHelper, RowBounds, and Hand-Written LIMIT/OFFSET
Compares three approaches to paginating list APIs in Spring Boot + MyBatis (hand-written LIMIT/OFFSET, RowBounds, and PageHelper) in terms of generated SQL, count retrieval, sorting, and extra dependencies. Explains why RowBounds results in in-memory pagination, the thread-local pitfalls of PageHelper and its helperDialect setting, and how to convert to Spring Data's Pageable, all with ready-to-run code.
-
How to Use MyBatis with Spring Boot - Implementation Guide for Mapper Definitions, Dynamic SQL, and Result Mapping
An implementation guide for integrating MyBatis into Spring Boot from scratch. Covers adding the mybatis-spring-boot-starter dependency, defining Mappers using both annotation and XML approaches, dynamic SQL with if and foreach tags, result mapping for joins using resultMap, the safety of #{} versus ${}, and @Transactional integration, all explained with working code.
-
MyBatis vs JPA in Spring Boot: Selection Criteria and Combined Usage Patterns [With Comparison Table]
Not sure whether to choose MyBatis or JPA for your Spring Boot project? This article organizes a comparison table covering SQL control flexibility, learning cost, performance, and maintainability, along with a practical decision flowchart, the difference between #{} and ${}, and concrete configuration examples for using both together. Get all the decision criteria in one place.