Spring Boot 无法创建名为“userService”的Bean,通过字段表示的依赖项不满足

rpppsulh  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(241)

我已经添加了@ XiamentScan,@ XiamtyScan在主类仍然是相同的错误,我是新来的,从几天得到这个错误.请帮助解决这个问题.

StackTrace:

第一个月

主类

  1. @SpringBootApplication
  2. //@ComponentScan(basePackages = {"org.simplilearn"})
  3. //@EntityScan(basePackages = "org.simplilearn")
  4. public class MedicareApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MedicareApplication.class, args);
  7. }
  8. }

字符串

用户服务

  1. @Service
  2. public class UserService {
  3. @Autowired
  4. private UserRepository userRepo;
  5. // Save the User to the database(POST METHOD)
  6. public User saveUser(User user)
  7. {
  8. return userRepo.save(user);
  9. }
  10. //Fetch the User from Database(GET METHOD)
  11. public List<User> getUsers(){
  12. return userRepo.findAll();
  13. }
  14. public User getUserById(int id) {
  15. return userRepo.findById(id).orElse(null);
  16. }
  17. //Delete the User from Database (DELETE METHOD)
  18. public String deleteUserById(int id) {
  19. userRepo.deleteById(id);
  20. return "Data" +id+ "Deleted Successfully!";
  21. }
  22. //Join the User and Product Table
  23. public List<OrderResponse> getUserProductJoinInfos(){
  24. return userRepo.joinUserProductTable();
  25. }
  26. }

UserRepository

  1. @Repository
  2. public interface UserRepository extends JpaRepository<User, Integer> {
  3. @Query("SELECT new org.simplilearn.entity.OrderResponse(u.UserId,u.name,u.email, u.mobile, u.address, p.medicinename, p.seller"+ ", p.price, p.description, p.quantity, p.orderDateTime) FROM User u JOIN u.products p")
  4. public List<OrderResponse> joinUserProductTable();
  5. }

用户实体

  1. @Entity
  2. public class User {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private int UserId;
  6. private int age;
  7. private String name;
  8. private String email;
  9. private String gender;
  10. private String address;
  11. private String mobile;
  12. @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  13. @JoinTable(name = "USER_PRODUCT_TABLE", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "UserId") }, inverseJoinColumns = { @JoinColumn(name = "product_id", referencedColumnName = "pid") })
  14. private List<Product> products;
  15. public User() {
  16. }
  17. public User( int age, String name, String email, String gender, String address, String mobile, List<Product> products) {
  18. super();
  19. this.age = age;
  20. this.name = name;
  21. this.email = email;
  22. this.gender = gender;
  23. this.address = address;
  24. this.mobile = mobile;
  25. this.products = products;
  26. }
  27. public int getUserId() {
  28. return UserId;
  29. }
  30. public void setUserId(int userId) {
  31. UserId = userId;
  32. }
  33. public int getAge() {
  34. return age;
  35. }
  36. public void setAge(int age) {
  37. this.age = age;
  38. }
  39. public String getName() {
  40. return name;
  41. }
  42. public void setName(String name) {
  43. this.name = name;
  44. }
  45. public String getEmail() {
  46. return email;
  47. }
  48. public void setEmail(String email) {
  49. this.email = email;
  50. }
  51. public String getGender() {
  52. return gender;
  53. }
  54. public void setGender(String gender) {
  55. this.gender = gender;
  56. }
  57. public String getAddress() {
  58. return address;
  59. }
  60. public void setAddress(String address) {
  61. this.address = address;
  62. }
  63. public String getMobile() {
  64. return mobile;
  65. }
  66. public void setMobile(String mobile) {
  67. this.mobile = mobile;
  68. }
  69. public List<Product> getProducts() {
  70. return products;
  71. }
  72. public void setProducts(List<Product> products) {
  73. this.products = products;
  74. }
  75. @Override
  76. public String toString() {
  77. return "User [UserId=" + UserId + ", age=" + age + ", name=" + name + ", email=" + email + ", gender=" + gender
  78. + ", address=" + address + ", mobile=" + mobile + ", products=" + products + "]";
  79. }
  80. }


我已经添加了@ XiamentScan,@ XiamtyScan在主类仍然是相同的错误,我是新来的,从几天得到这个错误.请帮助解决这个问题.

ui7jx7zq

ui7jx7zq1#

@SpringBootApplication里面已经有**@ WebentScan了,你可以将鼠标悬停在annotation上,看到列表,它会扫描Java下的com.example.myApplication**文件夹,如果你的组件不在这里,它就无法扫描。

如果你有任何错别字,我也会检查查询中的列名。

相关问题