首页 > 解决方案 > 数据未使用 H2 DB 从 Spring Boot 返回

问题描述

我在 H2 DB 中有一些记录,但数据没有使用 spring boot 从 H2 DB 返回,也没有错误。以下查询在后台执行。

 COURSE
=========
 COURSEID
 COURSENAME
 COURSEDESC
 CATEGORY

Hibernate: select course0_.course_id as course_i1_0_, course0_.category as category2_0_, course0_.course_desc as course_d3_0_, course0_.course_name as course_n4_0_ from course course0_

请找到下面的代码。

package com.example.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.model.Course;
import com.example.service.CourseService;

@RestController
public class CourseController {

    @Autowired
    private CourseService service;

    @RequestMapping("/all")
    public List<Course> allCourses(){

        Iterable<Course> i=service.getAllCourses();
        List<Course> c= new ArrayList<Course>();
        Iterator<Course> itr=i.iterator();
        while(itr.hasNext()){
            Course cn=itr.next();
            c.add(cn);
            System.out.println(cn);
        }
        return c;
    }
}

DAO接口:

package com.example.dao;


import org.springframework.data.repository.CrudRepository;

import com.example.model.Course;

public interface CourseDAO  extends CrudRepository<Course, Long>{

}

实体类:

package com.example.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Course")
public class Course {
    @Id
    private long courseId;
    private String courseName;
    private String courseDesc;
    private String category;

    public long getCourseId() {
        return courseId;
    }
    public void setCourseId(long courseId) {
        this.courseId = courseId;
    }
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public String getCourseDesc() {
        return courseDesc;
    }
    public void setCourseDesc(String courseDesc) {
        this.courseDesc = courseDesc;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }

}

服务等级:

package com.example.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.CourseDAO;
import com.example.model.Course;

@Service
public class CourseService {

    @Autowired  
    private CourseDAO courseDAO;

    public Iterable<Course> getAllCourses(){
        return courseDAO.findAll();
    }

}

应用程序属性:

server.port = 8090
spring.security.user.name=admin
spring.security.user.password=admin

# To See H2 Console in Browser:
# http://localhost:8080/h2-console
# Enabling H2 Console
spring.h2.console.enabled=false

# ===============================
# DB
# ===============================

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

# ===============================
# JPA / HIBERNATE
# ===============================

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

标签: springspring-bootspring-data

解决方案


推荐阅读