MyBatis update
1. 定義
慕課解釋:update 標(biāo)簽用于映射 SQL 中的
更新
語句。
2. 前言
本小節(jié),我們將一起學(xué)習(xí) MyBatis update。
在 MyBatis 中,update 標(biāo)簽對應(yīng)于 SQL 語句中的 update 更新。
3. 實例
3.1 xml 實例
如下就是一個真實的 update 標(biāo)簽實例。
<update id="updateUserAgeById">
UPDATE imooc_user SET age = #{age} WHERE id = #{id}
</update>
每一個 update 標(biāo)簽都必須有一個唯一的 id 屬性,在 update 標(biāo)簽內(nèi)部則是一條 SQL 語句。
3.2 注解實例
使用如下的注解方式,我們也可以實現(xiàn)同樣的功能。
@Update("UPDATE imooc_user SET age = #{age} WHERE id = #{id}")
int updateUserAgeById(@Param("age") Integer age, @Param("id") Integer id);
4. update 屬性
update 標(biāo)簽支持一些屬性來改變更新語句的行為。
其中常見且重要的屬性如下表:
屬性 | 描述 |
---|---|
id | 在命名空間中的唯一標(biāo)識符 |
parameterType | 語句的參數(shù)類型,默認可選,MyBatis 會自動推斷 |
flushCache | 設(shè)置為 true 后,只要語句被調(diào)用,都會導(dǎo)致本地緩存和二級緩存被清空,默認為 false |
timeout | 設(shè)置超時時間 |
statementType | STATEMENT,PREPARED 或 CALLABLE 中的一個,默認為 PREPARED(預(yù)處理) |
5. 實踐
5.1 例1. 更新用戶年齡
請使用 MyBatis 完成對 imooc_user 表中用戶年齡更新的功能。
分析:
按照 MyBatis 的開發(fā)模式,先在對應(yīng) UserMapper.xml 文件中添加用戶年齡更新的 update 標(biāo)簽,然后在 UserMapper.java 中增加上對應(yīng)的方法即可。
步驟:
首先,在 UserMapper.xml 中添加 update 標(biāo)簽,并在標(biāo)簽中寫入 SQL :
<update id="updateUserAgeById">
UPDATE imooc_user SET age = #{age} WHERE id = #{id}
</update>
然后在 UserMapper.java 中添加上對應(yīng)的接口方法,方法接受 age 和 id 兩個參數(shù)。
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 標(biāo)簽中有兩個參數(shù) age 和 id,我們需要通過 @Param 來告訴 MyBatis 參數(shù)的對應(yīng)關(guān)系。
結(jié)果:
通過如下代碼,我們運行 updateUserAgeById 這個方法。
UserMapper userMapper = session.getMapper(UserMapper.class);
int rows = userMapper.updateUserAgeById(180, 1);
System.out.println(rows);
// 一定要提交
session.commit();
session.close();
成功后,id 為 1 的用戶年齡已經(jīng)被更新為 180。
+----+-------------+-----+--------+
| id | username | age | score |
+----+-------------+-----+--------+
| 1 | peter | 180 | 100 |
+----+-------------+-----+--------+
5.2 例2. 更新用戶名稱和分?jǐn)?shù)
請使用 MyBatis 完成對 imooc_user 表中用戶名稱和分?jǐn)?shù)更新的功能。
分析:
同上。
步驟:
首先,在 UserMapper.xml 中添加另一個 update 標(biāo)簽,并在標(biāo)簽中寫入 SQL :
<update id="updateUsernameAndScoreById">
UPDATE imooc_user SET username = #{username}, score = #{score} WHERE id = #{id}
</update>
然后在 UserMapper.java 中添加上對應(yīng)的接口方法,方法接受 username、score 和 id 三個參數(shù)。
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);
}
結(jié)果:
通過如下代碼,我們運行 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. 小結(jié)
- update 標(biāo)簽并無太多的知識點,主要的工作量在書寫 SQL 上,因此良好的 SQL 功底可以幫助你更加快速的上手 MyBatis。