[英]How to set a data for a particular row in springboot
我创建了一个登录页面,我正在获取用户电子邮件 ID,然后我正在生成一个 otp 并发送给用户邮件。 现在我想通过使用他的 mailid 将数据库中生成的 otp 保存到特定用户。 但问题是当我将 otp 保存在数据库中时,它没有存储在给定的 mailid 中,我无法弄清楚。
我的代码:
发送 Otp 代码:
public String sendOtp(@RequestParam("email") String email) {
// int otp = random.nextInt(999999);
String otp = RandomString.make(8);
String subject = "OTP from session-handling-proj By Harshit";
String toEmail = email;
String body = "OTP : " + otp;
SessionHandling sessionHandling = new SessionHandling();
sessionHandling.setOneTimePassword(otp);
repo.save(sessionHandling);
this.emailService.sendMail(toEmail, subject, body);
return ("success");
数据库:
Id | password | emailid | name | Cpass | otp | time
8 | 123 | shy@gmail.com | shymm | 123 | NULL | NULL
9 | NULL | NULL | NULL | NULL | Wi0ioiuZ | NULL
10 | NULL | NULL | NULL | NULL | R98RT1OZ | NULL
当我尝试存储 otp 时,它创建了新行,即 9 和 10。
正如@Thomas 已经解释的那样,您应该始终在生产中散列您的密码。
但是,这里是使用 JPA 存储库更新 otp 的示例:
@Repository
public interface SessionHandlingRepository extends JpaRepository<SessionHandling, Long> {
@Transactional
@Modifying
@Query(value = "UPDATE SessionHandling s SET s.otp = :otpCode WHERE s.id = :id")
void updateOtp(@Param("id") Long whereId, @Param("otpCode") String otp);
}
首先,您必须将数据存储在与该特定 emailId 相关的会话处理类中,然后设置 otp 并保存它。
在其中创建存储库类 make 方法findByEmailID() 。
xyzrepo.class
@Query(value = "Select * from sessionhandling where emailId=?1",nativeQuery = true)
SessionHandling findByEmailId(String emailId);
你的方法 SendOtp 看起来像:
//Autowired xyzrepo
public String sendOtp(@RequestParam("email") String email) {
// int otp = random.nextInt(999999);
String otp = RandomString.make(8);
String subject = "OTP from session-handling-proj By Harshit";
String toEmail = email;
String body = "OTP : " + otp;
SessionHandling sessionHandling = xyzrepo.findByEmailId(email);
sessionHandling.setOneTimePassword(otp);
repo.save(sessionHandling);
this.emailService.sendMail(toEmail, subject, body);
return ("success");
您有 2 种情况:情况 1 :您是只保存一条邮件 ID 记录,还是每次登录操作可能有很多条记录。 根据您的业务将创建查询
如果只有一条记录:并且您确定 Db 中存在电子邮件,请使用此代码
@Repository
public interface SessionHandlingRepository extends JpaRepository<SessionHandling, Long> {
@Transactional
@Modifying
@Query(value = "UPDATE SessionHandling s SET s.otp = :otpCode WHERE s.emailid= :emailId")
void updateOtp(@Param("emailId") String mailId, @Param("otpCode") String otp);
}
如果您不确定,
所以你必须为所有细节保存一个新记录
案例 2:邮件允许许多记录:因此您还必须为电子邮件和新 Otp 的 SessionHandling 创建新记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.