附注:使用mongo DB进行此操作
首先是完全错误
2020-05-04 01:16:31.468 WARN 14412
--- [ main] ConfigServletWebServerApplicationContext :
Exception encountered during context initialization -
cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'productController':
Unsatisfied dependency expressed through field 'productService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'productServiceImpl':
Unsatisfied dependency expressed through field 'productRepo';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'productRepo':
Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException:
No property id found for type Product!
public class Product {
@Id
private int ProductId;
// and other variables plus setters and getters
@Repository
public interface ProductRepo extends MongoRepository<Product, String> {
// methods
}
public interface ProductService {
// methods
}
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductRepo productRepo;
// implementations
}
尝试将product
类更改为:
@Entity
public class Product {
@Id
private String productId; // MongoRepository<Product, String>
// methods
}
在product
类中,缺少@entity
批注,并且productID
应该是存储库中指定的string
。
或者更改为extends mongorepository
。