Java 用于创建方法公共摘要的查询的字段
我在项目中使用controller、user、repository和service创建了一个登录用户。在控制器中,若电子邮件id和密码不为空,则登录,否则抛出异常“用户不存在”。我收到下面的错误,你能帮我解决这个问题吗 错误:Java 用于创建方法公共摘要的查询的字段,java,spring,spring-boot,jpa,Java,Spring,Spring Boot,Jpa,我在项目中使用controller、user、repository和service创建了一个登录用户。在控制器中,若电子邮件id和密码不为空,则登录,否则抛出异常“用户不存在”。我收到下面的错误,你能帮我解决这个问题吗 错误: Error creating bean with name 'registrationRepository': FactoryBean threw exception on object creation; nested exception is java.lang.Il
Error creating bean with name 'registrationRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.adventure.network.model.User com.adventure.network.repository.RegistrationRepository.findByEmailIdAndByPassword(java.lang.String,java.lang.String)! No property byPassword found for type User!
控制器:
@RestController
public class RegistrationController {
@Autowired
private RegistrationService service;
@PostMapping("/login")
public User loginUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
String tempPassword = user.getPassword();
User userObject = null;
if(tempEmailId!=null && tempPassword!=null) {
userObject = service.fetchUserByEmailIdByPassword(tempEmailId, tempPassword);
}
if(userObject == null) {
throw new Exception("User is not exict");
}
return userObject;
}
服务:
@Service
public class RegistrationService {
@Autowired
private RegistrationRepository repo;
public User fetchUserByEmailIdByPassword(String email, String password) {
return repo.findByEmailIdAndByPassword(email, password);
}
}
存储库:
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailIdAndByPassword(String emailId, String password);
}
public interface RegistrationRepository扩展了JpaRepository{
公共用户findByEmailIdAndByPassword(字符串emailId,字符串密码);
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.adventure</groupId>
<artifactId>network</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>network</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.3.3.2发布
冒险
网络
0.0.1-快照
网络
SpringBoot的演示项目
11
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
com.h2数据库
氢
运行时
org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机
org.springframework.boot
springbootmaven插件
根据您的错误消息找不到类型User的属性词
,它正在查找名为byPassword
的属性,您需要在存储库中更改为和密码,如下所示
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailIdAndPassword(String emailId, String password);
}
public interface RegistrationRepository扩展了JpaRepository{
公共用户findByEmailIdAndPassword(字符串emailId,字符串密码);
}
Spring将解析by之后提到的每个属性,您只需要在方法的开头使用by
您还可以将名称更改为更易于阅读
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findUserByEmailIdAndPassword(String emailId, String password);
}
public interface RegistrationRepository扩展了JpaRepository{
公共用户FindUserByMaildAndPassword(字符串emailId,字符串密码);
}
重命名方法FindByeMaildAndPassword
为FindByeMaildAndPassword
噢,将FindByeMaildAndPassword更改为FindByeMaildAndPassword后,它得到了修复。该方法可以是任何名称。为什么我的方法名称出现错误?不在存储库中。Spring解析存储库类的方法名,以了解用于搜索的User
类的哪些属性。这就是JPA存储库在spring boot中的工作方式