Skip to main content
Hint

This article is generated by AI translation.

ObjectMapping

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.

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*

PropertyDescription
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., createTimecreate_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

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.
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).