0

我在我的 Spring 引导应用程序中使用 CrudRepository、Mysql。我有如下课程帐户

@Entity
@Table(name="account")
public class Account {

 @Column(name="account_id")
 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
 private long accountId;

 @Column(name="username", unique = true)
 private String username;

 @Column(name="email", unique = true)
 private String email;

 @Column(name="password")
 private String password;

 @Column(name="fname")
 private String fname;

 @Column(name="lname")
 private String lname;

 @Column(name="birth_date")
 @Temporal(TemporalType.DATE)
 private Date birth_date;

我的 AccountController 接受一些参数并更新 Account 类的实例

@PutMapping("/update/{username}")
@ResponseBody
public Boolean updateAccount(@PathVariable String username,
                                 @RequestParam(name="email",required = false) String email,
                                 @RequestParam(name="lname",required = false) String  lname,
                                 @RequestParam(name="fname",required = false) String fname,
                                 @RequestParam(name="birth_date",required = false) String birth_date,
                                 @RequestParam(name="password",required = false) String password){
    Account account = accountService.getAccountByUsername(username);
    if (email!= null) account.setEmail(email);
    if (lname!= null) account.setLname(lname);
    if (fname!= null) account.setFname(fname);
    if (password!= null) account.setPassword(password);
    if (birth_date!= null) {
        //   DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
        System.out.println("Account Controller");
        accountService.updateBirthDate(account, birth_date);
    }

    if (!accountService.checkAccountExistByEmail(email)) {
       accountService.updateAccount(account);
       return true;
    }else return false;
}

我的 AccountService 方法来更新birth_date 是这样的

public void updateBirthDate(Account account, String date){
    System.out.println("Account Service");
    accountRepository.updateBD(date, account.getId());
}

还有我的自定义存储库

@Modifying
@Transactional
@Query(value = "UPDATE account SET birth_date =:date WHERE account_id =:id",
       nativeQuery = true)
void updateBD(@Param("date") String date, @Param("id") Long id);

一切似乎都很好。你可以看到我的 AccountController,我在checkExistEmail之前调用了updateBirthday方法。但是,当我第一次使用完整参数更新 Account 时,Repository 中的updateBD方法会自动将更新后的帐户与其他字段(如电子邮件、lname、fname...)一起保存,因此会导致电子邮件字段在实际调用之前自动更新的问题。下面是日志文件 在此处输入图像描述

我怎样才能只更新birth_date而不更新其他字段。

4

1 回答 1

1

尽管我在代码中看到了一些非常规的东西,但是,您的问题的直接解决方案是:

@PutMapping("/update/{username}")
@ResponseBody
public Boolean updateAccount(@PathVariable String username,
                                 @RequestParam(name="email",required = false) String email,
                                 @RequestParam(name="lname",required = false) String  lname,
                                 @RequestParam(name="fname",required = false) String fname,
                                 @RequestParam(name="birth_date",required = false) String birth_date,
                                 @RequestParam(name="password",required = false) String password){
    Account account = accountService.getAccountByUsername(username);
    if (birth_date!= null) {
        //   DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
        System.out.println("Account Controller");
        accountService.updateBirthDate(account, birth_date);
    }

    if (email!= null) account.setEmail(email);
    if (lname!= null) account.setLname(lname);
    if (fname!= null) account.setFname(fname);
    if (password!= null) account.setPassword(password);
    if (!accountService.checkAccountExistByEmail(email)) {
       accountService.updateAccount(account);
       return true;
    }else return false;
}
于 2021-06-14T11:39:07.600 回答