主要就是实体没注入进去,或者什么没注入进入,这个具体要看日志提示信息.
spring data jpa 由于我用的是spring boot,所以我只说spring boot中的解决。需要在配置类的上面加上@EnableJpaRepositories(basePackages={"dao层对应的包路径"}),这样jpa的dao层就注入进来了。结果启动spring boot 时发现,又有 Not a managed type: class ******的错误,经查询发现少了jpa entity路径的配置,在配置类的头部加上标记:@EntityScan("entity对应的包路径")。对于spring boot 使用jpa,需要在目录下加上application.properties文件,如果是maven项目在resource目录下,里面是jpa的一些数据的配置例如:
spring.datasource.url=jdbc:mysql://192.168.1.206:3306/test1?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent_output=true
上述注解使用案列,
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration //标注一个类是配置类,spring boot在扫到这个注解时自动加载这个类相关的功能,比如前面的文章中介绍的配置AOP和拦截器时加在类上的Configuration @EnableAutoConfiguration() //启用自动配置 该框架就能够进行行为的配置,以引导应用程序的启动与运行, 根据导入的starter-pom 自动加载配置 @EnableJpaRepositories(basePackages={"com.dao"}) //@EnableJpaRepositories(basePackages={"dao层对应的包路径"}) @ComponentScan(value={"com.*","com.dao"})//扫描组件 @ComponentScan(value = "com.spriboot.controller") 配置扫描组件的路径 @SpringBootApplication //@EntityScan("entity对应的包路径") @EntityScan("com.entity") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }