求大佬解答,跟着老师一步一步学,代码都一样,测试repository出现异常
来源:4-1 买家类目-dao(上)
水岭
2018-09-22
异常内容:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.imooc.weixindemo.repository.ProductCategoryRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
ProductCategory类:
package com.imooc.weixindemo.dataobject;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class ProductCategory {
@Id
@GeneratedValue
private Integer categoryId;
private String categoryName;
private Integer categoryType;
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Integer getCategoryType() {
return categoryType;
}
public void setCategoryType(Integer categoryType) {
this.categoryType = categoryType;
}
}
ProductCategoryRepository类:
package com.imooc.weixindemo.repository;
import com.imooc.weixindemo.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
}
package com.imooc.weixindemo.repository;
import com.imooc.weixindemo.dataobject.ProductCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
@Autowired
private ProductCategoryRepository productCategoryRepository;
@Test
public void findOneTest(){
Optional<ProductCategory> optionalProductCategory = productCategoryRepository.findById(1);
}
@Test
public void saveTest(){
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("女生最爱");
productCategory.setCategoryType(3);
productCategoryRepository.save(productCategory);
}
}
而且,老师写的:
public void findOneTest(){
//在我的环境中不支持这样写 ???
ProductCategory productCategory = repository.findOne(1);
}
写回答
2回答
-
ProductCategoryRepository 这个类写得有问题,你贴出来
042018-09-26 -
水岭
提问者
2018-09-23
廖师兄老师,我重新一步一步的看着视频对照着,发现是不是因为导入的包不一致?
但是,我按照老师的方法,导入的时候找不spring-boot-starter-data-jpa??
不清楚这个如何解决
032018-09-24
相似问题