才开始,我创建了一个spring data jpa 的基础程序,从vo层开始我就只是加了一个@Data 注解,我觉得已经够了,在我的service 层上添加了我的@Service注解,紧接着添加了@Autowired 装载了到层的bean,在dao层继承了 JpaRepository 接口,在我的Controller 层上添加了@Autowired 装载了我service层的接口,写完实现后开始跑动程序,程序起不来,报错了,
-
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 'productDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productDao' defined in com.item.jpadata.dao.ProductDao defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.item.jpadata.vo.Product
原因就是缺少了一个@Entity 的注解 ,我以为用@Data注解就够了,
@Entity:用来标志实体类,知名这是一个和数据库表映射的实体类
@Id注解指明这个属性映射为数据库的主键
@GeneratedValue注解默认使用主键生成方式为自增,hibernate会自动生成一个名为HIBERNATE_SEQUENCE的序列。
@Table实体类映射表表名称,如果不加这个注解会默认使用类名
@Column使用来映射属性名和字段名,不注解的时候hibernate会自动根据属性名字生成数据表的字段名。如属性name映射生成字段NAME;多字母属性如testName会自动映射为TEST_NAME。表明映射规则也是如此。