我想创建一个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'
}
-
我尝试从CL(./gradlew--refresh dependencies)和eclipse刷新项目。
-
确保我的毕业生不存在与hibernate相关的依赖关系
-
尝试
BasicDataSource
DataSource
:
-
尝试
@Transactional
而不是
@Repository
注释repo类,尽管我认为它与此无关。
我想知道我的配置有什么问题,它给了我这样的错误。