我是 Spring Boot 和 Data Jpa 的一條魚,我嘗試創建一個基本的 Spring Boot 應用程式,但每次都遇到錯誤。你能幫助我嗎?
那是我的代碼:Spring Boot Application 類:
@SpringBootApplication
@ComponentScan(basePackages = "com.project.*")
@EnableJpaRepositories(basePackages = "com.project.repository.*")
@EntityScan(basePackages = "com.project.entities.*")
@EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
控制器類:
@RestController
@RequestMapping(value = "/api")
public class controller {
private IUserServices userServices;
@Autowired
public controller(IUserServices userServices) {
this.userServices = userServices;
}
@GetMapping(value = "/merhaba")
public String sayHello(){
return "Hello World";
}
@GetMapping(value = "/getall")
public List<User> getAll(){
return this.userServices.getAllUsers();
}
}
存盤庫類:
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
IServices 類:
@Service
public interface IUserServices {
void saveUser(User user);
List<User> getAllUsers();
}
服務Impl類:
@Service
public class UserServicesImpl implements IUserServices{
private UserRepository userRepository;
@Autowired
public UserServicesImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public void saveUser(User user) {
this.userRepository.save(user);
}
@Override
public List<User> getAllUsers() {
return this.userRepository.findAll();
}
}
物體類:
@Entity
@Table(catalog = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{"
"id=" id
", name='" name '\''
'}';
}
}
這是我的錯誤資訊:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.project.services.UserServicesImpl required a bean of
type 'com.project.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.project.repository.UserRepository' in your
configuration.
Process finished with exit code 0
所以這是應用程式屬性檔案:
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/u
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.properties.javax.persistence.validation.mode = none
uj5u.com熱心網友回復:
有一些問題您應該解決。
第一的
當您擁有帶有@SpringBootApplication的 Spring Boot 應用程式時,您不需要其他東西,例如@EnableAutoConfiguration等等,因此將它們全部洗掉。
您可以在此處閱讀更多相關資訊。
第二
你不需要用@Service 來注釋你的服務介面,因為你是在UserServicesImpl課堂上做的。
第三
您id在用戶物體中定義為整數,但在存盤庫中,您將 id 寫為Long. 這是錯的。它應該是這樣的。
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
嘗試上述解決方案,讓我知道結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322728.html
上一篇:設定HttpContext.Current在繼續等待的程序中是不一樣的。
下一篇:React:基于道具的設定和更新
