Basic structure of XML mapping file
MyBatis' XML mapping file contains the following parts:
mapper
Element: Defines the root element of the XML mapping file, which contains information such as namespace and SQL statements.select
,insert
,update
,delete
elements: SQL statements used to define query, insert, update, and delete operations respectively.resultMap
Element: used to define the mapping relationship of the result set.parameterMap
Element: used to define the mapping relationship of parameters.sql
Element: used to define SQL fragments, which can be referenced in other SQL statements.
The following is an example of a simple XML mapping file:
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
In the above code, we define an UserMapper
XML mapping file and define a getUserById
query operation named in it. This operation receives a int
parameter of type id
and returns a com.example.model.User
result set of type.
Syntax rules for XML mapping files
In the XML mapping file, we need to follow the following syntax rules:
- All XML elements must have a closing tag.
- Attribute values must be enclosed in quotation marks.
- XML element names and attribute names are case-sensitive.
- XML comments can use
<!-- -->
the tag.
The following is a complete example of an XML mapping file:
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="com.example.model.User">
INSERT INTO user (username, password) VALUES (#{username}, #{password})
</insert>
<update id="updateUser" parameterType="com.example.model.User">
UPDATE user SET password = #{password} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
<resultMap id="userMap" type="com.example.model.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
</mapper>
In the above code, we define an UserMapper
XML mapping file and define query, insert, update, delete operations and result mapping relationships in it.
Common uses of XML mapping files
In XML mapping files, we can use the following common usages:
- Use
parameterType
the attribute to specify parameter types. - Use
resultType
the attribute to specify the result type. - Use
#{}
placeholders to represent parameters. - Use elements such as
<if>
,<where>
,<foreach>
etc. to dynamically generate SQL statements. - Use
<selectKey>
the element to get the automatically generated primary key value.
Here is an <if>
example using the element:
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserByUsernameAndPassword" parameterType="com.example.model.User" resultType="com.example.model.User">
SELECT * FROM user WHERE 1 = 1
<if test="username != null">
AND username = #{username}
</if>
<if test="password != null">
AND password = #{password}
</if>
</select>
</mapper>
In the above code, we define a getUserByUsernameAndPassword
query operation named and use <if>
the element to dynamically generate SQL statements. If username
and password
are not null
, use them as query conditions.