簡體   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