我正在使用帶有本機查詢的 Spring Boot 存盤庫。雖然我試圖通過查詢更新幾個欄位并提供錯誤的值,但它并沒有引發任何例外。這是我的代碼,
@Modifying(clearAutomatically = true)
@Query(value = "update tbl_user set is_phone_Verified=true, mobile_verification_otp='XXX', updated_At=:updatedAt where "
"phone_number=:registeredMobile and "
"mobile_verification_otp=:phoneVerificationOtp", nativeQuery = true)
void updateMobileVerified(@Param("registeredMobile") String registeredMobile,
@Param("phoneVerificationOtp") String phoneVerificationOtp,
@Param("updatedAt") Date updatedAt);
現在,問題是即使我提供了錯誤的 otp 值,它也不會引發任何例外。從服務我呼叫這個方法。如果沒有例外,那么我將回傳 true。請查找服務方法。
@Override
public Boolean mobileVerification(String registeredMobile, String phoneVerificationOtp) {
try{
Date date = new Date();
userRepository.updateMobileVerified(registeredMobile,phoneVerificationOtp, date);
return true;
} catch(Exception e) {
return false;
}
}
有人可以建議我如何確定更新是否成功。
謝謝。
uj5u.com熱心網友回復:
如果您想確定是否有任何行被更新,該updateMobileVerified方法可以定義為回傳int而不是void指示更新的行數
uj5u.com熱心網友回復:
您似乎正在更新資料庫中的未知物體。我強烈建議tbl_user通過唯一 ID 查找物體,例如 user_id 或類似的東西。如果用戶不存在,則要么拋出例外,要么回傳 false。在找到給定 tbl_user 的情況下,您基本上會更新所需的屬性。這樣,您可以強制控制更新用戶資料。我希望這可以澄清你的愿景。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/413709.html
標籤:
下一篇:沒有對相關物體的額外查詢
