[个人笔记]基于SpringBoot新闻系统的分类管理和标签管理

1.分类管理
在dao层创建TypeRepository的接口

package com.zr0726.news.dao;

import com.zr0726.news.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;

//指定要使用实体的类型为Type,和主键的类型为Long
public interface TypeRepository extends JpaRepository<Type,Long> {
    //不允许添加同样的标签 查数据库 如果查到了就提示已存在
    Type findByName(String name);
}

在service层的impl层下创建TypeServiceImpl类

package com.zr0726.news.service.impl;

import com.zr0726.news.dao.TypeRepository;
import com.zr0726.news.po.Type;
import com.zr0726.news.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
//引用dao层的接口
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired//TypeRepository自己定义分页的类型
    private TypeRepository typeRepository;


    @Override//找到索要的页面 findAll是利用TypeRepository继承的方法来的
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }

    @Override
    public Type saveType(Type type) {
        return typeRepository.save(type);
    }

    @Override
    public Type getTypeByName(String name) {
        return typeRepository.findByName(name);
    }

    @Override
    public void delete(Long id) {
        typeRepository.deleteById(id);
    }

    @Override//拿到Type
    public Type getType(Long id) {
        return typeRepository.findById(id).orElse(null);//findOne(id)已过时
    }

    @Override
    public Type updateType(Long id, Type type) {
        Type type1 = typeRepository.findById(id).orElse(null);
        if (type1==null){
            System.out.println("未获得更新对象");
            return null;
        }
        //复制外面传进来的对象type 给type1
        BeanUtils.copyProperties(type, type1);
        return typeRepository.save(type1);
    }


}

编写TypeController类

package com.zr0726.news.web.admin;

import com.zr0726.news.po.Type;
import com.zr0726.news.service.TypeService;
import net.bytebuddy.TypeCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class  TypeController {

    @Autowired
    private TypeService typeService;

    @RequestMapping("/types")//根据id来进行分类排序 使用降序 只是定义数据显示的方式 DESC后面是传入对象
        public String type(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
                                       Pageable pageable, Model model){

        //指定一个接受字段pages 利用typeservice的方法 传入pageable
        model.addAttribute("page", typeService.listType(pageable));
        return "admin/types";

    }

    @GetMapping("/types/input")
    public String input(Model model){
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    //判断是否存在重复分类 添加操作
    @PostMapping("/types/add") //Valid为校验
    public String add(@Valid Type type, BindingResult result, RedirectAttributes attributes){
        //所有重复的错误
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1!=null){
            result.rejectValue("name", "nameError","不能添加重复的分类");
        }
        if (result.hasErrors()){
            return "admin/types-input";
        }
        Type type2 = typeService.saveType(type);
        if (type2==null){
            attributes.addFlashAttribute("message", "新增失败");
        }else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/types";
    }

    //删除操作
    @RequestMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        typeService.delete(id);
        attributes.addFlashAttribute("message", "删除成功");
        return  "redirect:/admin/types";
    }

    //更新操作
    @RequestMapping("/types/{id}/toUpdate")
    public String toUpdate(@PathVariable Long id,Model model){
        //先展示原有的数据
        System.out.println("id:"+id);
        model.addAttribute("type",typeService.getType(id));
        System.out.println("根据id传入数据为"+typeService.getType(id));
        return "admin/types-input";
    }

    @RequestMapping("/types/update/{id}")
    public String update(@Valid Type type,BindingResult result,@PathVariable Long id,RedirectAttributes attributes){
        System.out.println("传入type,"+type);
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1!=null){
            result.rejectValue("name", "nameError","不能添加重复的分类");
        }
        if (result.hasErrors()){
            return "admin/types-input";
        }
        Type type2 = typeService.updateType(id, type);
        if (type2!=null){
            attributes.addFlashAttribute("message", "更新成功");

        }
        else {
            attributes.addFlashAttribute("message", "更新失败");
        }
        //无论页面更新成功还是失败 根据提交按钮后提交的数据
        return "redirect:/admin/types";
    }
}

然后在原本的Type实体类修改

package com.zr0726.news.po;


