我最近开始学习Java和spring Boot ,我在youtube上看了一个关于如何构建一个小项目的教程,但是当它到了检查数据库连接的时候,我得到了这个消息:
customer=# \dt
Did not find any relations.
而且因为我对java和spring Boot 非常不熟悉,我发现自己在没有明确工作流程的情况下进行调试。如果有人能指引我正确的方向,我会非常感激。
下面是我的代码:
Main.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
@SpringBootApplication
@RestController
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@GetMapping("/")
public GreetingResponse greeting(){
GreetingResponse greetingString = new GreetingResponse(
"Hello",
List.of("Python", "Python", "Python"),
new Person("Batool", 28, 500_000)
);
return greetingString;
}
record Person(String name, int age, double savings){
}
record GreetingResponse(
String greeting,
List<String> favProgrammingLanguages,
Person person
){
}
客户.java
import java.util.Objects;
import jakarta.persistence.*;
@Entity
public class Customer {
@Id
@SequenceGenerator(name = "customer_id_sequence", sequenceName = "customer_id_sequence")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_id_sequence")
private Integer id;
private String name;
private String email;
private Integer age;
public Customer(Integer id, String name, String email, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public Integer getAge() {
return age;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id) && Objects.equals(name, customer.name) && Objects.equals(email, customer.email) && Objects.equals(age, customer.age);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email, age);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
docker-compose.yml
services:
db:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: <>
POSTGRES_PASSWORD: <>
PGDATA: /data/postgres
volumes:
- db:/data/postgres
ports:
- "5332:5432"
networks:
- db
restart: unless-stopped
networks:
db:
driver: bridge
volumes:
db:
application.yml
server:
port: 8080
spring:
datasource:
platform: postgres
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://db:5332/customer
username: <>
password: <>
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format-sql: true
show-sql: true
main:
web-development-type: servlet
1条答案
按热度按时间f4t66c6m1#
您告诉Spring连接到主机名为
db
的数据库服务器。但是db
主机名只能在docker-compose中创建的docker桥网络中工作。如果您将Sping Boot 应用程序作为docker-compose文件中的另一个服务运行,则该地址将有效。但你看起来并没有这么做。您很可能需要将Spring config文件中的datasource URL更改为:
因为docker-compose文件中的这一行暴露了本地计算机上端口
5332
或localhost
上的数据库服务: