When to Use resultType vs resultMap in MyBatis? A Practical Guide

This article explains the differences between MyBatis resultType and resultMap, outlines their common points, and provides clear usage scenarios with examples and images to help Java developers choose the appropriate mapping strategy.

Programmer DD
Programmer DD
Programmer DD
When to Use resultType vs resultMap in MyBatis? A Practical Guide

1. Overview

MyBatis is a semi‑automatic ORM framework that Java developers should master. Many newcomers are confused about the difference between resultType and resultMap , which this article clarifies.

2. resultType and resultMap

2.1 resultType

Specifies the fully qualified class name or alias of the expected return type of a select statement. If the query returns a collection, set the element type, not the collection itself. You may use resultType or resultMap, but not both.

2.2 resultMap

Also defines the expected return type, with the same restriction: it cannot be used together with resultType.

2.3 Common points

Both tags describe how MyBatis maps the result set of a select statement. They cannot coexist in the same select element.

2.4 Usage scenarios

Use resultType for simple POJOs where column names exactly match field names. MyBatis internally creates a resultMap for you. Use resultMap for complex mappings such as one‑to‑one or one‑to‑many relationships, nested objects, or when you need to map columns to different property names.

Example of a simple resultType usage:

Example of a resultMap for a complex DTO:

Corresponding mapping configuration:

ResultMap can also inherit from other mappings, but be mindful of performance; avoid excessive nesting and enable lazy loading ( lazyLoadingEnabled) when appropriate.

3. Summary

The article compares the similarities and differences between resultType and resultMap, outlining when each should be used. For detailed syntax and advanced features, refer to the official MyBatis documentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaMyBatisORMresultMapresultType
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.