java如何使用id获取存储在db中的post记录的“内容”
我正在尝试使用附加到这些记录的post id从h2数据库获取内容。我可以使用普通的findAll()提取结果,但是当我尝试使用Id时,我无法提取任何记录
这是我的控制器
@GetMapping("{id}")
PostDetails getPostDetails(@PathVariable("id") final Long id) {
return postService.getPostDetails(id);
}
这些是我的模型 我的后模特
@Data
@ToString
@Entity
@Table(name = "POST")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String content;
}
扩展postinfo的My Postdetails类
@Data
@ToString
public class PostDetails extends PostInfo {
private String content;
}
我的Postinfo类
@Data
@ToString
@Entity
public class PostInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
}
我的售后服务班
public interface PostService {
PostDetails createPost(@Valid CreatePostRequest createPostRequest);
Page<PostInfo>getPosts(final Pageable pageable);
PostDetails getPostDetails(Long id);
void deletePost(Long id);
}
我的存储库如下所示:
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
}
我的实现类
@Override
public PostDetails getPostDetails(Long id) {
return postRepository.findById(id).get();
}
我最终得不到任何数据。如何使用findById访问内容(来自PostDetails)。我一直在研究,运气不好。感谢您的帮助。提前谢谢
共 (0) 个答案