1.查询(Select):
bash
MyBatis: 注解
@Select("SELECT * FROM users WHERE id = #{id}")
User selectUserById(int id);
MyBatis: xml
<!-- 定义查询语句 -->
<select id="selectById" resultType="User">
SELECT * FROM users WHERE id = #{id}
</select>
MySQL 原生语法:
sql
SELECT * FROM users WHERE id = ?
2.插入(Insert):
bash
MyBatis: 注解
@Insert("INSERT INTO users
(id, name, email) VALUES (#{id}, #{name}, #{email})")
void insertUser(User user);
MyBatis: xml
<!-- 定义插入语句 -->
<insert id="insertUser" parameterType="User">
INSERT INTO users (id, username, email) VALUES (#{id}, #{username}, #{email})
</insert>
MySQL 原生语法:
sql
INSERT INTO users (id, username, email) VALUES (?, ?, ?)
3.更新(Update):
bash
MyBatis: xml
<!-- 定义更新语句 -->
<update id="updateUser" parameterType="User">
UPDATE users SET username = #{username}, email = #{email} WHERE id = #{id}
</update>
MySQL 原生语法:
sql
UPDATE users SET username = ?, email = ? WHERE id = ?
4. 删除(Delete):
bash
MyBatis: 注解
@Select("SELECT * FROM users WHERE id = #{id}")
User selectUserById(int id);
MyBatis: xml
<!-- 定义删除语句 -->
<delete id="deleteUserById">
DELETE FROM users WHERE id = #{id}
</delete>
MySQL 原生语法:
sql
DELETE FROM users WHERE id = ?