Mybatis批量更新對象數(shù)據(jù)的兩種實(shí)現(xiàn)方式
說明:
遇到一次需要批量修改對象的場景。
傳遞一個(gè)對象集合,需要根據(jù)對象ID批量修改數(shù)據(jù)庫數(shù)據(jù),使用的是MyBatis框架。查了一些資料,總結(jié)出兩種實(shí)現(xiàn)方式。
創(chuàng)建Demo
首先,創(chuàng)建一個(gè)簡單的Demo;
(User,用戶對象)
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private String id;
private String username;
private String password;
}(UserController,用戶控制器)
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
/**
* 根據(jù)ID查詢用戶
* @param id
* @return
*/
@GetMapping("/getUser/{id}")
public String getUser(@PathVariable("id") String id){
return userMapper.getUser(id).toString();
}
}(UserMapper,用戶數(shù)據(jù)訪問接口)
import com.hezy.pojo.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User getUser(String id);
}數(shù)據(jù)庫數(shù)據(jù),tb_user表

項(xiàng)目啟動(dòng),測試一下接口,沒得問題;

批量更新
創(chuàng)建一個(gè)新的接口,用于觸發(fā)批量更新對象的代碼,內(nèi)容是傳遞一個(gè)List集合對象給Mapper處理;
@GetMapping("/updateUser")
public void updateUser(){
ArrayList<User> users = new ArrayList<>();
User user1 = new User();
user1.setId("1");
user1.setUsername("zhangsan_fix");
User user2 = new User();
user2.setId("2");
user2.setUsername("lisi_fix");
user2.setPassword("654321_fix");
users.add(user1);
users.add(user2);
userMapper.updateUser(users);
}接下來,重點(diǎn)是Mapper.xml中的Statement要怎么寫,繼續(xù)往下看
方式一
首先,我們可以使用動(dòng)態(tài)SQL,如下:
<update id="updateUser">
<foreach collection="users" item="user" separator=";">
update tb_user
<set>
<if test="user.username != null and user.username != ''">
username = #{user.username},
</if>
<if test="user.password != null and user.password != ''">
password = #{user.password}
</if>
</set>
where id = #{user.id}
</foreach>
</update>我們把拼接后的SQL打印出來,會(huì)發(fā)現(xiàn)一個(gè)問題。SQL中,分號(hào)(;)表示一條語句的結(jié)束,使用上面的方式拼接出來的方式,是多條SQL。

因此產(chǎn)生了一個(gè)問題,Mybatis中一個(gè)Statement標(biāo)簽中可以有多條SQL嗎?
答案是默認(rèn)不可以,所以上面的代碼報(bào)錯(cuò)了。
需要在數(shù)據(jù)庫連接后面加上&allowMultiQueries=true配置,如下:

再次執(zhí)行,修改成功了,這是第一種方式;


查看日志發(fā)現(xiàn),Updates:1,就是說如果需要返回更新的記錄條數(shù),那么這種方式返回的更新條數(shù)會(huì)是1,不能體現(xiàn)出數(shù)據(jù)庫真實(shí)發(fā)生變化的記錄條數(shù);
另外,將多條SQL合成一條執(zhí)行,有SQL注入的風(fēng)險(xiǎn)
方式二
第二種方式是用一條SQL的方式來實(shí)現(xiàn),如下:
<update id="updateUser">
update tb_user
set
username = case
<foreach collection="users" item="user">
when id = #{user.id}
<choose>
<when test="user.username != null and user.username != ''">then #{user.username}</when>
<otherwise>then username</otherwise>
</choose>
</foreach>
end,
password = case
<foreach collection="users" item="user">
when id = #{user.id}
<choose>
<when test="user.password != null and user.password != ''">then #{user.password}</when>
<otherwise>then password</otherwise>
</choose>
</foreach>
end
where
<foreach collection="users" item="user" separator="or">
id = #{user.id}
</foreach>
</update>看著有些復(fù)雜,拼接后的SQL如下:
update tb_user set username = case when id = '1' then 'zhangsan_fix' when id = '2' then 'lisi_fix' end, password = case when id = '1' then password when id = '2' then '654321_fix' end where id = '1' or id = '2';
這種方式不需要加額外的配置,執(zhí)行測試;

查看數(shù)據(jù)庫,沒得問題,批量修改完成了;

總結(jié)
本文介紹了Mybatis框架下,批量更新對象的兩種方法:
- 方法一:將多條更新語句合成一條執(zhí)行,需要在數(shù)據(jù)庫鏈接后面增加配置,不能體現(xiàn)真實(shí)修改的記錄條數(shù),有SQL注入的風(fēng)險(xiǎn);
- 方法二:使用case then 關(guān)鍵字實(shí)現(xiàn),SQL復(fù)雜,行數(shù)多,降低了可閱讀性;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot調(diào)用第三方WebService接口的兩種方法
本文主要介紹了SpringBoot調(diào)用第三方WebService接口的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
解決沒有@RunWith 和 @SpringBootTest注解或失效問題
這篇文章主要介紹了解決沒有@RunWith 和 @SpringBootTest注解或失效問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Java中保留兩位小數(shù)的四種方法實(shí)現(xiàn)實(shí)例
今天小編就為大家分享一篇關(guān)于Java中保留兩位小數(shù)的四種方法實(shí)現(xiàn)實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Spring?Boot?集成Elasticsearch模塊實(shí)現(xiàn)簡單查詢功能
本文講解了Spring?Boot集成Elasticsearch采用的是ES模板的方式實(shí)現(xiàn)基礎(chǔ)查詢,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-06-06
SpringBoot+Vue+Element-ui實(shí)現(xiàn)前后端分離
使用前后端分離的方式,可以減少代碼耦合,本文主要介紹了SpringBoot+Vue+Element-ui實(shí)現(xiàn)前后端分離,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
java實(shí)現(xiàn)多數(shù)據(jù)源切換方式
本文介紹實(shí)現(xiàn)多數(shù)據(jù)源切換的四步方法:導(dǎo)入依賴、配置文件、啟動(dòng)類注解、使用@DS標(biāo)記mapper和服務(wù)層,通過注解實(shí)現(xiàn)數(shù)據(jù)源動(dòng)態(tài)切換,適用于實(shí)際開發(fā)中的多數(shù)據(jù)源場景2025-08-08