import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "t_type")
//分类管理
public class Type {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "分类名称不能为空")
    private String name;

    @OneToMany(mappedBy = "type")
    private List<News> news = new ArrayList<>();

    public Type() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<News> getNews() {
        return news;
    }

    public void setNews(List<News> news) {
        this.news = news;
    }

    @Override
    public String toString() {
        return "Type{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

然后运行截图:
在这里插入图片描述点击新增
在这里插入图片描述新增成功
在这里插入图片描述
点击删除,删除成功
在这里插入图片描述点击编辑后
在这里插入图片描述点击提交
在这里插入图片描述在这里插入图片描述2.标签管理
创建Tag实体类

package com.zr0726.news.po;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;

//标签管理
@Entity
@Table(name="t_tag")

//多对多
public class Tag {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotBlank(message =  "标签名称不能为空")
    private String name;

    public Tag() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Tag{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

创建service层下创建TagService 把各种接口写上

package com.zr0726.news.service;

import com.zr0726.news.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface TagService {
    //对标签进行分页管理
    Page<Tag> listTag(Pageable pageable);

    //保存标签
    Tag saveTag(Tag tag);

    //更新操作 根据
    Tag getTagByName(String name);

    //删除标签
    void deleteTag(Long id);


    //获取标签
    Tag getTag(Long id);
    //更新标签 后保存
    Tag updateTag(Long id,Tag tag);

}

然后在impl中新建TagServiceImpl类实现它

package com.zr0726.news.service.impl;

import com.zr0726.news.dao.TagRepository;
import com.zr0726.news.po.Tag;
import com.zr0726.news.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository tagRepository;

    @Override
    public Page<Tag> listTag(Pageable pageable) {
        return tagRepository.findAll(pageable);
    }

    @Override
    public Tag saveTag(Tag tag) {
        return tagRepository.save(tag);
    }

    @Override
    public Tag getTagByName(String name) {
        return tagRepository.findByName(name);
    }

    @Override
    public void deleteTag(Long id) {
        tagRepository.findById(id);
    }

    @Override
    public Tag getTag(Long id) {
        return tagRepository.findById(id).orElse(null);
    }

    @Override
    public Tag updateTag(Long id, Tag tag) {
        Tag tag1 = tagRepository.findById(id).orElse(null);
        if (tag1==null){
            System.out.println("未获得更新对象");
            return null;
        }
        BeanUtils.copyProperties(tag, tag1);
        return tagRepository.save(tag1);

    }
}

在web层下的admin层下创建TagController类 其实跟TypeController类是差不多的

package com.zr0726.news.web.admin;


import com.zr0726.news.po.Tag;
import com.zr0726.news.po.Type;
import com.zr0726.news.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TagController {
    @Autowired
    private TagService tagService;


    @RequestMapping("/tags")
    public String tags(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
                       Pageable pageable, Model model){
        model.addAttribute("page",tagService.listTag(pageable));
        return "admin/tags";

    }
    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag", new Tag());
        return "admin/tags-input";
    }

    @PostMapping("/tags/add")
    public String add(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1!=null){
            result.rejectValue("name", "nameError","不能添加重复的标签");
            System.out.println(result);
        }
        if (result.hasErrors()){
            return "admin/tags-input";
        }
        Tag tag2 = tagService.saveTag(tag);
        if (tag2==null){
            attributes.addFlashAttribute("message", "新增失败");
        }
        else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/tags";
    }

    //更新操作
    @RequestMapping("/tags/{id}/toUpdate")
    public String toUpdate(@PathVariable Long id,Model model){
        //先展示原有的数据
        System.out.println("id:"+id);
        model.addAttribute("tag",tagService.getTag(id));
        System.out.println("根据id传入数据为"+tagService.getTag(id));
        return "admin/tags-input";
    }


    @RequestMapping("/tags/update/{id}")
    public String update(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes){
        System.out.println("传入tag,"+tag);
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1!=null){
            result.rejectValue("name", "nameError","不能添加重复的分类");
        }
        if (result.hasErrors()){
            return "admin/tags-input";
        }
        Tag tag2 = tagService.updateTag(id, tag);
        System.out.println("tag2:"+tag2);
        if (tag2!=null){
            attributes.addFlashAttribute("message", "更新成功");
        }
        else {
            attributes.addFlashAttribute("message", "更新失败");
        }
        //无论页面更新成功还是失败 根据提交按钮后提交的数据
        return "redirect:/admin/tags";
    }
}

