gpt4 book ai didi

java - 如何持久化实体类中的数据并与数据传输对象结合?

转载 作者:行者123 更新时间:2023-12-01 16:35:16 38 4
gpt4 key购买 nike

我正在开发身份验证功能,但在保留实体类中的数据时遇到了问题。我可以从数据传输对象访问password_hash 和用户名,但无法从数据库中的User 类中查看电子邮件、firstName、lastName 或phoneNumber。下面的照片是我注册新用户时发生的情况的示例。可以看到有4列没有数据。 enter image description here

当我添加 newUser 时,我使用如下所示的 processRegistrationForm 方法...

    @PostMapping("/register")
public String processRegistrationForm(@ModelAttribute @Valid RegisterFormDTO registerFormDTO,
Errors errors, HttpServletRequest request,
Model model) {

// a few conditionals that I removed for brevity

User newUser = new User(registerFormDTO.getUsername(), registerFormDTO.getPassword(), registerFormDTO.getFirstName(), registerFormDTO.getLastName(), registerFormDTO.getEmail(), registerFormDTO.getPhoneNumber());
userRepository.save(newUser);
setUserInSession(request.getSession(), newUser);

return "redirect:";
}

registerFromDTO

public class RegisterFormDTO extends LoginFormDTO{

private String verifyPassword;


private String firstName;

private String lastName;

private String email;

private String phoneNumber;
//getters and setters.

LoginFormDTO

public class LoginFormDTO {

@NotNull
@NotBlank
@Size(min = 3, max = 20, message = "Invalid username. Must be between 3 and 30 characters.")
private String username;

@NotNull
@NotBlank
@Size(min = 5, max = 20, message = "Invalid password. Must be between 5 and 30 characters.")
private String password;
//getters and setters

用户类..

@Entity
public class User extends AbstractEntity {

@NotBlank
private String username;

@NotBlank
private String pwHash;

//@Column(name = "first_name")
private String firstName;
//@Column(name = "last_name")
private String lastName;
//@Column(name = "email")
private String email;
//@Column(name = "phone_number")
private String phoneNumber;

public User() {}

private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

public User(String username, String password, String firstName, String lastName, String email, String phoneNumber){
this.username =username;
this.pwHash = encoder.encode(password);
this.firstName =firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
}

public boolean isMatchingPassword(String password) {
return encoder.matches(password, pwHash);
}
//getters and setters

最后但并非最不重要的是这是我的注册表单 View

<form method="post">
<div class="form-group">
<label>Username
<input class="form-control" th:field="${registerFormDTO.username}" />
</label>
<p class="error" th:errors="${registerFormDTO.username}"></p>
</div>
<div class="form-group">
<label>Password
<input class="form-control" th:field="${registerFormDTO.password}" type="password" />
</label>
<p class="error" th:errors="${registerFormDTO.password}"></p>
</div>
<div class="form-group">
<label>Verify Password
<input class="form-control" th:field="${registerFormDTO.verifyPassword}" type="password" />
</label>
</div>
<div class="form-group">
<label>First Name
<input class="form-control" th:field="${registerFormDTO.firstName}" type="firstName" />
</label>
</div>
<div class="form-group">
<label>Last Name
<input class="form-control" th:field="${registerFormDTO.lastName}" type="lastName" />
</label>
</div>

<input type="submit" class="btn btn-primary" value="Register" />
</form>

最佳答案

您可以使用MapStruct从DTO映射到模型。

例如:

    @Mapper
public interface EmployeeMapper {
@Mappings({
@Mapping(target="employeeId", source="entity.id"),
@Mapping(target="employeeName", source="entity.name")
})
EmployeeDTO employeeToEmployeeDTO(Employee entity);
@Mappings({
@Mapping(target="id", source="dto.employeeId"),
@Mapping(target="name", source="dto.employeeName")
})
Employee employeeDTOtoEmployee(EmployeeDTO dto);
}

Quick Guide to MapStruct”有更多信息。

关于java - 如何持久化实体类中的数据并与数据传输对象结合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61965394/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com