从弹簧工具套件连接到数据库时出错
Posted
技术标签:
【中文标题】从弹簧工具套件连接到数据库时出错【英文标题】:Getting error while connecting to database from spring tool suite 【发布时间】:2020-08-22 07:58:15 【问题描述】:创建名为 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration' 的 bean 时出错:通过构造函数参数 0 表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中定义名称为“dataSource”的 bean 创建时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.zaxxer.hikari.HikariDataSource]:工厂方法 'dataSource' 抛出异常;嵌套异常是 java.lang.IllegalStateException:无法加载驱动程序类:oracle.jdbc.driver.OracleDriver
pom.xml 文件 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<groupId>com.Projectdashboardtool</groupId>
<artifactId>Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency> -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
控制器类
@RestController
@RequestMapping("/project")
public class ProjectController
@Autowired
private ProjectRepository projectRepository;
@GetMapping
public List<Project> list()
//List<Project> project=new ArrayList<>();
return projectRepository.findAll();
@PostMapping
@ResponseStatus(HttpStatus.OK)
public void create(@RequestBody Project project)
projectRepository.save(project);
@GetMapping("/id")
public Project get(@PathVariable("id") long id)
return projectRepository.getOne(id);
模型类
package com.Projectdashboardtool.Project.Model;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@JsonIgnoreProperties("hiberanateLazyInitializer", "handler")
public class Project
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String dg_number;
private String project;
private String release_level;
private String release_area;
private String release_number;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
private Date planned_release_date;
private Date release_date;
public String getDg_number()
return dg_number;
public void setDg_number(String dg_number)
this.dg_number = dg_number;
public String getProject()
return project;
public void setProject(String project)
this.project = project;
public String getRelease_level()
return release_level;
public void setRelease_level(String release_level)
this.release_level = release_level;
public String getRelease_area()
return release_area;
public void setRelease_area(String release_area)
this.release_area = release_area;
public String getRelease_number()
return release_number;
public void setRelease_number(String release_number)
this.release_number = release_number;
public Date getPlanned_release_date()
return planned_release_date;
public void setPlanned_release_date(Date planned_release_date)
this.planned_release_date = planned_release_date;
public Date getRelease_date()
return release_date;
public void setRelease_date(Date release_date)
this.release_date = release_date;
public String getRelease_type()
return release_type;
public void setRelease_type(String release_type)
this.release_type = release_type;
private String release_type;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
@Override
public String toString()
return "Project [id=" + id + ", dg_number=" + dg_number + ", project=" + project + ", release_level="
+ release_level + ", release_area=" + release_area + ", release_number=" + release_number
+ ", planned_release_date=" + planned_release_date + ", release_date=" + release_date
+ ", release_type=" + release_type + "]";
application.properties file
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.url=jdbc:oracle:thin:@//hostname/servicename
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
【问题讨论】:
能否请您显示您的数据源 bean 类 @GauravDhiman : 你问的是更新的数据吗? 请提供一些关于您的问题的附加信息。和 cmets 秀一样,你的问题还有改进的余地。 你创建了数据源bean吗?如果是,请分享该类,否则分享您声明的数据源属性 @GauravDhiman:除了这些类之外,我还有邮件类和一个存储库。我正在尝试新的应用程序。如果我错过了什么,请指导我。 【参考方案1】:添加依赖:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.1.0</version>
</dependency>
正确的休眠方言 - org.hibernate.dialect.OracleDialect
应该可以工作。
【讨论】:
【参考方案2】:我认为您正在使用 DB oracle 的属性文件。从您的异常中,我可以看到 spring boot 无法加载 oracle 驱动程序。因此,从 Oracle 网站下载 ojdbc6.jar,或者如果您在本地系统中安装 oracle11g,您可以在 jdbc/lib 文件夹中找到 ojdbc。
将此添加到您的项目中,在eclipse上右键单击项目->构建路径->添加此jar。
所以假设你正在使用 spring boot 将这个依赖添加到你的 pom.xml
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>//add letest version
</dependency>
假设如果 maven 未检测到您的依赖项,请按照以下步骤操作:-
运行此命令
mvn install:install-file - Dfile=C:\Users\user\.m2\repository\com\oracle\ojdbc6\11.2.0\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
那么你就可以使用这个了。
【讨论】:
我有 oracle 18.3 版本并使用 ojdbc8 jar 并放置在构建路径中 嗨,你能分享你的数据库配置吗 spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect spring.datasource.url=jdbc:oracle:thin:@//主机名/servicename spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver 你的 Hibernate dialect 属性应该是 spring.jpa.database-platform = org.hibernate.dialect.OracleDialect 谢谢Thiru,改成OracleDriver后我可以连接了【参考方案3】:Central Maven 上提供了 Oracle JDBC 驱动程序。看看这个blog。您可以为 18.3 JDBC 驱动程序添加此 GAV。
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
【讨论】:
以上是关于从弹簧工具套件连接到数据库时出错的主要内容,如果未能解决你的问题,请参考以下文章
从 VBScript 连接到 Sybase 时出错 - 内部客户端库错误
从 SQL Server Management Studio 连接到数据库服务器时出错
从 Firebase Cloud Function 连接到 Stripe 时出错