MyBatis中批量插入和批量更新的實(shí)現(xiàn)方法詳解
在編程中應(yīng)用程序和DB操作的第一步就是需要進(jìn)行網(wǎng)絡(luò)連接,這就是我們?cè)诔绦蛐枰O(shè)置數(shù)據(jù)源配置的原因,網(wǎng)絡(luò)連接即網(wǎng)絡(luò)通信是有一定耗時(shí)的,少量的DB操作問題不大,但是如果有大批量數(shù)據(jù)同時(shí)需要頻繁地進(jìn)行DB操作,這個(gè)時(shí)候網(wǎng)絡(luò)通信耗時(shí)對(duì)應(yīng)用運(yùn)行的影響就顯示出來(lái)了,如果一個(gè)一個(gè)的進(jìn)行DB操作肯定比較耗時(shí),這個(gè)時(shí)候就需要考慮批量操作,也就是一次執(zhí)行多個(gè)DB操作,分多批次進(jìn)行,這樣就減少和DB鏈接的網(wǎng)絡(luò)通信時(shí)間,那怎么進(jìn)行批量操作呢?下面簡(jiǎn)單介紹在Java中使用MyBatis進(jìn)行DB批量操作,步驟如下:
1,假設(shè)有MySQL庫(kù)的用戶信息表結(jié)構(gòu)如下:
create table user ( id bigint auto_increment comment '記錄唯一ID', user_code varchar(50) COLLATE utf8mb4_unicode_ci default '' not null comment '用戶編碼', user_name varchar(50) COLLATE utf8mb4_unicode_ci default '' not null comment '用戶姓名', age int(12) default 18 null comment '年齡', salary int(12) default 0 null comment '工資', email varchar(100) default '' null comment '郵箱', primary key (id), unique key 'uk_user_code' (user_code) ) ENGINE=InnoDB auto_increment=1 CHARSET=utf8 COMMENT='用戶信息表';
2,用戶信息對(duì)應(yīng)的Java Bean定義
public class User {
private Long id;
private String userCode;
private String userName;
private Integer age;
private String email;
private Integer salary;
//...getter/setter省略...
}3,批量操作需要知道原理,底層是以SQL語(yǔ)句為基礎(chǔ)的,原始SQL批量插入語(yǔ)句,例如:
insert into user (user_code, user_name, age)
values
('zhangsan','張三',18),('lisi','李四',20),
('wangwu','王五',30),('laoliu','老六',36)4,批量操作需要知道原理,底層是以SQL語(yǔ)句為基礎(chǔ)的,原始SQL批量更新語(yǔ)句,例如:
按要求批量更新,zhangsan, lisi, wangwu 這三個(gè)員工,
郵箱:zhangsan=zhangsan01@163.com,lisi=lisi03@163.com,wangwu=wangwu06@163.com
工資:21歲以下姓李的員工工資加200,21歲以上姓王的員工工資加400,
對(duì)應(yīng)批量更新SQL如下:
update user set
email =
case
when user_code = 'zhangsan' then 'zhangsan01@163.com'
when user_code = 'lisi' then 'lisi03@163.com'
when user_code = 'wangwu' then 'wangwu06@163.com'
end,
salary =
case
when age < 21 and user_name like '李%' then salary+200
when age > 21 and user_name like '王%' then salary+400
end
where user_code in ('zhangsan','lisi','wangwu')5,mapper定義操作DB持久化的接口
@Mapper
public interface UserMapper {
/**批量插入用戶數(shù)據(jù)*/
int batchInsertUser(@Param("userList") List<User> userList)
/**批量更新用戶數(shù)據(jù)*/
int batchUpdateUser(@Param("userList") List<User> userList)
}6,mybatis中xml配置文件對(duì)應(yīng)的定義
<!-- 其它省略 -->
<!-- 批量插入數(shù)據(jù) -->
<insert id="batchInsertUser" parameterType="java.util.List">
insert into user (user_code, user_name, age)
values
<foreach collection="userList" item="user" index="index" separator=",">
(#{user.userCode},
#{user.userName},
#{user.age})
</foreach>
</insert>
<!-- 批量更新數(shù)據(jù) -->
<update id="batchUpdateUser">
upate user
<trim prefix="set" suffixOverrides=",">
<trim prefix="email = case" suffix="end,">
<foreach collection="userList" item="user" index="index">
when user_code = #{user.userCode} then #{user.email}
</foreach>
</trim>
<trim prefix="salary = case" suffix="end,">
<![CDATA[ when age < 21 and user_name like '李%' then salary+200 ]]>
<![CDATA[ when age > 21 and user_name like '王%' then salary+400 ]]>
</trim>
</trim>
where user_code in (
<foreach collection="userList" item="user" index="index" separator=",">
#{user.userCode}
</foreach>
)
</update>7,業(yè)務(wù)接口UserServicve定義
public interface UserServicve {
/**批量數(shù)據(jù)插入*/
int batchInsertUser(List<User> userList);
/**批量數(shù)據(jù)更新*/
int batchUpdateUser(List<User> userList);
}8,業(yè)務(wù)接口UserServicve實(shí)現(xiàn)
public class UserServicveImpl implements UserServicve {
@Autowired
private UserMapper userMapper;
/**批量數(shù)據(jù)插入*/
@Overwrite
public int batchInsertUser(List<User> userList) {
if (CollectionUtil.isEmpty(userList)) {
return 0;
}
return userMapper.batchInsertUser(userList);
}
/**批量數(shù)據(jù)插入*/
@Overwrite
public int batchUpdateUser(List<User> userList) {
if (CollectionUtil.isEmpty(userList)) {
return 0;
}
return userMapper.batchUpdateUser(userList);
}
}批量在需要頻繁的大數(shù)據(jù)量操作DB的地方經(jīng)常用到,批量對(duì)DB進(jìn)行操作可以減少應(yīng)用和DB的網(wǎng)絡(luò)連接耗時(shí),可以很大提高應(yīng)用的響應(yīng)效率
到此這篇關(guān)于MyBatis中批量插入和批量更新的實(shí)現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)MyBatis批量插入和批量更新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Java實(shí)現(xiàn)請(qǐng)求deepseek
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)請(qǐng)求deepseek功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
服務(wù)器獲取Jar包運(yùn)行目錄實(shí)現(xiàn)方式
本文介紹了兩種獲取Java應(yīng)用程序運(yùn)行目錄的方法:使用`System.getProperty("user.dir")`和通過`ProtectionDomain`及`CodeSource`類,前者簡(jiǎn)單直接,但返回的是當(dāng)前工作目錄;后者更為復(fù)雜,但能準(zhǔn)確獲取JAR文件的路徑,選擇哪種方法取決于具體需求2025-11-11
使用PoolingHttpClientConnectionManager實(shí)現(xiàn)http連接池過程
文章主要介紹了`PoolingHttpClientConnectionManager`實(shí)現(xiàn)HTTP連接池的原理、依賴實(shí)現(xiàn)、定時(shí)回收鏈接的方法,以及如何正確釋放連接以復(fù)用2025-11-11
java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
MyBatis處理懶加載和預(yù)加載的實(shí)戰(zhàn)詳解
在Java開發(fā)中,MyBatis是一個(gè)廣泛使用的持久層框架,它為開發(fā)者提供了靈活、強(qiáng)大的數(shù)據(jù)訪問能力,兩種常用的加載策略是懶加載和預(yù)加載,本文將介紹這兩種加載方式的概念、適用場(chǎng)景以及MyBatis中的實(shí)現(xiàn)方式,需要的朋友可以參考下2026-02-02
SpringBoot配置Ollama實(shí)現(xiàn)本地部署DeepSeek
本文主要介紹了在本地環(huán)境中使用?Ollama?配置?DeepSeek?模型,并在?IntelliJ?IDEA?中創(chuàng)建一個(gè)?Spring?Boot?項(xiàng)目來(lái)調(diào)用該模型,文中通過圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
SpringBoot監(jiān)聽器的實(shí)現(xiàn)示例
在SpringBoot中,你可以使用監(jiān)聽器來(lái)響應(yīng)特定的事件,本文主要介紹了SpringBoot監(jiān)聽器的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Java面試題篇之Sleep()方法與Wait()方法的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Java面試題篇之Sleep()方法與Wait()方法區(qū)別的相關(guān)資料,wait()是Object類中的方法,而sleep()是Thread類中的靜態(tài)方法,wait()方法用于多個(gè)線程之間的協(xié)作和通信,而sleep()方法用于線程的休眠,需要的朋友可以參考下2024-07-07
MyBatis-plus的五種批量插入方式對(duì)比分析
本文主要介紹了MyBatis-plus的五種批量插入方式對(duì)比分析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

