Skip to main content

Mapping Tables

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

When to Use entity​

ScenarioIs entity recommended?
Java type and database table map directlyRecommended
BaseMapper or LambdaTemplate needs table mappingRecommended
You want XML mapping instead of annotationsRecommended
Query result comes from JOINs or function columnsPrefer resultMap
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 Builder API requires entity or Object Mapping annotations.
  • If the SELECT uses aliases, functions, CASE, or columns from multiple tables, resultMap is safer.

entity tag​

Define and use entity
<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
idOptional Identifier; if empty, type is used.
catalogOptional Catalog of the mapped table.
schemaOptional Schema of the mapped table.
tableOptional Table name.
typeRequired Fully qualified Java type to map to.
caseInsensitiveOptional Case-insensitive mapping for column/property names. Default true.
autoMappingOptional Whether to perform auto-mapping. Default true.
mapUnderscoreToCamelCaseOptional With auto-mapping, convert camelCase property names to snake_case columns (e.g., createTime → create_time). Default false.
useDelimitedOptional Force quoting/delimiters on table/column names when generating SQL. Default false.
character-setOptional Table character set (available via TableDescription).
collationOptional Table collation (TableDescription).
commentOptional Table comment (TableDescription).
otherOptional Other table options (TableDescription).
ddlAutoOptional Auto DDL strategy: none, create, add, update, create-drop (auto DDL not yet supported).

id tag and mapping tag

PropertyDescription
columnRequired Column name in the result set.
propertyRequired Property name in the target type.
javaTypeOptional Usually inferred; specify when the property is abstract or an interface.
jdbcTypeOptional Overrides the default Java↔JDBC mapping.
typeHandlerOptional Override the type handler to use.
keyTypeOptional For Builder API only. Key generation strategy when the value is null. See key generator.
insertOptional For Builder API only. Whether it participates in insert. See write policy.
updateOptional For Builder API only. Whether it participates in update. See write policy.
selectTemplateOptional For Builder API only. Column expression when used in SELECT. Default empty (column name). See templates.
insertTemplateOptional For Builder API only. Argument expression for INSERT. Default ?. See templates.
setValueTemplateOptional For Builder API only. Argument expression in UPDATE SET. Default ?. See templates.
whereColTemplateOptional For Builder API only. Column expression in WHERE for update/delete. Default empty (column name). See templates.
whereValueTemplateOptional For Builder API only. Argument expression in WHERE for update/delete. Default ?. See templates.
orderByColTemplateOptional For Builder API only. Column expression in ORDER BY. Default empty (column name). See templates.
sqlTypeOptional Column data type (TableDescription).
lengthOptional Column length (TableDescription).
precisionOptional Numeric precision (TableDescription).
scaleOptional Numeric scale (TableDescription).
character-setOptional Column character set (TableDescription).
collationOptional Column collation (TableDescription).
nullableOptional Whether the column allows NULL; for <id> this is always false and cannot be set (TableDescription).
defaultOptional Column default value (TableDescription).
commentOptional Column comment (TableDescription).
otherOptional Other column options (TableDescription).