MyBatis update

1. 定义

慕课解释:update 标签用于映射 SQL 中的更新语句。

2. 前言

本小节,我们将一起学习 MyBatis update。

在 MyBatis 中,update 标签对应于 SQL 语句中的 update 更新。

3. 实例

3.1 xml 实例

如下就是一个真实的 update 标签实例。

<update id="updateUserAgeById">
  UPDATE imooc_user SET age = #{age} WHERE id = #{id}
</update>

每一个 update 标签都必须有一个唯一的 id 属性,在 update 标签内部则是一条 SQL 语句。

3.2 注解实例

使用如下的注解方式,我们也可以实现同样的功能。

@Update("UPDATE imooc_user SET age = #{age} WHERE id = #{id}")
int updateUserAgeById(@Param("age") Integer age, @Param("id") Integer id);

4. update 属性

update 标签支持一些属性来改变更新语句的行为。

其中常见且重要的属性如下表:

属性 描述
id 在命名空间中的唯一标识符
parameterType 语句的参数类型,默认可选,MyBatis 会自动推断
flushCache 设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认为 false
timeout 设置超时时间
statementType STATEMENT,PREPARED 或 CALLABLE 中的一个,默认为 PREPARED(预处理)

5. 实践

5.1 例1. 更新用户年龄

请使用 MyBatis 完成对 imooc_user 表中用户年龄更新的功能。

分析:

按照 MyBatis 的开发模式,先在对应 UserMapper.xml 文件中添加用户年龄更新的 update 标签,然后在 UserMapper.java 中增加上对应的方法即可。

步骤:

首先,在 UserMapper.xml 中添加 update 标签,并在标签中写入 SQL :

<update id="updateUserAgeById">
  UPDATE imooc_user SET age = #{age} WHERE id = #{id}
</update>

然后在 UserMapper.java 中添加上对应的接口方法,方法接受 age 和 id 两个参数。

package com.imooc.mybatis.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {
  int updateUserAgeById(@Param("age") Integer age, @Param("id") Integer id);
}

注意:这里我们使用了@Param这个注解,由于在 update 标签中有两个参数 age 和 id,我们需要通过 @Param 来告诉 MyBatis 参数的对应关系。

结果:

通过如下代码,我们运行 updateUserAgeById 这个方法。

UserMapper userMapper = session.getMapper(UserMapper.class);
int rows = userMapper.updateUserAgeById(180, 1);
System.out.println(rows);
// 一定要提交
session.commit();
session.close();

成功后,id 为 1 的用户年龄已经被更新为 180。

+----+-------------+-----+--------+
| id | username    | age | score  |
+----+-------------+-----+--------+
| 1  | peter       | 180 | 100    |
+----+-------------+-----+--------+

5.2 例2. 更新用户名称和分数

请使用 MyBatis 完成对 imooc_user 表中用户名称和分数更新的功能。

分析:

同上。

步骤:

首先,在 UserMapper.xml 中添加另一个 update 标签,并在标签中写入 SQL :

<update id="updateUsernameAndScoreById">
  UPDATE imooc_user SET username = #{username}, score = #{score} WHERE id = #{id}
</update>

然后在 UserMapper.java 中添加上对应的接口方法,方法接受 username、score 和 id 三个参数。

package com.imooc.mybatis.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {
  int updateUsernameAndScoreById(@Param("username") String username, @Param("score") Integer score, @Param("id") Integer id);
}

结果:

通过如下代码,我们运行 updateUsernameAndScoreById 这个方法。

UserMapper userMapper = session.getMapper(UserMapper.class);
int rows = userMapper.updateUsernameAndScoreById("peter-gao", 1000,1);
System.out.println(rows);
// 一定要提交
session.commit();
session.close();

成功后,id 为 1 的用户信息已被更改。

+----+-------------+-----+--------+
| id | username    | age | score  |
+----+-------------+-----+--------+
| 1  | peter-gao   | 180 | 1000   |
+----+-------------+-----+--------+

6. 小结

  • update 标签并无太多的知识点,主要的工作量在书写 SQL 上,因此良好的 SQL 功底可以帮助你更加快速的上手 MyBatis。