6.博客系统| 点赞功能
时间: 2024-01-29 16:57:29 浏览: 115
博客系统中的点赞功能是指读者可以点击按钮来表示对一篇博客的喜爱或赞同,从而增加博客的曝光率和互动性。实现点赞功能的一种常见做法是在前端添加一个按钮,当用户点击按钮时,通过 AJAX 请求将点赞信息传递给后端,后端再将点赞信息存储到数据库中。同时,还可以对点赞进行限制,例如同一个用户只能点赞一次,或者点赞操作需要登录等。在展示博客时,可以根据点赞数进行排序,将点赞数高的博客显示在更显眼的位置。
相关问题
springboot编写博客系统的点赞功能
实现博客点赞功能的基本思路如下:
1. 定义点赞实体类
```java
@Entity
public class Like {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private Long blogId;
// getter, setter and constructor
}
```
2. 创建点赞表
```sql
CREATE TABLE `like` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`blog_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
```
3. 创建点赞服务类
```java
@Service
public class LikeService {
@Autowired
private LikeRepository likeRepository;
public void like(Long userId, Long blogId) {
Like like = new Like(userId, blogId);
likeRepository.save(like);
}
public void cancelLike(Long userId, Long blogId) {
likeRepository.deleteByUserIdAndBlogId(userId, blogId);
}
}
```
4. 创建点赞控制器
```java
@RestController
public class LikeController {
@Autowired
private LikeService likeService;
@PostMapping("/like")
public void like(@RequestParam Long userId, @RequestParam Long blogId) {
likeService.like(userId, blogId);
}
@PostMapping("/cancel-like")
public void cancelLike(@RequestParam Long userId, @RequestParam Long blogId) {
likeService.cancelLike(userId, blogId);
}
}
```
5. 在博客详情页面中添加点赞按钮
```html
<button onclick="like()">点赞</button>
<button onclick="cancelLike()">取消点赞</button>
<script>
function like() {
$.post("/like?userId=${userId}&blogId=${blogId}", function() {
alert("点赞成功!");
});
}
function cancelLike() {
$.post("/cancel-like?userId=${userId}&blogId=${blogId}", function() {
alert("取消点赞成功!");
});
}
</script>
```
以上就是实现博客点赞功能的基本步骤,具体实现还需要结合具体的业务需求进行调整。
springboot编写博客系统的点赞功能代码
这里提供一个简单的点赞功能代码示例,基于Spring Boot和JPA实现:
首先,我们需要创建一个实体类来表示博客文章:
```java
@Entity
public class BlogPost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
private int likes; // 点赞数
// 省略构造函数、getter和setter
}
```
然后,我们需要创建一个控制器来处理点赞操作:
```java
@RestController
@RequestMapping("/api/blogpost")
public class BlogPostController {
@Autowired
private BlogPostRepository blogPostRepository;
@PostMapping("/{id}/like")
public ResponseEntity<Void> likeBlogPost(@PathVariable Long id) {
Optional<BlogPost> optionalBlogPost = blogPostRepository.findById(id);
if (optionalBlogPost.isPresent()) {
BlogPost blogPost = optionalBlogPost.get();
blogPost.setLikes(blogPost.getLikes() + 1);
blogPostRepository.save(blogPost);
return ResponseEntity.ok().build();
} else {
return ResponseEntity.notFound().build();
}
}
}
```
在控制器中,我们定义了一个 `likeBlogPost` 方法来处理点赞操作。该方法接收一个博客文章的ID作为路径参数,并将点赞数加1,然后保存到数据库中。
最后,我们需要创建一个JPA仓库来处理数据库操作:
```java
@Repository
public interface BlogPostRepository extends JpaRepository<BlogPost, Long> {
}
```
这个仓库接口继承自 `JpaRepository`,它提供了一些常用的数据库操作方法,例如 `findById` 和 `save`。
现在,我们就可以使用这个简单的代码来实现博客文章的点赞功能了。当用户点击点赞按钮时,我们可以向 `api/blogpost/{id}/like` 发送一个POST请求,其中 `{id}` 是博客文章的ID。服务器将自动更新点赞数,并将其保存到数据库中。