Spring JPA之save方法示例詳解
一、save(單條添加)
源碼
@Transactional
@Override
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null.");
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
由代碼可知,它是先去判斷了entity是否為new:
如果是new,就執(zhí)行save方法;
如果非new,則執(zhí)行update方法。這就是jpa為啥只有save接口沒有update接口的原因吧!
此時我們應該有個疑惑:怎么樣才算new呢?繼續(xù)往下看:

/**
* Must be {@link Transient} in order to ensure that no JPA provider complains because of a missing setter.
*
* @see org.springframework.data.domain.Persistable#isNew()
*/
@Transient // DATAJPA-622
@Override
public boolean isNew() {
return null == getId();
}
恍然大悟,原來他是去看 entity 的id(實體類的主鍵)是否存在:不存在就是new。(通俗易懂,很好理解)
service 層
添加 save 方法(save 是三方件自帶接口不需要再dao層中添加)
@Transactional
public User save(User user) {
return userDao.save(user);
}
control層
/**
* 單條數(shù)據(jù)保存
* @param id 自增的,可以不填
* @param name
* @param age
* @return
*/
@GetMapping("/save")
public User save(Integer id,String name, Integer age){
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
return userService.save(user);
}
瀏覽器測試OKhttp://localhost:7777/save?id=1&name=lili&age=18


測試id存在的情況OKhttp://localhost:7777/save?id=1&name=毛毛&age=20


二、saveAll(批量添加)
源碼
/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#save(java.lang.Iterable)
*/
@Transactional
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "Entities must not be null!");
List<S> result = new ArrayList<>();
for (S entity : entities) {
result.add(save(entity));
}
return result;
}
一目了然,saveAll其實就是傳入entity的集合,然后遍歷進行save操作
service
添加 saveAll 方法(saveAll 是三方件自帶接口不需要再dao層中添加)
@Transactional
public <S extends User> List<S> saveAll(Iterable<S> entities) {
return userDao.saveAll(entities);
}
control層
/**
* 批量保存數(shù)據(jù)
* @param ids
* @param names
* @param ages
* @return
*/
@GetMapping("/saveAll")
public List<User> saveAll(int[] ids,String[] names,Integer[] ages){
List<User> list = new ArrayList<>();
for(int i=0; i<names.length; i++){
User user = new User();
user.setId(ids[i]);
user.setName(names[i]);
user.setAge(ages[i]);
list.add(user);
}
return userService.saveAll(list);
}
瀏覽器測試OKhttp://localhost:7777/saveAll?ids=2,3&names=丁丁,壯壯&ages=13,14


注: 如果在實體類中的id上添加注解@GeneratedValue(strategy = GenerationType.IDENTITY),可以給id增加自增屬性,這樣我們就無需再傳入id了
以上就是Spring JPA之save方法示例詳解的詳細內(nèi)容,更多關于Spring JPA save方法詳解的資料請關注腳本之家其它相關文章!
相關文章
IDEA創(chuàng)建SpringBoot的maven項目的方法步驟
這篇文章主要介紹了IDEA創(chuàng)建SpringBoot的maven項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/i
這篇文章主要介紹了IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
SpringBoot整合jasypt實現(xiàn)重要數(shù)據(jù)加密
Jasypt是一個專注于簡化Java加密操作的開源工具,這篇文章主要為大家介紹了詳細介紹了如何使用jasypt實現(xiàn)重要數(shù)據(jù)加密,感興趣的小伙伴可以了解下2025-03-03
logback?EvaluatorFilter日志過濾器源碼解讀
這篇文章主要為大家介紹了logback?EvaluatorFilter日志過濾器源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
idea向System.getenv()添加系統(tǒng)環(huán)境變量的操作
這篇文章主要介紹了idea向System.getenv()添加系統(tǒng)環(huán)境變量的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

