mybatis mapper.xml like right
时间: 2023-07-13 17:25:57 浏览: 122
MyBatis mapper.xml files are used to define SQL statements and map them to Java methods. Here is an example of a simple mapper.xml file:
```xml
<?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.dao.UserDao">
<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, email, password) VALUES (#{name}, #{email}, #{password})
</insert>
<update id="updateUser" parameterType="com.example.model.User">
UPDATE users SET name = #{name}, email = #{email}, password = #{password} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM users WHERE id = #{id}
</delete>
</mapper>
```
In this example, the mapper.xml file defines four SQL statements: `getUserById`, `insertUser`, `updateUser`, and `deleteUser`. Each statement has a unique `id` attribute and specifies the parameter type and result type. The statements themselves are defined using standard SQL syntax, with parameter placeholders represented by the `#{}` syntax.
Note that the `namespace` attribute in the `mapper` element should match the fully-qualified name of the corresponding Java interface, which defines the methods that will be mapped to these SQL statements.
阅读全文