描述:
com.rst.boot.services.impl.UserServiceImpl中的字段userRepository需要找不到名为'entityManagerFactory'的bean。
行动:
考虑在配置中定义一个名为“ entityManagerFactory”的bean。
这是我的服务班级:
@Service
public class UserServiceImpl implements UserServices {
@Autowired
private UserRepository userRepository;
@Override
public List<UserDTO> findAllUsers() {
List<UserDTO> allusers= userRepository.findAll();
return allusers;
}
@Override
public String saveUser(UserDTO userdata) {
userRepository.save(userdata);
return "Data saved";
}
}
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd“>
4.0.0
<groupId>org.example</groupId>
<artifactId>Leaning</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Springboot Demo App</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
最佳答案
首先,在您的bootstrap类中,确保您具有@EnableAutoConfiguration批注。如果您有@SpringBootApplication,应该足够了,因为后者用第一个注释。
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
其次,确保UserRepository扩展了JpaRepository或CrudRepository:
public interface UserRepository extends JpaRepository<User, Long> {}
最后,检查您的Maven或Gradle配置中是否具有正确的依赖项。例如。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>