这是我在编译时遇到的错误。我是春季新手,无法理解该错误。请帮助我找出答案。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepo' defined in com.example.jpa.repository.UserRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! No property findName found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! No property findName found for type User!
这是我的存储库课程
根据@Query中的错误,但我正在了解如何修复以及我的错误在哪里。 我在mysql中创建了一个名为user1的tavle。
package com.example.jpa.repository;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder.In;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.jpa.entity.User;
@Repository
public interface UserRepo extends JpaRepository<User, Integer> {
public List<User> findByAgeGreaterThanEqual(int age) ;
public List<User> findByAgeIn(int age);
public List<User> findByNameOrderByName(String name);
@Modifying
@Query (value="select * from user1",nativeQuery=true)
public List<User> getAllUser();
}
这是我的实体类
package com.example.jpa.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
//@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String city;
private String status;
public User() {
super();
}
public User(int id, String name, String city, String status) {
super();
this.id = id;
this.name = name;
this.city = city;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
这是我的主要应用程序主类
package com.example.jpa;
import java.util.List;
import java.util.Optional;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.example.jpa.entity.User;
import com.example.jpa.repository.UserRepo;
@SpringBootApplication
public class SpringApplicaton1Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringApplicaton1Application.class, args);
UserRepo repo = context.getBean(UserRepo.class);
User user = new User();
user.setId(1);
user.setCity("Delhi");
user.setName("Ramesh");
user.setStatus("Java Developer");
List<User> users= repo.getAllUser();
users.forEach(n->{
System.out.println(n);
});
}
}
这是我的Application.property文件
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/practice
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.hibernate.use-new-id-generator-mappings= false
spring.jpa.hibernate.ddl-auto=update
使用 2 个注释,例如enablejparepositories、entityscan
我通过在模型类中添加这些注释来解决这个问题
@Table //is a corresponding table that matches that entity in the database
@Entity // for specifies class is an entity and is mapped to a database table.
@Data // for getter and seter
public class UserModel {
}
@Query(值=“从用户中选择*”,nativeQuery=true) 表名应该大写,即与类名相同。