繁体   English   中英

如何在 Spring 引导上使用简单的 JPA

[英]How to use simple JPA on Spring Boot

我正在学习 Spring 引导并尝试测试 JPA

我采用了“ https://spring.io/guides/gs/accessing-data-jpa/ ”示例

我还尝试了一个只有 data-jpa 和 h2 的简单初始化项目

但我总是遇到同样的错误

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:

Property: driverclassname
Value: org.postgresql.Driver
Origin: "driverClassName" from property source "source"
Reason: Failed to load driver class org.postgresql.Driver in either of HikariConfig class loader or Thread context classloader

Action:

Update your application's configuration

我读过很多关于这个的帖子,但它总是带有很多不同的配置和解决方案

事实是,它只是一个从 0 开始的简单程序,取自 initializr 或入门课程,它应该可以在没有特定配置的情况下工作(我猜)

是缺少计算机配置还是我真的需要添加很多应用程序属性才能使其工作?

谢谢你

(对不起我的英语不好^^')

您共享的链接没有谈论数据库配置。

Spring 引导需要在引导时进行 DB 配置。

请在application.properties中添加以下配置

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

请参阅此处了解更多详细信息

将下一个属性添加到您的 application.properties 文件

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

在 src/main/resources 中添加一个 data.sql 文件,内容如下:

DROP TABLE IF EXISTS customer;

CREATE TABLE customer (
  id INT AUTO_INCREMENT  PRIMARY KEY,
  first_name VARCHAR(250) NOT NULL,
  last_name VARCHAR(250) NOT NULL
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM