用java写一个帖子展示功能,包含service层、controller层,数据库表设计
时间: 2024-05-22 21:13:45 浏览: 11
数据库表设计:
CREATE TABLE `post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_id` int(11) NOT NULL,
`content` text NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_comment_post` (`post_id`),
CONSTRAINT `fk_comment_post` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
Service层:
public interface PostService {
List<Post> getAllPosts();
Post getPostById(int id);
void addPost(Post post);
void updatePost(Post post);
void deletePost(int id);
}
public interface CommentService {
List<Comment> getCommentsByPostId(int postId);
void addComment(Comment comment);
void updateComment(Comment comment);
void deleteComment(int id);
}
@Controller层:
@Controller
public class PostController {
@Autowired
private PostService postService;
@Autowired
private CommentService commentService;
@GetMapping("/")
public String getAllPosts(Model model) {
List<Post> posts = postService.getAllPosts();
model.addAttribute("posts", posts);
return "posts";
}
@GetMapping("/post/{id}")
public String getPostById(@PathVariable int id, Model model) {
Post post = postService.getPostById(id);
List<Comment> comments = commentService.getCommentsByPostId(id);
model.addAttribute("post", post);
model.addAttribute("comments", comments);
return "post";
}
@GetMapping("/addPost")
public String addPost(Model model) {
model.addAttribute("post", new Post());
return "addPost";
}
@PostMapping("/addPost")
public String addPost(@ModelAttribute Post post) {
postService.addPost(post);
return "redirect:/";
}
@GetMapping("/updatePost/{id}")
public String updatePost(@PathVariable int id, Model model) {
Post post = postService.getPostById(id);
model.addAttribute("post", post);
return "updatePost";
}
@PostMapping("/updatePost")
public String updatePost(@ModelAttribute Post post) {
postService.updatePost(post);
return "redirect:/";
}
@GetMapping("/deletePost/{id}")
public String deletePost(@PathVariable int id) {
postService.deletePost(id);
return "redirect:/";
}
@PostMapping("/addComment")
public String addComment(@ModelAttribute Comment comment) {
commentService.addComment(comment);
return "redirect:/post/" + comment.getPostId();
}
@GetMapping("/updateComment/{id}")
public String updateComment(@PathVariable int id, Model model) {
Comment comment = commentService.getCommentById(id);
model.addAttribute("comment", comment);
return "updateComment";
}
@PostMapping("/updateComment")
public String updateComment(@ModelAttribute Comment comment) {
commentService.updateComment(comment);
return "redirect:/post/" + comment.getPostId();
}
@GetMapping("/deleteComment/{id}")
public String deleteComment(@PathVariable int id) {
Comment comment = commentService.getCommentById(id);
int postId = comment.getPostId();
commentService.deleteComment(id);
return "redirect:/post/" + postId;
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)