spring - 数据未使用 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
解决方案
推荐阅读
- yaml - 在 YAML 转储时保持源未格式化
- suitecrm - 如何处理多团队-In Suite CRM
- kubernetes - 为什么在 k8s 上启用服务拓扑会出错?
- kubernetes - 如何在多个 pod 上安装相同的持久卷?
- python - tf.data.Dataset.from_generator - TypeError:如果浅结构是一个序列,输入也必须是一个序列
- c# - ASP.NET Core 中真正的无缓冲文件上传?
- javascript - 空白:在 Vue 中将文本作为道具传递时,预换行不起作用
- java - 使用预定义值创建 Java 数据集的更快方法
- ios - 在快速编程语言中解码 JSON 数据时遇到问题
- mysql - 从连接池中“清理”的 MySQL 连接是否可以安全删除?