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语句和结果映射,可以实现数据库操作和结果映射的功能。