Hint
This article is generated by AI translation.
ResultMapping
Use either <resultMap> or <entity> to map query results:
- <resultMap>: map SQL result sets.
- <entity>: map a concrete table.
How to choose between resultMap and entity?
- For the same table with different query shapes,
entityis good when columns all come from one table;resultMapwhen columns span multiple tables. - Using the Fluent API requires
entityor object-mapping annotations. - If the SELECT uses aliases, functions, CASE, or columns from multiple tables,
resultMapis 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
| Property | Description |
|---|---|
| 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., createTime → create_time). Default false. |
result tag
| Property | Description |
|---|---|
| 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. |