sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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的数据

 

posted on 2022-03-18 08:46  sunny123456  阅读(842)  评论(0编辑  收藏  举报