我正在尝试访问Spring框架中的MySQL数据库,但是我收到以下错误。
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
这是我的代码:
pom.xml依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc.mysql://localhost:3306/geektextdatabase
# Username and password
spring.datasource.username=root
spring.datasource.password=littlegraycells
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.tomcat.test-while-idle= true
spring.datasource.tomcat.validation-query= SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).
# Show or not log for each sql query
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
adminportalapplication.java
package com.adminportal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class AdminportalApplication {
public static void main(String[] args) {
SpringApplication.run(AdminportalApplication.class, args);
}
}
有人可以帮助我,告诉我我错过了什么吗?我错过了任何依赖吗?我使用的是Java 9,我不知道这是否会造成麻烦。
答案 0 :(得分:0)
查看错误,有一个Spring配置类HibernateJpaAutoConfiguration(它位于依赖项的类路径中),它依赖于未找到的数据源bean
@Autowired
private DataSource dataSource;
因此,简而言之,您的DataSource bean是定义的吗?如果没有在配置文件中定义(或在AdminportalApplication.java中)
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
答案 1 :(得分:-1)
您没有注册一个名为DataSource
的bean,它实际上代表了一个连接到您数据库的bean,您可以在线找到很多教程如何在您的内部设置DataSource
bean Spring
中的上下文,此处为one。我理解它可能不是最好的例子之一,但正如我所说,网上有很多它们,你可能找到最适合你的那个