spring data jpa 出现Not a managed type

来源:互联网 发布:linux 手动开启服务 编辑:程序博客网 时间:2024/05/21 03:25

         这几天看自己买的那本spring boot的书,学些spring boot,spring boot零配置还是挺好用的,当看到spring data jpa时,我想自己写一下,spring data jpa 还是挺好用的,其中的那个写jpa对应的dao层,只需要extends JpaRepository<实体, 实体对应的主键>,然后在接口上用@Query写对应的hql语句,这点我觉得快赶上mybatis的灵活性了,在其他层注入的时候发现注入不进去,经自己一中午的查资料和实践发现,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://IP:port/库
spring.datasource.username = 用户名
spring.datasource.password = 密码
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

0 0
原创粉丝点击