本文主要是介绍实现个人资讯-添加文章功能和编辑修改文章功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 下载md编辑器 -
准备工作
官网:https://pandao.github.io/editor.md/
查看官网完整案例!
index.html核心代码
<div style="position: absolute;right:-50px;z-index: 110"><a href="/blog/add">添加文章</a></div>
BlogController核心代码
@Controller
@Slf4j
public class BlogController {@GetMapping("/blog/add")public ModelAndView toAdd(){ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("/blog/add");return modelAndView;}/*** 根据id查询文章信息* @param blogId* @return*/@GetMapping("/api/blog/get/{blogId}")@ResponseBodypublic Blog getBlog(@PathVariable("blogId")Integer blogId) {Optional.ofNullable(blogId).orElseThrow(() -> new RuntimeException("文章找不到!!!"));//这个是查看对应id的blog实例!Blog blog = blogService.getById(blogId);Optional.ofNullable(blog).orElseThrow(() -> new RuntimeException("文章找不到!!!"));return blog;}/*** 新增和添加文章接口* @param blog* @return*/@PostMapping("/api/blog/saveupdate")@ResponseBodypublic Blog saveBlog(@RequestBody Blog blog){return blogService.saveBlog(blog);}//把下面这个段业务代码写到blogService中
// @PostMapping("/api/blog/saveupdate")
// @ResponseBody
// public Blog saveBlog(@RequestBody Blog blog){
// // 这里判断的原因是:只有保存的时候,才有初始值,
// // 如果你不判断修改如果别人已经删除的。你会恢复会去的
// if (blog.getId() == null) {
// blog.setStatus(StatusEnum.STATUS);
// blog.setIsDelete(0);
// blog.setUserId(1);
// }
// boolean flag = blogService.saveOrUpdate(blog);
// return flag ? blog : null;
// }@GetMapping("/blog/edit/{blogId}")public ModelAndView toedit(@PathVariable("blogId")Integer blogId) {// 1: 定义模型视图ModelAndView modelAndView = new ModelAndView();// 2: 把编辑的博客id放入到模型中//为了编辑时,数据库内容回显给浏览器作为id使用!modelAndView.addObject("blogId",blogId);// 2: 指定跳转的页面modelAndView.setViewName("/blog/add");return modelAndView;}}
BlogServiceImpl使用了xxxThreadLocal 这个技术可以单独讲
package com.kuangstudy.service.blog;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuangstudy.config.userlocal.UserThreadLocal;
import com.kuangstudy.entity.Blog;
import com.kuangstudy.entity.User;
import com.kuangstudy.enums.StatusEnum;
import com.kuangstudy.mapper.BlogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;/*** Description:* Author: taimi 37310* Version: 1.0* Create Date Time: 2022/1/4 19:09.* Update Date Time:** @see*/
@Service
@Slf4j
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {@Overridepublic Blog saveBlog(Blog blog) {// 这里判断的原因是:只有保存的时候,才有初始值,// 如果你不判断修改如果别人已经删除的。你会恢复会去的if (blog.getId() == null) {blog.setStatus(StatusEnum.STATUS);blog.setIsDelete(0);User user = UserThreadLocal.get();System.out.println(user);blog.setUserId(user.getId());}boolean flag = this.saveOrUpdate
这篇关于实现个人资讯-添加文章功能和编辑修改文章功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!