【问题标题】:Spring-boot default EntityManagerSpring-boot默认EntityManager
【发布时间】:2018-09-21 09:04:37
【问题描述】:

spring-boot 是否有默认的 EntityManager。我现在正在设置一个,但我注意到当我的项目加载时我看到了这个:

LocalContainerEntityManagerFactoryBean : 为持久性单元“默认”构建 JPA 容器 EntityManagerFactory

这是一个默认的 EntityManager,如果是,我该如何访问它?

提前谢谢你。

【问题讨论】:

  • 就在你的课堂上@Autowire

标签: spring-boot jpa hibernate-entitymanager


【解决方案1】:

使用spring-boot-starter-data-jpa 时,您只需使用application.properties 中的spring.datasource.{url, username, password, driver-class-name} 属性配置数据源。

如果您想使用像 H2 这样的内存数据库进行开发,那也不是必须的。只需将数据库作为依赖项包含在内。

一旦你这样做了,你应该能够将EntityManager 注入你的bean。

【讨论】:

    【解决方案2】:

    你可以使用@PersistenceContext注解将实体管理器注入你的spring bean:

    @PersistenceContext
    EntityManager em;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 2014-12-13
      • 2017-06-13
      • 2020-11-28
      • 2019-10-21
      • 2015-11-03
      • 2017-12-10
      相关资源
      最近更新 更多