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