我需要在注册页面添加电话号码,并且还需要将其保存在数据库中。我按照以下链接。
http://www.jhipster.tech/tips/022_tip_registering_user_with_additional_information.html
但是因为这里的Jhispter版本改变了代码与上面链接中的代码有点不同。因此,我有点困惑。根据我做的链接说明"更新ManagedUserVM"。之后我需要帮助,因为代码不同。
答案 0 :(得分:1)
它真的没有那么大的改变,而且逻辑仍然是一样的。
registerAccount
函数现在应该如下所示:
public void registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
if (!checkPasswordLength(managedUserVM.getPassword())) {
throw new InvalidPasswordException();
}
userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).ifPresent(u -> {throw new LoginAlreadyUsedException();});
userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).ifPresent(u -> {throw new EmailAlreadyUsedException();});
User user = userService.registerUser(managedUserVM, managedUserVM.getPassword(), managedUserVM.getPhone());
mailService.sendActivationEmail(user);
}
registerUser
中的UserService
函数(这是前者createUser
的重命名):
public User registerUser(UserDTO userDTO, String password, String phone) {
// JHipster code omitted for brevity
...
// Create and save the UserExtra entity
UserExtra newUserExtra = new UserExtra();
newUserExtra.setUser(newUser);
newUserExtra.setPhone(phone);
userExtraRepository.save(newUserExtra);
log.debug("Created Information for UserExtra: {}", newUserExtra);
return newUser;
}
请注意,您可能必须手动更改数据库更改日志(如果使用SQL数据库)才能正确链接User和UserExtra的ID,所以它看起来像这样:
<createTable tableName="user_extra">
<column name="phone" type="varchar(255)">
<constraints nullable="true" />
</column>
<column name="user_id" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<!-- jhipster-needle-liquibase-add-column - JHipster will add columns here, do not remove-->
</createTable>