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

本文详细介绍了使用MyBatis实现文章发布和编辑的步骤,包括从前端获取文章内容,通过控制台验证,再到数据库的存储过程。同时,阐述了如何通过ID查询文章以供编辑,并在编辑后更新数据库。

发布文章

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

 

而编辑文章

我分为两个部分

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("修改成功");
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值