Spring JPA之save方法示例详解
作者:烟雨戏江南 时间:2023-09-06 15:50:35
一、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,就执行save方法;
如果非new,则执行update方法。这就是jpa为啥只有save接口没有update接口的原因吧!
此时我们应该有个疑惑:怎么样才算new呢?继续往下看:
/**
* 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层
/**
* 单条数据保存
* @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层
/**
* 批量保存数据
* @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了
来源:https://juejin.cn/post/7221037881184616507
标签:Spring,JPA,save,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 实现左滑出现删除选项
2021-05-28 12:05:59
![](https://img.aspxhome.com/file/2023/1/88661_0s.png)
SpringBoot动态修改日志级别的操作
2022-10-31 05:00:30
![](https://img.aspxhome.com/file/2023/6/67116_0s.png)
c#简单工厂、工厂方法与抽象工厂的区别分析
2021-11-30 04:39:47
C++类和对象之类的6个默认成员函数详解
2022-01-05 13:50:35
![](https://img.aspxhome.com/file/2023/5/129535_0s.png)
关于Jsoup将相对路径转为绝对路径的方法
2022-03-11 04:13:50
![](https://img.aspxhome.com/file/2023/4/63844_0s.png)
Spring框架构造注入type属性实例详解
2021-10-04 19:45:19
![](https://img.aspxhome.com/file/2023/6/105966_0s.png)
IDEA 热部署设置(JRebel插件激活)
2021-08-25 08:14:39
![](https://img.aspxhome.com/file/2023/7/131767_0s.png)
Java之 TreeSet的详细使用说明
2021-08-22 14:52:22
![](https://img.aspxhome.com/file/2023/8/77078_0s.jpg)
Spring @Profile注解详解
2023-04-20 06:26:16
Kotlin中的handler如何避免内存泄漏详解
2023-10-18 22:02:40
Java并发编程示例(七):守护线程的创建和运行
2023-11-25 11:39:32
C#采用mouse_event函数实现模拟鼠标功能
2023-07-07 16:09:54
WPF自定义实现IP地址输入控件
2022-02-24 05:35:36
![](https://img.aspxhome.com/file/2023/3/76383_0s.gif)
Android自定义View实现左右滑动选择出生年份
2023-05-15 22:58:49
![](https://img.aspxhome.com/file/2023/5/138865_0s.gif)
java中MultipartFile互转File的方法
2022-12-14 16:52:09
Android 处理OnItemClickListener时关于焦点颜色的设置问题
2021-09-02 01:47:00
![](https://img.aspxhome.com/file/2023/0/94980_0s.png)
Spring JDBCTemplate原理及使用实例
2023-03-11 09:47:19
![](https://img.aspxhome.com/file/2023/4/62134_0s.png)
C#窗体程序实现全屏及取消全屏步骤
2023-02-18 11:48:38
![](https://img.aspxhome.com/file/2023/9/81069_0s.jpg)
SpringBoot SpEL语法扫盲与查询手册的实现
2022-06-02 23:52:39
Android 将view 转换为Bitmap出现空指针问题解决办法
2022-02-12 02:17:23