springboot 博客开发——发布文章、编辑文章

发布文章

就是把输入框以及编辑器的内容获取到,然后控制台进行校验,然后添加到数据库就可以

而编辑文章

我分为两个部分

1.根据id然后查询这条数据,然后把这个数据返回到编辑前端页面

2.然后这个页面进行编辑,在重新提交就行

mybatis

一个是根据id查询 然后另一个是更新操作

<select id="findById" parameterType="int" resultMap="BaseResultMap">
  	select * from article where id=#{id}
</select>

<update id="updateById" parameterType="com.binglian.pojo.Article">
        update article set 
        article_name=#{articleName},
        article_category=#{articleCategory},
        article_summary=#{articleSummary},
        article_content=#{articleContent},
        article_Time=#{articleTime}
        where id=#{id}
</update>

然后是控制台,这里的业务层 直接调用dao层就可以

/**
	 * 后台管理 文章管理
	 * @return
	 */
	@GetMapping("/list")
	public String list(@RequestParam(value="page",defaultValue="1")Integer page,
					   @RequestParam(value="size",defaultValue="10") Integer size,
					   ModelMap model){
		PageHelper.startPage(page,size);
		List<Article> article=articleService.listAll();
		PageInfo<Article> articlePage=new PageInfo<>(article);
		
		ArticlePageVO<Article> articlePageVO=new ArticlePageVO<>();
		articlePageVO.setPage(articlePage.getPageNum());
		articlePageVO.setTotal(articlePage.getPages());
		articlePageVO.setRecords(articlePage.getTotal());
		articlePageVO.setRows(article);
		articlePageVO.setNextPage(articlePage.getNextPage());
		articlePageVO.setPrePage(articlePage.getPrePage());
		model.addAttribute("ArticlePageVO",articlePageVO);
		return "admin/article/list";
	}
	


	/*
	 * 先要把编辑的文章 返回给前端 在声明一个方法提交修改的内容
	 * 编辑文章
	 */
	@GetMapping("/edit")
	public String editArticle(@RequestParam(name="id") Integer id,ModelMap model){
		Article article=articleService.getByIdArticleList(id);
		
		model.addAttribute("article", article);
		return "admin/article/edit";
	}
	
	
	/**
	 * 提交修改的文章
	 * @param article
	 * @return
	 * 编辑文章
	 */
	@PostMapping("/editSubmit")
	public BinglianJSONResult EditArticleSubmit(@RequestBody Article article){
		
		//判断标题 内容是否为空
		if(StringUtils.isBlank(article.getArticleCategory()) || StringUtils.isBlank(article.getArticleName())){
			return BinglianJSONResult.errorMsg("标题和分类不能为空");
		}
		
		//判断分类名称是否存在
		Category categoryIsExst=categoryService.queryCategoryName(article.getArticleCategory());
		if (categoryIsExst ==null) {
			return BinglianJSONResult.errorMsg("分类不存在,请重新输入");
		}
		Article articleUpdate=articleService.getByIdArticleList(article.getId());
		articleUpdate.setArticleCategory(article.getArticleCategory());
		articleUpdate.setArticleContent(article.getArticleContent());
		articleUpdate.setArticleName(article.getArticleName());
		articleUpdate.setArticleSummary(article.getArticleSummary());
		articleService.updateArticle(articleUpdate);
		
		return BinglianJSONResult.ok("修改成功");
	}
经验分享 程序员 微信小程序 职场和发展