From daa8eeef2e99b3c4c43e9be7c84694e914a1e5d9 Mon Sep 17 00:00:00 2001 From: Revers <0x00stone@gmail.com> Date: Sat, 24 Feb 2024 22:10:25 +0800 Subject: [PATCH] fix article and ad idempotent problem --- .../music/service/CMSAdvertisementService.java | 16 +++++++++++++++- .../luoo/music/service/CMSArticleService.java | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java index 47f261d..5e7936e 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSAdvertisementService.java @@ -11,6 +11,8 @@ import com.luoo.music.pojo.UserInfo; import com.luoo.music.util.Constants; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; @@ -76,7 +78,19 @@ public class CMSAdvertisementService { ad.setUserName(userInfo.getName()); ad.setUserType(userInfo.getType()); } - adDao.save(ad); + + + Example example = Example.of(ad, ExampleMatcher.matching() + .withIgnorePaths("id") + .withIgnorePaths("image") + .withIgnorePaths("pub_time") + .withIgnorePaths("createTime") + .withIgnorePaths("updateTime") + ); + + if(!adDao.exists(example)) { + adDao.save(ad); + } return Result.success(); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java index 3b23933..df93e5b 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java @@ -11,6 +11,8 @@ import com.luoo.music.pojo.UserInfo; import com.luoo.music.util.Constants; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; @@ -101,7 +103,18 @@ public class CMSArticleService { article.setPubTime(LocalDateTime.now()); }*/ - articleDao.save(article); + Example
example = Example.of(article, ExampleMatcher.matching() + .withIgnorePaths("id") + .withIgnorePaths("image") + .withIgnorePaths("pubTime") + .withIgnorePaths("createTime") + .withIgnorePaths("updateTime") + ); + + if(!articleDao.exists(example)) { + articleDao.save(article); + } + return Result.success(); }