[英]Spring Boot 2.1 EntityManagerFactory Bean could not be found
[英]Spring Boot - entityManagerFactory cannot be found
我知道這個問題在SO上已經問了很多遍了,但是我嘗試了所有建議,但都沒有奏效。
我想創建一個REST控制器,並創建一個數據源以連接到mysql數據庫實例。 但是,當我啟動spring boot應用程序時,出現以下錯誤:
當我將@EnableJPARepositories
添加到我的數據源配置類中時,錯誤更改為:
Consider defining a bean of type 'com.boot.myproject.investment.repository.MyRepository' in your configuration.
這是主要的類:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
控制器:
public class MyController {
@Autowired
private MyRepository repo;
//....
}
我的數據源配置類:
@Configuration
public class DatasourceConfiguration {
@Bean
@ConfigurationProperties("app.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
application.properties
:
app.datasource.url=jdbc:mysql://localhost:3306/myAppDB
app.datasource.driver-class-name=com.mysql.jdbc.Driver
app.datasource.username=root
app.datasource.password=
實體:
@Entity
@Table(name="MyEntity")
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
}
倉庫:
@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {
}
build.gradle
依賴項:
dependencies {
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.4.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.8.RELEASE'
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.5.0'
compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '2.0.9.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.44'
}
我嘗試了幾種不同的方法:
嘗試了BasicDataSource
而不是DataSource
:
BasicDataSource basicDataSource =新的BasicDataSource();
嘗試使用@Transactional
而不是@Repository
來注釋存儲庫類,盡管我認為這沒有關系。
我想知道我的配置出了什么問題,它給了我這種錯誤。
我有一個非常相似的配置,唯一的區別是我的DatasourceConfiguration
有一個LocalContainerEntitymanagerFactoryBean
。
除了其他注釋之外,還添加以下內容:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource )
{
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource( dataSource );
entityManagerFactory.setPackagesToScan( MyEntity.class.getPackage().getName() );
entityManagerFactory.setJpaVendorAdapter( new HibernateJpaVendorAdapter() );
return entityManagerFactory;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.