在Spring Boot中有效地使用自定义注释验证器中的数据库
因此,我想更新用户数据,但是在此之前,我想使用请求中给出的MEMBEBEID在数据库中检查用户数据是否存在。
通常,我会首先使用Spring提供的“ FindbyMemberID”功能找到用户数据,然后检查结果。如果结果为null,我应该丢一些错误,因为这意味着用户不在数据库中。如果存在,我将继续更新过程。
现在,我想尝试使用Spring的自定义注释验证器检查MEMBEBEID是否存在。这是我的ConstraintValidator类,
@Component
public class MemberIdMustExistsValidator implements ConstraintValidator<MemberIdMustExists, String> {
@Autowired
private MemberRepository memberRepository;
@Override
public void initialize(MemberIdMustExists memberIdMustExists) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isBlank(value)) {
return true;
}
return memberRepository.findByMemberId(value) != null;
}
}
我附加了MemberID字段的注释,这是我的服务类:
@Service
public class UpdateMemberImpl implements UpdateMember {
@Autowired
private MemberRepository memberRepository;
@Override
public Single<GetMemberResponse> execute(
GetMemberRequest request) {
Member memeber = memberRepository.findByMemberId(request.getMemberId());
//logic to update goes here
}
}
看起来更简单,因为我不必检查用户是否存在并在服务类中丢弃一些错误。但是我注意到我将数据库调用的时间要多于使用第一种更新用户数据的方法。使用此方法,我需要两次调用“ FindbyMemberId”,第一个是检查CustomValidator类中的存在,第二个是在服务类中获取用户数据以更新数据。
对于这种情况有更有效的方法还是我做错了?因为我想使用自定义注释验证器,但似乎需要我对数据库的呼叫比使用普通方式更多。
So I want to update user data, but before it, I want to check if the user data exist in my database using membeId given from the request.
Usually, I'll find the user data first using 'findByMemberId' function provided by spring and check the result. If the result is null, I should throw some errors because it means the user is not in the database. And if it exists I'll continue the update process.
Now I want to try using a custom annotation validator from spring to check whether the membeId exists or not. Here is my ConstraintValidator class
@Component
public class MemberIdMustExistsValidator implements ConstraintValidator<MemberIdMustExists, String> {
@Autowired
private MemberRepository memberRepository;
@Override
public void initialize(MemberIdMustExists memberIdMustExists) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isBlank(value)) {
return true;
}
return memberRepository.findByMemberId(value) != null;
}
}
I attached the annotation in memberId field and here is my service class :
@Service
public class UpdateMemberImpl implements UpdateMember {
@Autowired
private MemberRepository memberRepository;
@Override
public Single<GetMemberResponse> execute(
GetMemberRequest request) {
Member memeber = memberRepository.findByMemberId(request.getMemberId());
//logic to update goes here
}
}
It looks simpler because I don't have to check whether the user exists or not and throw some errors in the service class. But I noticed that I call the database more than if use the first way to update user data. With this method, I need to call 'findByMemberId' twice, the first is for checking the existence in CustomValidator class and the second is to get the user data in the service class to update the data.
Is there a more effective way for this case or Did I do it wrong? Because I want to use Custom Annotation Validator but it seems like requires me to make a call to the database more than I use the normal way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论