MyBatis的SQL映射文件是用來定義SQL語句和映射關系的XML文件。在編寫MyBatis的SQL映射文件時,需要按照以下步驟進行:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<!-- SQL statements and mappings go here -->
</mapper>
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
SELECT * FROM users WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="com.example.model.User">
INSERT INTO users (name, age) VALUES (#{name}, #{age})
</insert>
<update id="updateUser" parameterType="com.example.model.User">
UPDATE users SET name = #{name}, age = #{age} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM users WHERE id = #{id}
</delete>
<resultMap id="userResultMap" type="com.example.model.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
SELECT * FROM users WHERE id = #{id}
</select>
以上是編寫MyBatis的SQL映射文件的基本步驟,通過定義SQL語句和結果映射,可以實現數據庫操作和結果映射的功能。