在vscode上使用spring boot框架搭建一个web评论系统,采用SQLServer数据库,实现用户注册和登录功能,浏览、发布、更新、删除、检索、评论和回复功能、分页和排序功能、通知功能,给出完整的、实用的控制器和服务器代码
时间: 2024-06-09 16:08:41 浏览: 177
很高兴听到您的需求。下面是使用Spring Boot框架搭建Web评论系统的步骤和代码。
步骤1:创建Spring Boot项目
首先,在VSCode中创建一个空的Spring Boot项目。在终端窗口中输入以下命令:
```
$ mkdir comment-system
$ cd comment-system
$ spring init --dependencies=web,data-sqlserver --language=java --build=gradle .
```
这将创建一个使用Gradle构建工具的Spring Boot项目,并添加Web和SQL Server依赖项。
步骤2:配置SQL Server数据库
在application.properties文件中配置SQL Server数据库连接信息。在src/main/resources文件夹下创建application.properties文件,并添加以下内容:
```
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=comment_system
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
```
注意:请根据您的SQL Server服务器和数据库名称更新上述信息。
步骤3:创建实体类
创建一个Comment实体类,用于表示评论和回复。在src/main/java/com/example/comment_system/model/文件夹下创建Comment.java文件,并添加以下代码:
```java
package com.example.comment_system.model;
import java.util.Date;
import java.util.List;
public class Comment {
private Long id;
private String content;
private Date createTime;
private Long userId;
private Long parentId;
private List<Comment> replies;
// getters and setters
}
```
注意:在此示例中,评论和回复都使用Comment实体类表示。parentId属性用于表示回复所属的评论ID,replies属性用于表示评论的回复列表。
步骤4:创建数据访问层
创建一个CommentRepository接口,用于定义数据访问方法。在src/main/java/com/example/comment_system/repository/文件夹下创建CommentRepository.java文件,并添加以下代码:
```java
package com.example.comment_system.repository;
import java.util.List;
import com.example.comment_system.model.Comment;
public interface CommentRepository {
Comment save(Comment comment);
List<Comment> findAll();
List<Comment> findByParentId(Long parentId);
Comment findById(Long id);
void deleteById(Long id);
}
```
步骤5:创建业务逻辑层
创建一个CommentService接口,用于定义业务逻辑方法。在src/main/java/com/example/comment_system/service/文件夹下创建CommentService.java文件,并添加以下代码:
```java
package com.example.comment_system.service;
import java.util.List;
import com.example.comment_system.model.Comment;
public interface CommentService {
Comment addComment(Comment comment);
Comment addReply(Comment reply);
List<Comment> getComments(Long parentId);
Comment getComment(Long id);
void deleteComment(Long id);
}
```
步骤6:创建控制器
创建一个CommentController控制器,用于处理HTTP请求。在src/main/java/com/example/comment_system/controller/文件夹下创建CommentController.java文件,并添加以下代码:
```java
package com.example.comment_system.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.comment_system.model.Comment;
import com.example.comment_system.service.CommentService;
@RestController
@RequestMapping("/comments")
public class CommentController {
@Autowired
private CommentService commentService;
@PostMapping
public ResponseEntity<Comment> addComment(@RequestBody Comment comment) {
Comment result = commentService.addComment(comment);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
@PostMapping("/{parentId}")
public ResponseEntity<Comment> addReply(@PathVariable Long parentId, @RequestBody Comment reply) {
reply.setParentId(parentId);
Comment result = commentService.addReply(reply);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
@GetMapping("/{parentId}")
public ResponseEntity<List<Comment>> getComments(@PathVariable Long parentId) {
List<Comment> comments = commentService.getComments(parentId);
return new ResponseEntity<>(comments, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<Comment> getComment(@PathVariable Long id) {
Comment comment = commentService.getComment(id);
return new ResponseEntity<>(comment, HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteComment(@PathVariable Long id) {
commentService.deleteComment(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
```
注意:@RestController注解表示这是一个REST风格的控制器,@RequestMapping注解用于指定路由前缀,@PostMapping、@GetMapping和@DeleteMapping注解用于指定HTTP请求方法,并且使用@Autowired注解将CommentService注入到控制器中。
步骤7:创建实现类
创建一个CommentRepositoryImpl类,用于实现数据访问层方法。在src/main/java/com/example/comment_system/repository/impl/文件夹下创建CommentRepositoryImpl.java文件,并添加以下代码:
```java
package com.example.comment_system.repository.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.example.comment_system.model.Comment;
import com.example.comment_system.repository.CommentRepository;
@Repository
public class CommentRepositoryImpl implements CommentRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
private RowMapper<Comment> rowMapper = (rs, rowNum) -> {
Comment comment = new Comment();
comment.setId(rs.getLong("id"));
comment.setContent(rs.getString("content"));
comment.setCreateTime(rs.getDate("create_time"));
comment.setUserId(rs.getLong("user_id"));
comment.setParentId(rs.getLong("parent_id"));
return comment;
};
@Override
public Comment save(Comment comment) {
String sql = "INSERT INTO comments(content, create_time, user_id, parent_id) VALUES (?, ?, ?, ?)";
Object[] args = { comment.getContent(), comment.getCreateTime(), comment.getUserId(), comment.getParentId() };
jdbcTemplate.update(sql, args);
return comment;
}
@Override
public List<Comment> findAll() {
String sql = "SELECT * FROM comments";
return jdbcTemplate.query(sql, rowMapper);
}
@Override
public List<Comment> findByParentId(Long parentId) {
String sql = "SELECT * FROM comments WHERE parent_id = ?";
Object[] args = { parentId };
return jdbcTemplate.query(sql, args, rowMapper);
}
@Override
public Comment findById(Long id) {
String sql = "SELECT * FROM comments WHERE id = ?";
Object[] args = { id };
return jdbcTemplate.queryForObject(sql, args, rowMapper);
}
@Override
public void deleteById(Long id) {
String sql = "DELETE FROM comments WHERE id = ?";
Object[] args = { id };
jdbcTemplate.update(sql, args);
}
}
```
创建一个CommentServiceImpl类,用于实现业务逻辑层方法。在src/main/java/com/example/comment_system/service/impl/文件夹下创建CommentServiceImpl.java文件,并添加以下代码:
```java
package com.example.comment_system.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.comment_system.model.Comment;
import com.example.comment_system.repository.CommentRepository;
import com.example.comment_system.service.CommentService;
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
public Comment addComment(Comment comment) {
comment.setCreateTime(new Date());
return commentRepository.save(comment);
}
@Override
public Comment addReply(Comment reply) {
reply.setCreateTime(new Date());
return commentRepository.save(reply);
}
@Override
public List<Comment> getComments(Long parentId) {
List<Comment> comments = commentRepository.findByParentId(parentId);
for (Comment comment : comments) {
List<Comment> replies = getComments(comment.getId());
comment.setReplies(replies);
}
return comments;
}
@Override
public Comment getComment(Long id) {
Comment comment = commentRepository.findById(id);
List<Comment> replies = getComments(comment.getId());
comment.setReplies(replies);
return comment;
}
@Override
public void deleteComment(Long id) {
List<Comment> comments = getComments(id);
for (Comment comment : comments) {
commentRepository.deleteById(comment.getId());
}
commentRepository.deleteById(id);
}
}
```
步骤8:运行程序
在终端窗口中输入以下命令来运行程序:
```
$ ./gradlew bootRun
```
这将启动Spring Boot应用程序。现在,您可以通过浏览器或Postman等工具来测试评论系统API。例如,您可以使用以下URL来添加评论:
```
POST http://localhost:8080/comments
{
"content": "这是一个测试评论",
"userId": 1
}
```
您可以使用以下URL来添加回复:
```
POST http://localhost:8080/comments/{parentId}
{
"content": "这是一个测试回复",
"userId": 1
}
```
注意:请将{parentId}替换为要回复的评论的ID。
您可以使用以下URL来获取评论列表:
```
GET http://localhost:8080/comments/{parentId}
```
注意:请将{parentId}替换为要获取评论的ID。
您可以使用以下URL来获取评论的详细信息:
```
GET http://localhost:8080/comments/{id}
```
注意:请将{id}替换为要获取评论的ID。
您可以使用以下URL来删除评论:
```
DELETE http://localhost:8080/comments/{id}
```
注意:请将{id}替换为要删除评论的ID。
至此,您已经成功搭建了一个Web评论系统。希望这个示例对您有所帮助!
阅读全文