
Я использую eclipse и Spring Boot, мой проект подключен к базе данных Postgres.
Соединение с моей базой данных работает нормально.
Программа должна была получать данные из HTML-формы и сохранять их. он в моей базе данных.
Это мой объект:
Код: Выделить всё
package entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "xromy")
public class xRomy {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
// muss wohl public sein
public Integer id;
private String name;
private Integer alter;
public xRomy() {
}
public xRomy(String name, Integer alter) {
this.name = name;
this.alter = alter;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAlter() {
return alter;
}
public void setAlter(Integer alter) {
this.alter = alter;
}
@Override
public String toString() {
return "xRomy{" + "\n id=" + id + "\n name=" + name + "\n alter=" + alter + "}";
}
}
Код: Выделить всё
package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import entity.xRomy;
@Repository
public interface RomyRep extends JpaRepository {
}
Код: Выделить всё
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import entity.xRomy;
import repository.RomyRep;
@RestController
public class ControllerRom {
@Autowired
RomyRep romyRep;
// zeigt Tabelle
@GetMapping("/testView")
public ModelAndView home() {
ModelAndView m = new ModelAndView("tabelle");
m.addObject("Test", romyRep.findAll());
return m;
}
@GetMapping("/{id}")
public ModelAndView Daten(xRomy xromy) {
ModelAndView m = new ModelAndView("daten");
m.addObject("Daten", romyRep.findById(xromy.id));
return m;
}
}
Код: Выделить всё
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import entity.xRomy;
import jakarta.validation.Valid;
import repository.RomyRep;
@RestController
public class ConttrollNeuDs {
@Autowired
RomyRep romyRep;
// um neuen Eintrag eizugeben
@GetMapping("/neuerEintrag")
public ModelAndView getName() {
ModelAndView model = new ModelAndView("newName");
model.addObject("xromy", new xRomy());
return model;
}
// sollte neuen Eintrag speichern
@PostMapping("/save")
public String postNew(@Valid xRomy xromy, BindingResult result, ModelAndView model) {
model.addObject("xromy", xromy);
xRomy Xromy = new xRomy();
Xromy.setName(xromy.getName());
Xromy.setAlter(xromy.getAlter());
romyRep.save(Xromy);
return "redirect:/testView";
}
}
Код: Выделить всё
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeControll {
@GetMapping("/")
public ModelAndView home() {
ModelAndView m = new ModelAndView("hallo");
return m;
}
}
Код: Выделить всё
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.3.2
com.example
hilfe
0.0.1-SNAPSHOT
hilfe
Demo project for Spring Boot
17
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.postgresql
postgresql
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-maven-plugin
Код: Выделить всё
spring.application.name=hilfe
#Datenbank Vb
spring.datasource.url=jdbc:postgresql://localhost:5433/romy
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.postgresql.Driver
#
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
#
spring.mvc.static-path-pattern=/resources/**
#test
spring.jpa.generate-ddl=true
Я попробовал почти все и прочитал много здесь писали о подобных проблемах, но не смогли найти решения.
Подробнее здесь: https://stackoverflow.com/questions/787 ... ted-spring
Мобильная версия