Skip to main content
Hint

This article is generated by AI translation.

Doc Elements

in mapper XML is built from these elements:

select tag

<select> equals @Query. For simple cases just write the SQL:

Example 1: simple
<select id="queryListByAge">
select * from users where age = #{age}
</select>
Example 2: map results via resultType
<select id="queryById" resultType="com.example.dto.UserBean">
select
id, name, age, create_time
from
users
where
id = #{id}
</select>

Attributes

PropertyDescription
id必选 Identifier for the statement.
statementType可选 How JDBC executes the query. Default PREPARED.
- STATEMENTjava.sql.Statement
- PREPAREDjava.sql.PreparedStatement
- CALLABLEjava.sql.CallableStatement
timeout可选 If >0 sets java.sql.Statement.setQueryTimeout(int) in seconds. Default -1.
fetchSize可选 If >0 sets java.sql.Statement.setFetchSize(int) as a hint for how many rows to fetch per round trip. Default 256.
resultMap可选 Reference a mapping defined by <resultMap> or <entity>. Use either resultMap or resultType, not both. If neither is set, the mapper method return type is used.
resultType可选 Fully qualified type or type alias for the result. For collections, use the element type, not the collection. Use either resultMap or resultType, not both. If neither is set, the mapper method return type is used.
resultSetType可选 Value passed as resultSetType when creating the statement.
Options: FORWARD_ONLY, SCROLL_INSENSITIVE, SCROLL_SENSITIVE, DEFAULT (default).
- With STATEMENT: maps to Connection.createStatement(int, int) first arg.
- With PREPARED: maps to Connection.prepareStatement(String, int, int) second arg.
- With CALLABLE: maps to Connection.prepareCall(String, int, int) second arg.
resultSetExtractor可选 Configure a ResultSetExtractor for result handling (ignored if bindOut is set).
resultRowCallback可选 Configure a RowCallbackHandler for result handling (ignored if bindOut is set).
resultRowMapper可选 Configure a RowMapper for result handling (ignored if bindOut is set).
resultTypeHandler可选 Configure a TypeHandler for result handling (ignored if bindOut is set).
bindOut可选 Bind output params when using Multiple Results or Stored Procedures. Requires mapper return type Map<String,Object>.

update and delete tags

<update> equals @Update; <delete> equals @Delete. Configure UPDATE / DELETE SQL.

<update id="updateAge">
update users set age = #{age} where id = #{id}
</update>

<delete id="deleteById">
delete from users where id = #{id}
</delete>

Attributes

PropertyDescription
id必选 Identifier for the statement.
statementType可选 JDBC execution style. Default PREPARED.
- STATEMENTjava.sql.Statement
- PREPAREDjava.sql.PreparedStatement
- CALLABLEjava.sql.CallableStatement
timeout可选 If >0 sets java.sql.Statement.setQueryTimeout(int) in seconds. Default -1.

insert tag

<insert> equals @Insert to configure INSERT.

<insert id="insertUser">
insert into users (
id, name, age, create_time
) values (
#{id}, #{name}, #{age}, #{createTime}
)
</insert>

Attributes

PropertyDescription
id必选 Identifier for the statement.
statementType可选 JDBC execution style. Default PREPARED.
- STATEMENTjava.sql.Statement
- PREPAREDjava.sql.PreparedStatement
- CALLABLEjava.sql.CallableStatement
timeout可选 If >0 sets java.sql.Statement.setQueryTimeout(int) in seconds. Default -1.
useGeneratedKeys可选 Whether to use JDBC generated keys. Ignored if SelectKeySql/@SelectKey is also configured.
keyProperty可选 Bean property to fill when useGeneratedKeys is true. Ignored if @SelectKey is configured.
keyColumn可选 Result-set column used when useGeneratedKeys is true. Ignored if @SelectKey is configured.

selectKey tag

<selectKey> works with <insert>, same as @SelectKeySql.

Use <selectKey> to fetch generated keys (or custom-generated keys) without an extra query when inserting.

<insert id="insertUser">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
SELECT CONCAT('1', CEILING(RAND() * 1000 + 1000))
</selectKey>
insert into `test_user` (
`id`, `name`, `age`, `create_time`
) values (
#{id}, #{name}, #{age}, #{createTime}
)
</insert>

Attributes

PropertyDescription
keyProperty必选 Bean property to receive the generated key.
keyColumn可选 Column name used to read the generated key.
order必选 BEFORE or AFTER: when to run the key SQL relative to the INSERT.
statementType可选 JDBC execution style. Default PREPARED.
- STATEMENTjava.sql.Statement
- PREPAREDjava.sql.PreparedStatement
- CALLABLEjava.sql.CallableStatement
timeout可选 If >0 sets java.sql.Statement.setQueryTimeout(int) in seconds. Default -1.
fetchSize可选 If >0 sets java.sql.Statement.setFetchSize(int) as a hint for how many rows to fetch per round trip. Default 256.
resultSetType可选 Value passed as resultSetType when creating the statement.
Options: FORWARD_ONLY, SCROLL_INSENSITIVE, SCROLL_SENSITIVE, DEFAULT (default).
- With STATEMENT: maps to Connection.createStatement(int, int) first arg.
- With PREPARED: maps to Connection.prepareStatement(String, int, int) second arg.
- With CALLABLE: maps to Connection.prepareCall(String, int, int) second arg.

execute tag

<execute> equals @Execute and runs arbitrary SQL.

<execute id="updateAge">
update users set age = #{age} where id = #{id}
</execute>

Attributes

PropertyDescription
id必选 Identifier for the statement.
statementType可选 JDBC execution style. Default PREPARED.
- STATEMENTjava.sql.Statement
- PREPAREDjava.sql.PreparedStatement
- CALLABLEjava.sql.CallableStatement
timeout可选 If >0 sets java.sql.Statement.setQueryTimeout(int) in seconds. Default -1.
bindOut可选 Filter output arguments; only the specified ones are accepted. If set, mapper return type must be Map<String,Object>.

sql tag

Define SQL fragments and reuse them in the same mapper file (e.g., column lists).

<sql id="testuser_columns">
name,age,create_time
</sql>

<insert id="insertUser">
insert into `test_user` (
<include refid="testuser_columns"/>
) values (
#{name}, #{age}, now()
)
</insert>

Attributes

PropertyDescription
id必选 Identifier for the fragment.