Не грузятся данные из БД H2 с помощью Spring JPA

85
23 марта 2022, 06:20

Пробую выгрузить все данные из БД H2 с помощью Spring JPA , после перехода по ссылке http://localhost:8080/greeting на выходе получаются пустые значения. Как сделать так, чтобы получить все данные из таблицы?

Контроллер:

package RestExample.MainPack.Controller;
import RestExample.MainPack.model.salespointdo;
import RestExample.MainPack.repos.SalesPointRepos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
    @Autowired
    SalesPointRepos salesPointRepos;

    @GetMapping("/greeting")
    @ResponseBody
    public String greeting() {

        salespointdo ss= new salespointdo();
        Iterable<salespointdo> allSP=salesPointRepos.findAll();
        StringBuilder sb= new StringBuilder();
        allSP.forEach(sp->sb.append(sp+"<br>"));

        return  allSP.toString();
    }
}

Entity:

    package RestExample.MainPack.model;
import javax.persistence.*;
@Entity
@Table(name = "salespointdo")
public class salespointdo {
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;
 @Column(name = "name")
 private String name;
 @Column(name = "city")
 private String city;
 @Column(name = "address")
 private String address;
    public salespointdo() {
    }
    public Long getID() {
        return id;
    }
    public void setID(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "City: "+getCity()+" "+"Address: "+getAddress();
    }
}

Repository:

    package RestExample.MainPack.repos;
import RestExample.MainPack.model.salespointdo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SalesPointRepos extends CrudRepository<salespointdo,Long> {

}

SpringBoot:

package RestExample.MainPack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartRest {
    public static void main(String[] args) {
        SpringApplication.run(StartRest.class, args);

    }
}

application.properties:

spring.h2.console.enabled=true

spring.datasource.url=jdbc:h2:./SalesPoint
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.platform=h2

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

Выше привел логи из SpringBoot и таблицу H2 и результат выполнения

Answer 1

application.properties

# To See H2 Console in Browser:
# http://localhost:8080/h2-console
# Enabling H2 Console
spring.h2.console.enabled=true
#
## ===============================#
## DB                             #
## ===============================#
#
spring.datasource.url=jdbc:h2:mem:testdatabase
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

Попробуй так

READ ALSO
Показать все обьекты из файла через swing

Показать все обьекты из файла через swing

Я пытаюсь сделать обработку файла с помощью swingТо есть показать весь файл, удалить строку по фамилии, найти строку с помощью фамилии, записать...

75
Сортировка слов по количеству символов от большего к меньшему

Сортировка слов по количеству символов от большего к меньшему

Суть задачи такова, что есть какой-то список слов к примеру: "Hi" + "Group" + "Java" + "stacks"И в исходном варианте он должен выглядеть примерно вот так:...

136
Создать двумерный массив из строки

Создать двумерный массив из строки

есть строка в файле [[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 3, 0, 0], [0, 0, 4, 0, 0], [0, 0, 0, 0, 0]] из javautil

91
Указать на activity в классе Application

Указать на activity в классе Application

У меня есть класс Application, от него наследуются все активити, мне нужно внедрить в него в onCreate рекламу UnityТам есть метод Unityads

75