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