java - GET 请求返回 {} 但 db 表有条目(Spring boot+JPA+MySQL)
问题描述
我试图将表中的所有条目作为 JSON 对象接收,获取请求显示“200 OK”,但它返回 [{}]。
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "man")
public class User {
@Id
@GeneratedValue
@Column(name = "man_id")
private Integer id;
@Column(name = "man_name")
private String name;
@Column(name = "salary")
private Double salary;
@Column(name = "reg")
private LocalDateTime dateTime;
}
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDAO extends JpaRepository<User,Integer> {
}
服务层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserDAO userDAO;
@Autowired
public UserService(UserDAO userDAO) {
this.userDAO = userDAO;
}
public List<User> findAll(){
return userDAO.findAll();
}
}
控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/man")
public class UserControl {
private final UserService userService;
@Autowired
public UserControl(UserService userService) {
this.userService = userService;
}
@GetMapping("/all")
public List<User> findAll(){
return userService.findAll();
}
}
应用程序.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: onionnetwork
jpa:
hibernate.dll-auto: update
generate: true
show-sql: true
使用的依赖项: starter-web , stater-jpa , mysql 连接器
注意:我目前正在通过 YouTube 教程学习 Spring Boot,所以会有一些令人毛骨悚然的编程实践。如果你们可以帮助我提供评论和良好的资源来学习 Spring-boot,非常感谢。:)
解决方案
推荐阅读
- recursion - 如何使用尾递归在 Prolog 中反转整数?
- sql - 在 services.msc 下找不到 SQL Server“连接到服务器”SQL 服务
- android - 检查 Chromecast 是否可以播放视频文件
- json - 在c#中将JSON数据反序列化到一个类中
- java - 字符串子字符串故意删除字符 0
- amazon-web-services - 将“cloud-provider=aws”与 kubeadm 一起使用时,kube-controller-manager 无法启动
- tkinter - 如何在 Tkinter 窗口上显示图像?
- java - 按下退出键时取消表格选择
- r - 将图像从帧数据保存到R中的文件
- neo4j - 将 csv 加载到 neo4j 的分步说明