博客功能
根据功能设计添加属性
blog实体类
package com.wzh.springbootproject.entity;
import java.util.Objects;
/**
* 博客信息
*/
public class Blog {
/** ID */
private Integer id;
/** 标题 */
private String title;
/** 内容 */
private String content;
/** 简介 */
private String descr;
/** 封面 */
private String cover;
/** 标签 */
private String tags;
/** 发布人ID */
private Integer userId;
/** 发布日期 */
private String date;
/** 浏览量 */
private Integer readCount;
private Integer categoryId;
private String categoryName;
private String userName;
private User user;
private Integer likesCount;
private Boolean userLike; // 返回当前浏览的数据是否被当前登录的用户点赞
private Integer collectCount;
private Boolean userCollect;
public Integer getCollectCount() {
return collectCount;
}
public void setCollectCount(Integer collectCount) {
this.collectCount = collectCount;
}
public Boolean getUserCollect() {
return userCollect;
}
public void setUserCollect(Boolean userCollect) {
this.userCollect = userCollect;
}
public Boolean getUserLike() {
return userLike;
}
public void setUserLike(Boolean userLike) {
this.userLike = userLike;
}
public Integer getLikesCount() {
return likesCount;
}
public void setLikesCount(Integer likesCount) {
this.likesCount = likesCount;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Integer getReadCount() {
return readCount;
}
public void setReadCount(Integer readCount) {
this.readCount = readCount;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Blog)) return false;
Blog blog = (Blog) o;
return Objects.equals(getId(), blog.getId());
}
@Override
public int hashCode() {
return Objects.hash(getId());
}
}
BlogController
```java
package com.wzh.springbootproject.controller;
import com.wzh.springbootproject.common.Result;
import com.wzh.springbootproject.entity.Blog;
import com.wzh.springbootproject.service.BlogService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
/**
* 博客信息前端操作接口
**/
@RestController
@RequestMapping("/blog")
public class
BlogController {
@Resource
private BlogService blogService;
/**
* 新增
*/
@PostMapping("/add")
public Result add(@RequestBody Blog blog) {
blogService.add(blog);
return Result.success();
}
/**
* 删除
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
blogService.deleteById(id);
return Result.success();
}
/**
* 批量删除
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
blogService.deleteBatch(ids);
return Result.success();
}
/**
* 修改
*/
@PutMapping("/update")
public Result updateById(@RequestBody Blog blog) {
blogService.updateById(blog);
return Result.success();
}
@PutMapping("/updateReadCount/{blogId}")
public Result updateReadCount(@PathVariable Integer blogId) {
blogService.updateReadCount(blogId);
return Result.success();
}
/**
* 根据ID查询
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Blog blog = blogService.selectById(id);
return Result.success(blog);
}
/**
* 查询所有
*/
@GetMapping("/selectAll")
public Result selectAll(Blog blog) {
List<Blog> list = blogService.selectAll(blog);
return Result.success(list);
}
/**
* 分页查询
*/
@GetMapping("/selectPage")
public Result selectPage(Blog blog,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Blog> page = blogService.selectPage(blog, pageNum, pageSize);
return Result.success(page);
}
/**
* 分页查询当前用户的博客列表
*/
@GetMapping("/selectUser")
public Result selectUser(Blog blog,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Blog> page = blogService.selectUser(blog, pageNum, pageSize);
return Result.success(page);
}
/**
* 分页查询当前用户点赞的博客列表
*/
@GetMapping("/selectLike")
public Result selectLike(Blog blog,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Blog> page = blogService.selectLike(blog, pageNum, pageSize);
return Result.success(page);
}
/**
* 分页查询当前用户收藏的博客列表
*/
@GetMapping("/selectCollect")
public Result selectCollect(Blog blog,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Blog> page = blogService.selectCollect(blog, pageNum, pageSize);
return Result.success(page);
}
/**
* 分页查询当前用户评论的博客列表
*/
@GetMapping("/selectComment")
public Result selectComment(Blog blog,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Blog> page = blogService.selectComment(blog, pageNum, pageSize);
return Result.success(page);
}
/**
* 博客榜单
*/
@GetMapping("/selectTop")
public Result selectTop() {
List<Blog> list = blogService.selectTop();
return Result.success(list);
}
/**
* 博客推荐
*/
@GetMapping("/selectRecommend/{blogId}")
public Result selectRecommend(@PathVariable Integer blogId) {
Set<Blog> blogSet = blogService.selectRecommend(blogId);
return Result.success(blogSet);
}
}
BlogMapper
package com.wzh.springbootproject.mapper;
import com.wzh.springbootproject.entity.Blog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
* 操作blog相关数据接口
*/
public interface BlogMapper {
/**
* 新增
*/
int insert(Blog blog);
/**
* 删除
*/
int deleteById(Integer id);
/**
* 修改
*/
int updateById(Blog blog);
/**
* 根据ID查询
*/
Blog selectById(Integer id);
/**
* 查询所有
*/
List<Blog> selectAll(Blog blog);
@Select("select * from blog where user_id = #{userId}")
List<Blog> selectUserBlog(Integer userId);
@Update("update blog set read_count = read_count + 1 where id = #{blogId}")
void updateReadCount(Integer blogId);
List<Blog> selectLike(Blog blog);
List<Blog> selectCollect(Blog blog);
List<Blog> selectComment(Blog blog);
}
resource文件夹中写BlogMapper.xml实现
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wzh.springbootproject.mapper.BlogMapper">
<sql id="Base_Column_List">
id,title,content,descr,cover,tags,user_id,date,read_count,category_id
</sql>
<select id="selectAll" resultType="com.wzh.springbootproject.entity.Blog">
select
blog.*, category.name as categoryName, user.name as userName
from blog
left join category
on blog.category_id = category.id
left join user
on blog.user_id = user.id
<where>
<if test="title != null">and blog.title like concat('%', #{title}, '%')</if>
<if test="categoryName != null">and category.name like concat('%', #{categoryName}, '%')</if>
<if test="userName != null">and user.name like concat('%', #{userName}, '%')</if>
<if test="userId != null">and user.id = #{userId}</if>
</where>
order by id desc
</select>
<select id="selectById" resultType="com.wzh.springbootproject.entity.Blog">
select
blog.*, category.name as categoryName, user.name as userName
from blog
left join category
on blog.category_id = category.id
left join user
on blog.user_id = user.id
where blog.id = #{id}
</select>
<select id="selectLike" resultType="com.wzh.springbootproject.entity.Blog">
select blog.*, user.name as userName
from likes
left join blog
on likes.fid = blog.id
left join category
on blog.category_id = category.id
left join user
on blog.user_id = user.id
<where>
and likes.module = '博客'
<if test="title != null">and blog.title like concat('%', #{title}, '%')</if>
<if test="categoryName != null">and category.name like concat('%', #{categoryName}, '%')</if>
<if test="userName != null">and user.name like concat('%', #{userName}, '%')</if>
<if test="userId != null">and likes.user_id = #{userId}</if>
</where>
order by id desc
</select>
<select id="selectCollect" resultType="com.wzh.springbootproject.entity.Blog">
select blog.*, user.name as userName
from collect
left join blog
on collect.fid = blog.id
left join category
on blog.category_id = category.id
left join user
on blog.user_id = user.id
<where>
and collect.module = '博客'
<if test="title != null">and blog.title like concat('%', #{title}, '%')</if>
<if test="categoryName != null">and category.name like concat('%', #{categoryName}, '%')</if>
<if test="userName != null">and user.name like concat('%', #{userName}, '%')</if>
<if test="userId != null">and collect.user_id = #{userId}</if>
</where>
order by id desc
</select>
<select id="selectComment" resultType="com.wzh.springbootproject.entity.Blog">
select blog.*, user.name as userName
from comment
left join blog
on comment.fid = blog.id
left join category
on blog.category_id = category.id
left join user
on blog.user_id = user.id
<where>
and comment.module = '博客'
<if test="title != null">and blog.title like concat('%', #{title}, '%')</if>
<if test="categoryName != null">and category.name like concat('%', #{categoryName}, '%')</if>
<if test="userName != null">and user.name like concat('%', #{userName}, '%')</if>
<if test="userId != null">and comment.user_id = #{userId}</if>
</where>
group by comment.fid , comment.module
//order by id desc
</select>
<delete id="deleteById">
delete
from blog
where id = #{id}
</delete>
<insert id="insert" parameterType="com.wzh.springbootproject.entity.Blog" useGeneratedKeys="true" keyProperty="id">
insert into blog
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="title != null">title,</if>
<if test="content != null">content,</if>
<if test="descr != null">descr,</if>
<if test="cover != null">cover,</if>
<if test="tags != null">tags,</if>
<if test="userId != null">user_id,</if>
<if test="date != null">date,</if>
<if test="readCount != null">read_count,</if>
<if test="categoryId != null">category_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="title != null">#{title},</if>
<if test="content != null">#{content},</if>
<if test="descr != null">#{descr},</if>
<if test="cover != null">#{cover},</if>
<if test="tags != null">#{tags},</if>
<if test="userId != null">#{userId},</if>
<if test="date != null">#{date},</if>
<if test="readCount != null">#{readCount},</if>
<if test="categoryId != null">#{categoryId},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.wzh.springbootproject.entity.Blog">
update blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="content != null">
content = #{content},
</if>
<if test="descr != null">
descr = #{descr},
</if>
<if test="cover != null">
cover = #{cover},
</if>
<if test="tags != null">
tags = #{tags},
</if>
<if test="userId != null">
user_id = #{userId},
</if>
<if test="date != null">
date = #{date},
</if>
<if test="readCount != null">
read_count = #{readCount},
</if>
<if test="categoryId != null">
category_id = #{categoryId},
</if>
</set>
where id = #{id}
</update>
</mapper>
可能会在查询第一个属性id的地方报错,上网搜了下解决方法
将Language injections中MyBatis操作中的Localname前sql删掉
我看有评论说这好像不是在解决问题,是在解决出问题的人,但是确实有用
BlogService
package com.wzh.springbootproject.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.wzh.springbootproject.common.LikesModuleEnum;
import com.wzh.springbootproject.common.RoleEnum;
import com.wzh.springbootproject.entity.*;
import com.wzh.springbootproject.mapper.BlogMapper;
import com.wzh.springbootproject.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
* 博客信息业务处理
**/
@Service
public class BlogService {
@Resource
private BlogMapper blogMapper;
@Resource
UserService userService;
@Resource
LikesService likesService;
@Resource
CollectService collectService;
/**
* 新增
*/
public void add(Blog blog) {
blog.setDate(DateUtil.today());
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
blog.setUserId(currentUser.getId());
}
blogMapper.insert(blog);
}
/**
* 删除
*/
public void deleteById(Integer id) {
blogMapper.deleteById(id);
}
/**
* 批量删除
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
blogMapper.deleteById(id);
}
}
/**
* 修改
*/
public void updateById(Blog blog) {
blogMapper.updateById(blog);
}
/**
* 根据ID查询
*/
public Blog selectById(Integer id) {
Blog blog = blogMapper.selectById(id);
User user = userService.selectById(blog.getUserId());
List<Blog> userBlogList = blogMapper.selectUserBlog(user.getId());
user.setBlogCount(userBlogList.size());
// 当前用户收到的点赞和收藏的数据
int userLikesCount = 0;
int userCollectCount = 0;
for (Blog b : userBlogList) {
Integer fid = b.getId();
int likesCount = likesService.selectByFidAndModule(fid, LikesModuleEnum.BLOG.getValue());
userLikesCount += likesCount;
int collectCount = collectService.selectByFidAndModule(fid, LikesModuleEnum.BLOG.getValue());
userCollectCount += collectCount;
}
user.setLikesCount(userLikesCount);
user.setCollectCount(userCollectCount);
blog.setUser(user); // 设置作者信息
// 查询当前博客的点赞数据
int likesCount = likesService.selectByFidAndModule(id, LikesModuleEnum.BLOG.getValue());
blog.setLikesCount(likesCount);
Likes userLikes = likesService.selectUserLikes(id, LikesModuleEnum.BLOG.getValue());
blog.setUserLike(userLikes != null);
// 查询当前博客的收藏数据
int collectCount = collectService.selectByFidAndModule(id, LikesModuleEnum.BLOG.getValue());
blog.setCollectCount(collectCount);
Collect userCollect = collectService.selectUserCollect(id, LikesModuleEnum.BLOG.getValue());
blog.setUserCollect(userCollect != null);
return blog;
}
/**
* 查询所有
*/
public List<Blog> selectAll(Blog blog) {
return blogMapper.selectAll(blog);
}
/**
* 分页查询
*/
public PageInfo<Blog> selectPage(Blog blog, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Blog> list = blogMapper.selectAll(blog);
for (Blog b : list) {
int likesCount = likesService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
b.setLikesCount(likesCount);
}
return PageInfo.of(list);
}
public List<Blog> selectTop() {
List<Blog> blogList = this.selectAll(null);
blogList = blogList.stream().sorted((b1, b2) -> b2.getReadCount().compareTo(b1.getReadCount()))
.limit(20)
.collect(Collectors.toList());
return blogList;
}
public Set<Blog> selectRecommend(Integer blogId) {
Blog blog = this.selectById(blogId);
String tags = blog.getTags();
Set<Blog> blogSet = new HashSet<>();
if (ObjectUtil.isNotEmpty(tags)) {
List<Blog> blogList = this.selectAll(null);
JSONArray tagsArr = JSONUtil.parseArray(tags);
for (Object tag : tagsArr) {
// 筛选出包含当前博客标签的其他的博客列表
Set<Blog> collect = blogList.stream().filter(b -> b.getTags().contains(tag.toString()) && !blogId.equals(b.getId()))
.collect(Collectors.toSet());
blogSet.addAll(collect);
}
}
blogSet = blogSet.stream().limit(5).collect(Collectors.toSet());
blogSet.forEach(b -> {
int likesCount = likesService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
b.setLikesCount(likesCount);
});
return blogSet;
}
public void updateReadCount(Integer blogId) {
blogMapper.updateReadCount(blogId);
}
public PageInfo<Blog> selectUser(Blog blog, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
blog.setUserId(currentUser.getId());
}
return this.selectPage(blog, pageNum, pageSize);
}
// 查询用户点赞的数据
public PageInfo<Blog> selectLike(Blog blog, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
blog.setUserId(currentUser.getId());
}
PageHelper.startPage(pageNum, pageSize);
List<Blog> list = blogMapper.selectLike(blog);
PageInfo<Blog> pageInfo = PageInfo.of(list);
List<Blog> blogList = pageInfo.getList();
for (Blog b : blogList) {
int likesCount = likesService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
b.setLikesCount(likesCount);
}
return pageInfo;
}
public PageInfo<Blog> selectCollect(Blog blog, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
blog.setUserId(currentUser.getId());
}
PageHelper.startPage(pageNum, pageSize);
List<Blog> list = blogMapper.selectCollect(blog);
PageInfo<Blog> pageInfo = PageInfo.of(list);
List<Blog> blogList = pageInfo.getList();
for (Blog b : blogList) {
int likesCount = likesService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
b.setLikesCount(likesCount);
}
return pageInfo;
}
public PageInfo<Blog> selectComment(Blog blog, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
blog.setUserId(currentUser.getId());
}
PageHelper.startPage(pageNum, pageSize);
List<Blog> list = blogMapper.selectComment(blog);
PageInfo<Blog> pageInfo = PageInfo.of(list);
List<Blog> blogList = pageInfo.getList();
for (Blog b : blogList) {
int likesCount = likesService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
b.setLikesCount(likesCount);
}
return pageInfo;
}
}
博客的增删改查与其他差不多,其中查询的时候关联查询,要用到分类控制,下一篇写分类控制