jpa 中的save()方法 如果对应的id不存在,save方法则为insert,如果存在实际执行根据主键update


jpa 中的save()方法 如果对应的id不存在,save方法则为insert,如果存在实际执行根据主键update
https://www.cnblogs.com/Andrew520/p/9408057.html

?
1
2
3
4
5
6
7
8
@RequestMapping (value = "" , method = RequestMethod.POST)
     public String postAccount( @RequestParam (value = "name" ) String name,
                               @RequestParam (value = "money" ) double money) {
         Account account = new Account();
         account.setMoney(money);
         account.setName(name);
         //account.setId(4);
         Account account1 = accountDao.save(account);

如果对应的id不存在,save方法则为insert

将注释 打开,数据库中是存在id=4的对象的

//account.setId(4);

发送请求 http://localhost:8080/account?name=dd&money=77  ,则此处save为更新 id=4的数据

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM