Data Access Articles
12 articles about Data Access. Explore Spring Boot implementation, design, and operations across related topics.
-
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.
-
How to Use @NamedEntityGraph in Spring Data JPA - Reusable Fetch Definitions and Choosing Between EntityGraph.Type
This article explains how to stop writing @EntityGraph(attributePaths=...) on every method and instead define it once on the entity using @NamedEntityGraph for reuse. It covers how to write attributeNodes/subgraphs, the difference between LOAD and FETCH, applying it to a Repository, and the criteria for choosing between this and the ad-hoc version, all with code examples.
-
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.
-
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 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.
-
How to Persist enums to a Database with Spring Boot + JPA - Choosing Between @Enumerated(EnumType.STRING) and AttributeConverter
A guide to persisting enum fields to database columns with Spring Boot + JPA. Covers the difference between @Enumerated(STRING/ORDINAL), the dangers of ORDINAL, implementing an AttributeConverter for custom code values, and criteria for choosing between the two approaches, with practical examples.
-
How to Retrieve DTOs Directly with Spring Data JPA Projections - Choosing Between Interface-Based and Class-Based Projections
Learn how to SELECT only the columns you need and project them directly into DTOs/interfaces with Spring Data JPA projections. Compares interface-based, class-based (constructor), and dynamic projections through implementations, covering differences in the generated SQL, selection criteria, and pitfalls when combined with @Query.
-
How to Execute Raw SQL with Spring Boot's JdbcTemplate - Using queryForObject, RowMapper, and Batch Updates
A hands-on, code-focused guide to safely writing raw SQL with Spring's standard JdbcTemplate, covering single-row and scalar retrieval with queryForObject, multi-row retrieval with RowMapper/BeanPropertyRowMapper, CRUD with update/batchUpdate, named parameter binding with NamedParameterJdbcTemplate, and integration with @Transactional.
-
How to Implement Soft Delete with Spring Boot + JPA - Choosing Between @SQLDelete, @SQLRestriction, and Filters
Learn how to implement soft delete transparently with Spring Boot 3.x + Hibernate 6.4. We compare @SQLDelete + @SQLRestriction (formerly @Where) with @SoftDelete and @FilterDef so you can choose the right approach quickly, and cover common pitfalls such as unique constraint conflicts, restore operations, and recording who performed the deletion.
-
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.