java 点赞取消点赞
时间: 2024-01-12 18:22:30 浏览: 97
以下是Java点赞和取消点赞的示例代码:
1. 用户点赞功能:
```java
@ApiOperation(value = "用户点赞功能", notes = "用户点赞功能")
@PostMapping("/like")
public LexJSONResult like(String id, String videoId, String videoCreatedId) {
userService.like(id, videoId, videoCreatedId);
return LexJSONResult.ok();
}
```
2. 用户取消点赞功能:
```java
@ApiOperation(value = "用户取消点赞功能", notes = "用户取消点赞功能")
@PostMapping("/unlike")
public LexJSONResult unlike(String id, String videoId, String videoCreatedId) {
userService.unlike(id, videoId, videoCreatedId);
return LexJSONResult.ok();
}
```
相关问题
java点赞_java实现点赞功能示例
Java实现点赞功能的示例:
1. 创建一个点赞表,包含用户ID和被点赞的ID。
```
CREATE TABLE `like_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT '用户ID',
`liked_id` int(11) NOT NULL COMMENT '被点赞的ID',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_liked_id` (`liked_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='点赞表';
```
2. 创建点赞服务,提供点赞和取消点赞的接口。
```
@Service
public class LikeService {
private final LikeMapper likeMapper;
@Autowired
public LikeService(LikeMapper likeMapper) {
this.likeMapper = likeMapper;
}
/**
* 点赞
*
* @param userId 用户ID
* @param likedId 被点赞的ID
*/
public void like(int userId, int likedId) {
Like like = new Like();
like.setUserId(userId);
like.setLikedId(likedId);
likeMapper.insert(like);
}
/**
* 取消点赞
*
* @param userId 用户ID
* @param likedId 被点赞的ID
*/
public void unlike(int userId, int likedId) {
LikeExample example = new LikeExample();
example.createCriteria()
.andUserIdEqualTo(userId)
.andLikedIdEqualTo(likedId);
likeMapper.deleteByExample(example);
}
}
```
3. 创建点赞控制器,处理点赞和取消点赞的请求。
```
@RestController
@RequestMapping("/like")
public class LikeController {
private final LikeService likeService;
@Autowired
public LikeController(LikeService likeService) {
this.likeService = likeService;
}
/**
* 点赞
*
* @param userId 用户ID
* @param likedId 被点赞的ID
* @return
*/
@PostMapping("/like")
public ResultVO like(@RequestParam("userId") int userId,
@RequestParam("likedId") int likedId) {
likeService.like(userId, likedId);
return ResultVO.success();
}
/**
* 取消点赞
*
* @param userId 用户ID
* @param likedId 被点赞的ID
* @return
*/
@PostMapping("/unlike")
public ResultVO unlike(@RequestParam("userId") int userId,
@RequestParam("likedId") int likedId) {
likeService.unlike(userId, likedId);
return ResultVO.success();
}
}
```
java如何实现点赞功能
点赞功能可以通过在Java后端实现,具体步骤如下:
1. 创建一个点赞表(比如`likes`表),其中需要包含以下字段:
- 点赞ID(如`like_id`)
- 点赞用户ID(如`user_id`)
- 被点赞内容ID(如`content_id`)
- 点赞时间(如`like_time`)
2. 在Java后端实现点赞操作,可以编写一个`LikesService`服务类,在该类中定义以下方法:
```java
// 点赞
public void like(int userId, int contentId) {
// 判断用户是否已经点过赞
if (isLiked(userId, contentId)) {
throw new RuntimeException("You have liked this content before.");
}
// 插入点赞记录
Likes like = new Likes(userId, contentId, new Date());
likesDao.insert(like);
}
// 取消点赞
public void unlike(int userId, int contentId) {
likesDao.delete(userId, contentId);
}
// 判断用户是否已经点过赞
public boolean isLiked(int userId, int contentId) {
Likes like = likesDao.select(userId, contentId);
return like != null;
}
```
在上面的代码中,`likesDao`是一个`LikesDao`对象,用于操作点赞表。
3. 在前端页面中,通过调用后端API实现点赞功能。比如,可以编写一个`like`函数,用于向后端发送点赞请求:
```javascript
function like(userId, contentId) {
$.ajax({
url: "/api/likes/like",
method: "POST",
data: {userId: userId, contentId: contentId},
success: function(data) {
alert("You have liked this content.");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
```
在上面的代码中,`/api/likes/like`是后端API的路径,`userId`和`contentId`是需要点赞的用户ID和内容ID。
4. 在前端页面中,通过调用后端API实现取消点赞功能。比如,可以编写一个`unlike`函数,用于向后端发送取消点赞请求:
```javascript
function unlike(userId, contentId) {
$.ajax({
url: "/api/likes/unlike",
method: "POST",
data: {userId: userId, contentId: contentId},
success: function(data) {
alert("You have unliked this content.");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
```
在上面的代码中,`/api/likes/unlike`是后端API的路径,`userId`和`contentId`是需要取消点赞的用户ID和内容ID。
以上就是Java实现点赞功能的基本步骤,需要根据具体的项目需求进行适当的修改。
阅读全文