myatisplus的saveOrUpdate的提交總是update問題
背景
很多朋友,想把新增和編輯做在一起。
沒想到自己遇到坑了,
自己的saveOrUpdate的提交總是update。
控制臺sql輸出
UPDATE t_course_type SET course_type_name=?, create_time=?, create_user=?, update_time=?, update_user=?
報(bào)錯
com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of table update operation
官方文檔
// TableId 注解存在更新記錄,否插入一條記錄 boolean saveOrUpdate(T entity); // 根據(jù)updateWrapper嘗試更新,否繼續(xù)執(zhí)行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
分析
發(fā)現(xiàn)上面的sql沒有where條件。
以為是新增操作,沒有id,主鍵是自增的,就加了個判斷
ObjectUtils.isNotEmpty(courseTypeId)。
發(fā)現(xiàn)是updateWrapper的eq方法 不能加condition參數(shù)。
錯誤方式
updateWrapper.eq(ObjectUtils.isNotEmpty(courseTypeId), CourseTypeEntity::getCourseTypeId, courseTypeId);
正確方式
去除第一個condition參數(shù)。保留2個參數(shù)即可。
updateWrapper.eq(CourseTypeEntity::getCourseTypeId, courseTypeId);
成功案例實(shí)現(xiàn)源碼分享
LambdaUpdateWrapper<CourseTypeEntity> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(CourseTypeEntity::getCourseTypeId, courseTypeId);
CourseTypeEntity courseTypeEntity = new CourseTypeEntity();
if (courseTypeIdNotEmpty) {
courseTypeEntity.setCourseTypeId(courseTypeId)
.setUpdateTime(LocalDateTime.now())
.setUpdateUser(username)
.setCourseTypeName(courseTypeName)
.setCourseCount(courseCount);
} else {
courseTypeEntity.setCourseTypeName(courseTypeName)
.setCreateTime(LocalDateTime.now())
.setCreateUser(username)
.setUpdateTime(LocalDateTime.now())
.setUpdateUser(username);
}
boolean saveOrUpdate = this.saveOrUpdate(courseTypeEntity, updateWrapper);
注意
要去了解這個saveOrUpdate的源碼的處理機(jī)制。
源碼
default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
return this.update(entity, updateWrapper) || this.saveOrUpdate(entity);
}
因?yàn)樵摲椒J(rèn)是使用實(shí)體對象的id去匹配,
如果有就更新,
如果沒有就插入。
對于主鍵自增的場景,
一般不會手動設(shè)置id,每一次的id都不相同,
所以如果不使用條件選擇器,一定是插入。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成Activiti7工作流引擎的示例代碼
本文主要介紹了SpringBoot集成Activiti7工作流引擎的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼
本文主要介紹了JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java線程池對象ThreadPoolExecutor的深入講解
在我們的開發(fā)中“池”的概念并不罕見,有數(shù)據(jù)庫連接池、線程池、對象池、常量池等等。下面這篇文章主要給大家介紹了關(guān)于java線程池對象ThreadPoolExecutor的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09
SpringBoot數(shù)據(jù)庫初始化datasource配置方式
這篇文章主要為大家介紹了SpringBoot數(shù)據(jù)庫初始化datasource配置方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Maven中生命周期深度解析與實(shí)戰(zhàn)指南
這篇文章主要為大家詳細(xì)介紹了Maven 生命周期實(shí)戰(zhàn)指南,包含核心概念、階段詳解、Spring Boot 特化場景及企業(yè)級實(shí)踐建議,希望對大家有一定的幫助2025-08-08
Java如何解決發(fā)送Post請求報(bào)Stream?closed問題
這篇文章主要介紹了Java如何解決發(fā)送Post請求報(bào)Stream?closed問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Java中IO流文件讀取、寫入和復(fù)制的實(shí)例
下面小編就為大家?guī)硪黄狫ava中IO流文件讀取、寫入和復(fù)制的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