成功结果如下:
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

前言: 这是本人开发的个人知识管理软件,特别适合需要阅读大量pdf\word\mht\txt等格式文献的科研人员,有效提高个人知识管理能力,减轻记忆压力。因为这几年来都没有时间开发和维护,所以现在开源,希望有人能发扬光大。由于时间关系,没有很好整理文档,而且不是最新版,需要的请联系。本人曾参与Sourceforge的latex2rtf项目,在知识管理方面具有独创见解,希望大家能共同探讨,促进我国科研人员的个人知识管理水平。 本软件综合了Tag2Find、Leapfrog Tag等Tag管理软件的优点,同时克服了这类软件速度奇慢的缺点,具有Everything一样的即时搜索性能。所以叫Tagging-Taggie。 大致工作流程: ------------------------------------------------------------ 1. 启动Tagging软件,此时后台会运行一些针对常用阅读软件开发的AutoHotkey脚本(可以自定义); 2. 在你熟悉的阅读软件(例如Acrobat Adobe、Pdf Xchange Viewer, Office Word等里面)按下快捷键 Ctrl+`,将弹出一个迷你窗口(叫Taggie),可以输入各类标签(也可以从常用词选择,如文章类型,重要性),同时显示以前的关键词,所有标签和当前页码等信息会自动保存到数据库。 如果按下快捷键 Alt+`,则不弹出任何窗口,但是数据库将记录此文件的标题,当前选的文字前20个字等信息,这样方便地保存了您的访问记录,而且不受软件的限制。 3. 打开Tagging主界面(类似Everything),可以一边打字输入一边获得检索结果,同时有最近浏览记录、访问最多记录、最常用Tag等信息。 注:上述快捷键可以自定义,例如设置为F1是最轻松的。 背景知识: ----------------------------------------------------------- 一般来说,我们阅读科技文献时,希望随时快速记下带有自己思维方式的Tag,比如这篇文章是欧洲某国的,这一页很重要,这篇文章很重要,这篇文章是90年代的,这篇文章是某公司或某大学的,这是会议文章/期刊文章/技术报告/国际标准等等。但是如果采用重命名文件的方法(适用于Tag较短的情况),就会疲于应付。 如果你是研究生或者科研工作者,那么自然需要阅读大量的文献,采用其它知识管理软件都需要大量的鼠标和键盘动作,同样会疲于应付。 换句话说,采用文件夹管理只是实现了文件的树状分类,但是一篇文献在每个人脑海里面还有特殊的标签,只有采用标签和树状分类才能保证我们的每一篇文献都能快速找到。 采用本软件,你就可以从各种蛛丝马迹找到你曾经阅读过的文献。 工作机理 ------------------------------------------------------- 1. Taggie会自动获得当前文件的特定属性,例如文件创建日期、pdf的页数、已有关键词等,并根据这些信息为该文件创建一个UUID.lnk,如果开启了Distributed Link Tracking Client服务,在本地计算机你可以随意重命名或者移动该文件,以后用Tagging搜索Tag时都可以找到该文件。 2. 当前版本用到的数据库其实就是类似csv或者xls的文本文件,你也可以通过OneNote接口把数据保存到One文件里面去,这样可以在OneNote里面补充注释,管理起来更加富有层次。 细节和讨论: ---------------------------------------------------------- 1. 多个标签数据库的同步和合并; 可以为不同电脑指定一个特定的数据库名字,在Tagging里面可以勾选要搜索的数据库,一般不用考虑数据同步。但是最好能随时把个人的数据库上传到快盘之类的地方,或者用Groove同步,实现团队成员的知识管理。 2. 采用Symbolic link的方式与采用快捷方式的对比。 还没有测试。 3. 。。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值