Skip to main content
Hint

This article is generated by AI translation.

ResultMapping

Use either <resultMap> or <entity> to map query results:

How to choose between resultMap and entity?
  • For the same table with different query shapes, entity is good when columns all come from one table; resultMap when columns span multiple tables.
  • Using the Fluent API requires entity or object-mapping annotations.
  • If the SELECT uses aliases, functions, CASE, or columns from multiple tables, resultMap is safer.

##resultMap tag

using resultMap
<resultMap id="user_resultMap" type="com.example.dto.UserBean">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="create_time" property="createTime"/>
</resultMap>

<select id="queryById" resultMap="user_resultMap">
select
id, name, age, create_time
from
users
where
id = #{id}
</select>

Attributes

resultMap tag

PropertyDescription
id可选 Identifier; if empty, type is used.
type必选 Fully qualified Java type to map to.
caseInsensitive可选 Case-insensitive mapping for column/property names. Default true.
autoMapping可选 Whether to perform auto-mapping. Default true.
mapUnderscoreToCamelCase可选 With auto-mapping, convert camelCase property names to snake_case columns (e.g., createTimecreate_time). Default false.

result tag

PropertyDescription
column必选 Column name in the result set.
property必选 Property name in the target type.
javaType可选 Usually inferred; specify when the property is abstract or an interface.
jdbcType可选 Overrides the default Java↔JDBC mapping.
typeHandler可选 Override the type handler to use.