MyBatis XML mapping file

Basic structure of XML mapping file

MyBatis' XML mapping file contains the following parts:

  • mapperElement: Defines the root element of the XML mapping file, which contains information such as namespace and SQL statements.
  • select, insert, update, deleteelements: SQL statements used to define query, insert, update, and delete operations respectively.
  • resultMapElement: used to define the mapping relationship of the result set.
  • parameterMapElement: used to define the mapping relationship of parameters.
  • sqlElement: 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 UserMapperXML mapping file and define a getUserByIdquery operation named in it. This operation receives a intparameter of type idand returns a com.example.model.Userresult 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 UserMapperXML 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 parameterTypethe attribute to specify parameter types.
  • Use resultTypethe 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 getUserByUsernameAndPasswordquery operation named and use <if>the element to dynamically generate SQL statements. If usernameand passwordare not null, use them as query conditions.

Guess you like

Origin blog.csdn.net/qq_43597256/article/details/131191832