Hint
This article is generated by AI translation.
ObjectMapping
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.
entity tag
定义和使用 resultMap
<entity id="user_resultMap" table="users" type="com.example.dto.UserBean">
<id column="id" property="id"/>
<mapping column="name" property="name"/>
<mapping column="age" property="age"/>
<mapping column="create_time" property="createTime"/>
</entity>
<select id="queryById" resultMap="user_resultMap">
select
id, name, age, create_time
from
users
where
id = #{id}
</select>
Attributes
entity tag*
| Property | Description |
|---|---|
| id | 可选 Identifier; if empty, type is used. |
| catalog | 可选 Catalog of the mapped table. |
| schema | 可选 Schema of the mapped table. |
| table | 可选 Table name. |
| 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. |
| useDelimited | 可选 Force quoting/delimiters on table/column names when generating SQL. Default false. |
| character-set | 可选 Table character set (available via TableDescription). |
| collation | 可选 Table collation (TableDescription). |
| comment | 可选 Table comment (TableDescription). |
| other | 可选 Other table options (TableDescription). |
| ddlAuto | 可选 Auto DDL strategy: none, create, add, update, create-drop (auto DDL not yet supported). |
id tag and mapping 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. |
| keyType | 可选 For Fluent API only. Key generation strategy when the value is null. See key generator. |
| insert | 可选 For Fluent API only. Whether it participates in insert. See write policy. |
| update | 可选 For Fluent API only. Whether it participates in update. See write policy. |
| selectTemplate | 可选 For Fluent API only. Column expression when used in SELECT. Default empty (column name). See templates. |
| insertTemplate | 可选 For Fluent API only. Argument expression for INSERT. Default ?. See templates. |
| setValueTemplate | 可选 For Fluent API only. Argument expression in UPDATE SET. Default ?. See templates. |
| whereColTemplate | 可选 For Fluent API only. Column expression in WHERE for update/delete. Default empty (column name). See templates. |
| whereValueTemplate | 可选 For Fluent API only. Argument expression in WHERE for update/delete. Default ?. See templates. |
| orderByColTemplate | 可选 For Fluent API only. Column expression in ORDER BY. Default empty (column name). See templates. |
| sqlType | 可选 Column data type (TableDescription). |
| length | 可选 Column length (TableDescription). |
| precision | 可选 Numeric precision (TableDescription). |
| scale | 可选 Numeric scale (TableDescription). |
| character-set | 可选 Column character set (TableDescription). |
| collation | 可选 Column collation (TableDescription). |
| nullable | 可选 Whether the column allows NULL; for <id> this is always true and cannot be set (TableDescription). |
| default | 可选 Column default value (TableDescription). |
| comment | 可选 Column comment (TableDescription). |
| other | 可选 Other column options (TableDescription). |