Compare commits
3
Commits
dd63e74094
...
b4760cab96
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4760cab96 | ||
|
|
d3dd6982ea | ||
|
|
5770f23dcd |
@@ -35,3 +35,10 @@
|
|||||||
```mysql
|
```mysql
|
||||||
CREATE UNIQUE INDEX idx_userId_blogId ON thumb (userId, blogId);
|
CREATE UNIQUE INDEX idx_userId_blogId ON thumb (userId, blogId);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
2. 为什么要在 `BlogServiceImpl` 导入 `ThumbService` 上加 `@Lazy`
|
||||||
|
|
||||||
|
答:
|
||||||
|
- 博客服务中需要使用点赞服务查询博客对应的点赞情况列表以返回对应视图。
|
||||||
|
- 点赞服务中需要使用博客服务更新单条博客的点赞计数器。
|
||||||
|
- 两个服务之间产生了**循环依赖**,Spring Boot 2.6 以后的版本默认不允许循环依赖,这里可以使用懒加载的方式避免这一问题。
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package cn.hezhaohui.thumb.controller;
|
||||||
|
|
||||||
|
import cn.hezhaohui.thumb.common.BaseResponse;
|
||||||
|
import cn.hezhaohui.thumb.common.ResultUtils;
|
||||||
|
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||||
|
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||||
|
import cn.hezhaohui.thumb.service.BlogService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("blog")
|
||||||
|
public class BlogController {
|
||||||
|
@Resource
|
||||||
|
private BlogService blogService;
|
||||||
|
|
||||||
|
@GetMapping("/get")
|
||||||
|
public BaseResponse<BlogVO> get(long blogId, HttpServletRequest request) {
|
||||||
|
BlogVO blogVO = blogService.getBlogVOById(blogId, request);
|
||||||
|
return ResultUtils.sucess(blogVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("list")
|
||||||
|
public BaseResponse<List<BlogVO>> list(HttpServletRequest request) {
|
||||||
|
List<Blog> blogList = blogService.list();
|
||||||
|
List<BlogVO> blogVOList = blogService.getBlogVOList(blogList, request);
|
||||||
|
return ResultUtils.sucess(blogVOList);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package cn.hezhaohui.thumb.model.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class BlogVO {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String coverImg;
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
private Integer thumbCount;
|
||||||
|
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
private Boolean hasThumb;
|
||||||
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
package cn.hezhaohui.thumb.service;
|
package cn.hezhaohui.thumb.service;
|
||||||
|
|
||||||
import cn.hezhaohui.thumb.model.entity.Blog;
|
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||||
|
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 23117
|
* @author 23117
|
||||||
@@ -10,4 +14,8 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||||||
*/
|
*/
|
||||||
public interface BlogService extends IService<Blog> {
|
public interface BlogService extends IService<Blog> {
|
||||||
|
|
||||||
|
|
||||||
|
BlogVO getBlogVOById(long blogId, HttpServletRequest request);
|
||||||
|
|
||||||
|
List<BlogVO> getBlogVOList(List<Blog> blogList, HttpServletRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,5 +11,5 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||||||
*/
|
*/
|
||||||
public interface UserService extends IService<User> {
|
public interface UserService extends IService<User> {
|
||||||
|
|
||||||
public User getLoginUser(HttpServletRequest request);
|
User getLoginUser(HttpServletRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,27 @@
|
|||||||
package cn.hezhaohui.thumb.service.impl;
|
package cn.hezhaohui.thumb.service.impl;
|
||||||
|
|
||||||
|
import cn.hezhaohui.thumb.model.entity.Thumb;
|
||||||
|
import cn.hezhaohui.thumb.model.entity.User;
|
||||||
|
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||||
|
import cn.hezhaohui.thumb.service.ThumbService;
|
||||||
|
import cn.hezhaohui.thumb.service.UserService;
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import cn.hezhaohui.thumb.model.entity.Blog;
|
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||||
import cn.hezhaohui.thumb.service.BlogService;
|
import cn.hezhaohui.thumb.service.BlogService;
|
||||||
import cn.hezhaohui.thumb.mapper.BlogMapper;
|
import cn.hezhaohui.thumb.mapper.BlogMapper;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 23117
|
* @author 23117
|
||||||
* @description 针对表【blog】的数据库操作Service实现
|
* @description 针对表【blog】的数据库操作Service实现
|
||||||
@@ -15,6 +31,63 @@ import org.springframework.stereotype.Service;
|
|||||||
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||||
implements BlogService{
|
implements BlogService{
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
@Lazy
|
||||||
|
private ThumbService thumbService;
|
||||||
|
|
||||||
|
public BlogServiceImpl(UserService userService) {
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlogVO getBlogVOById(long blogId, HttpServletRequest request) {
|
||||||
|
Blog blog = this.getById(blogId);
|
||||||
|
User loginUser = userService.getLoginUser(request);
|
||||||
|
return this.getBlogVO(blog, loginUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<BlogVO> getBlogVOList(List<Blog> blogList, HttpServletRequest request) {
|
||||||
|
User loginUser = userService.getLoginUser(request);
|
||||||
|
Map<Long, Boolean> blogIdHasThumbMap = new HashMap<>();
|
||||||
|
if (ObjUtil.isNotEmpty(loginUser)) {
|
||||||
|
// Blog Id
|
||||||
|
Set<Long> blogIdSet = blogList.stream().map(Blog::getId).collect(Collectors.toSet());
|
||||||
|
// Get Thumb-up Data
|
||||||
|
List<Thumb> thumbList = thumbService.lambdaQuery()
|
||||||
|
.eq(Thumb::getUserid, loginUser.getId())
|
||||||
|
.in(Thumb::getBlogId, blogIdSet)
|
||||||
|
.list();
|
||||||
|
thumbList.forEach(blogThumb -> blogIdHasThumbMap.put(blogThumb.getBlogId(), true));
|
||||||
|
}
|
||||||
|
return blogList.stream()
|
||||||
|
.map(blog -> {
|
||||||
|
BlogVO blogVO = BeanUtil.copyProperties(blog, BlogVO.class);
|
||||||
|
blogVO.setHasThumb(blogIdHasThumbMap.get(blog.getId()));
|
||||||
|
return blogVO;
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private BlogVO getBlogVO(Blog blog, User loginUser) {
|
||||||
|
BlogVO blogVO = new BlogVO();
|
||||||
|
BeanUtil.copyProperties(blog, blogVO);
|
||||||
|
|
||||||
|
if (loginUser == null) {
|
||||||
|
return blogVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thumb thumb = thumbService.lambdaQuery()
|
||||||
|
.eq(Thumb::getUserid, loginUser.getId())
|
||||||
|
.eq(Thumb::getBlogId, blog.getId())
|
||||||
|
.one();
|
||||||
|
blogVO.setHasThumb(thumb != null);
|
||||||
|
|
||||||
|
return blogVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user