Result Maps and Automatic Mapping

Harry · 11 Sep 2026 · 9 views

Automatic Mapping Helps

When the column names match or simply map from snake_case to camelCase, MyBatis fills the object for you.

<settings>
  <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

SELECT customer_id, full_name FROM customers
-- maps to Customer.customerId and Customer.fullName

Explicit resultMap

For needed control use an explicit resultMap:

<resultMap id="customerMap" type="Customer">
  <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="email" column="email"/>
  <result property="city" column="hometown"/>  <!-- rename a column -->
</resultMap>

<select id="findAll" resultMap="customerMap">
  SELECT id, name, email, city AS hometown FROM customers
</select>

Nested Columns With <association>

<resultMap id="orderMap" type="Order">
  <id property="id" column="order_id"/>
  <result property="total" column="total"/>
  <association property="customer" javaType="Customer"
    columnPrefix="cust_">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
  </association>
</resultMap>

Key Points

  • Sensible defaults map most columns automatically.
  • resultMap handles renames and nested objects.
  • <association> populates a single nested object.
  • mapUnderscoreToCamelCase removes the need for most maps.
Share this post:

Comments (0)

Please login or register to comment.