繁体   English   中英

未能为方法创建查询。 在springboot中创建自定义查询

[英]Failed to create a query for method. Creating a custom query in springboot

我是springboot的新手,我正在尝试编写一个登录方法,该方法将根据电子邮件ID和密码或电话号码和密码登录用户。 在我的服务中,我写了这样的东西

public interface AuthService extends CrudRepository<AuthenticationModel, String> {
    Iterable<AuthenticationModel> findUserByEmailId(String emailId, String password);
    Iterable<AuthenticationModel> findUserByPhoneNumber(Long phoneNumber, String password);
}

我知道像findUserByEmailIdfindUserByPhoneNumber这样的方法没有写在CrudRepository接口中。 这就是我收到此错误的原因。

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Iterable com.xyz.services.authenticate.AuthService.findUserByPhoneNumber(java.lang.Long,java.lang.String)! No property phoneNumber found for type AuthenticationModel!
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96)

我关心的是如何在springboot中实现我的自定义查询? 我知道 JPQL 和本机 SQL 实现。 但这是推荐的方法吗? 说将来我应该实现搜索功能,现在搜索机制本身就是一个巨大的实现。 我可以在搜索机制中实现 JPQL 或原生 SQL 实现吗? 或者还有另一种我不知道的springboot提供的方式吗?

我知道一篇文章中有很多问题。 但是当我试图深入了解互联网上的实际答案时,我感到很困惑。

编辑: AuthenticationModel.class

public class AuthenticationModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    @NotBlank
    @Column(name = "user_uuid", unique = true)
    private UUID userUuid;
    @NotNull
    @NotBlank
    @Email
    @Column(name = "user_email_id", unique = true)
    private String userEmailId;
    @NotNull
    @NotBlank
    @Positive
    @Size(min = 10, max = 10)
    @Column(name = "user_phone_number", unique = true)
    private Long userPhoneNumber;
    @NotNull
    @Column(name = "user_password")
    private String userPassword;
}

Spring 数据 JPA 从方法名称派生查询。 对于 JPA 方法命名查询,您也应该使用确切的字段名称和参数。 这里在方法名称中使用UserEmailIdUserPhoneNumberUserPassword

List<AuthenticationModel> findByUserEmailIdAndUserPassword(String emailId, String password);
List<AuthenticationModel> findByUserPhoneNumberAndUserPassword(Long phoneNumber, String password);

在这里您可以阅读有关JPA方法命名查询的详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM