亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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。