spring boot练习

21 篇文章 1 订阅
7 篇文章 0 订阅

B站教程地址
有点古老
代码基本上跟着敲完了,但是还得啃好久的感觉

功能概述

一、主页展示

前端thyemleaf获取数据库数据对页面进行渲染
在这里插入图片描述

th:text="|${blog.description}......|"
th:text="${page.totalElements}"

实体类的方法对数据库的数据进行处理
在这里插入图片描述
在这里插入图片描述

二、文章标签

同样利用数据库的数据通过thyemleaf在前端渲染

在这里插入图片描述
标签等可以在后台进行添加删除等
修改标签-保存修改

 @PostMapping("/Mtag/{id}")
    public String editPost(@Valid Tag tag, BindingResult result,
                           @PathVariable Long id, RedirectAttributes attributes){
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1 != null){
            result.rejectValue("name","nameError","该标签已存在");
        }
        if (result.hasErrors()){
            return "admin/Rtag";
        }
        Tag t = tagService.updateTag(id,tag);
        if (t==null){
            attributes.addFlashAttribute("message","更新失败");
        }else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/Mtag";
    }

删除标签

  @GetMapping("/Mtag/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/Mtag";
    }

添加标签

@PostMapping("/Mtag")
public String post(@Valid Tag tag,BindingResult result, RedirectAttributes attributes){
    Tag tag1 = tagService.getTagByName(tag.getName());
    if (tag1 != null){
        result.rejectValue("name","nameError","该标签已存在");
    }
    if (result.hasErrors()){
        return "admin/Rtag";
    }
    Tag t = tagService.saveTag(tag);
    if (t==null){
        attributes.addFlashAttribute("message","新增失败");
    }else {
        attributes.addFlashAttribute("message","新增成功");
    }
    return "redirect:/admin/Mtag";
}

三、文章分类

和上面↑标签的相似度高达99.99%
在这里插入图片描述

四、日期归档

在这里插入图片描述
博客数仓
在这里插入图片描述
实体类中调用数仓的数据查询

@Override
public List<Blog> listRecommendBlogTop(Integer size) {
    Sort sort = Sort.by(Sort.Direction.DESC,"updateTime");
    Pageable pageable = PageRequest.of(0,size,sort);
    return blogRepository.findTop(pageable);
}

@Override
public Map<String, List<Blog>> archiveBlog() {
    List<String> years = blogRepository.findGroupYear();
    Map<String, List<Blog>> map = new HashMap<>();
    for (String year : years){
        map.put(year,blogRepository.findByYear(year));
    }
    return map;
}

@Override
public Long countBlog() {
    return blogRepository.count();
}

前端数据渲染

th:text="${blogCount}"
th:each="item : ${archiveMap}"
th:text="${item.key}"
th:text="${blog.title}"
th:each="blog : ${item.value}"
th:text="${#dates.format(blog.updateTime,'MM月dd')}"

就是简单的获取数据库中文章时间,然后根据时间进行排序,并且显示文章发布时的版权

五、博客编辑/保存/发布

后台的数据处理
在这里插入图片描述
在这里插入图片描述
在这里集成了MarkDown的文本编辑器插件
新增

@Override
public Blog saveBlog(Blog blog) {

    if (blog.getId()==null){
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
    }else {
        blog.setUpdateTime(new Date());
    }
    return blogRepository.save(blog);
}

修改

@Override
public Blog updateBlog(Long id, Blog blog) {
    Blog b = blogRepository.findById(id).get();
    if (b == null) {
        throw new NotFoundException("该博客不存在");
    }
    BeanUtils.copyProperties(blog,b, MyBeanUtils.getNullPropertyNames(blog));
    b.setUpdateTime(new Date());
    return blogRepository.save(b);
}

删除

@Override
public void deleteBlog(Long id) {
    blogRepository.deleteById(id);
}

博客这部分的代码是最复杂的,还没弄懂…再继续看看…

六、文章搜索

在这里插入图片描述
关键字查询,道理很简单,数据库中博客有匹配的相同字段,就获取该博客的数据然后显示到前端页面中,和归档、博客原理大同小异…不贴代码了这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

之墨_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值