新闻管理系统分类模块的增删改功能实现,标签模块的功能实现

一.分类模块的增删改功能实现

1.dao层

在dao层创建TypeRepository,
继承JpaRepository<Type,Long>,实现数据库中的一些sql操作。

package com.guang.demo.dao;

import com.guang.demo.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;


public interface TypeRepository extends JpaRepository<Type,Long> {

    //根据名字查询类型
    Type findByName(String name);

}

2.service层

创建TypeService接口,定义增删改查的方法

package com.guang.demo.service;

import com.guang.demo.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface TypeService {

    //分页展示数据
    Page<Type> listType(Pageable pageable);

    //新增
    Type saveType(Type type);

    //根据类型名称查找type
    Type getTypeByName(String name);

    //删除type
    void deleteType(Long id);

    //根据id查询type
    Type getType(Long id);

    //更新type数据
    Type updateType(Long id,Type type);
}

创建TypeServiceImpl实现类

package com.guang.demo.service;

import com.guang.demo.dao.TypeRepository;
import com.guang.demo.po.Type;
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 TypeServiceImpl implements TypeService{

    @Autowired
    private TypeRepository typeRepository;

    @Override
    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 deleteType(Long id) {
        typeRepository.deleteById(id);
    }

    @Override
    public Type getType(Long id) {
        return typeRepository.findById(id).orElse(null);//
    }

    @Override
    public Type updateType(Long id,Type type) {
        Type type1 = typeRepository.findById(id).orElse(null);
        if (type1==null){
            System.out.println("未获得更新对象");
            return null;
        }
        BeanUtils.copyProperties(type,type1);
        return typeRepository.save(type1);
    }
}

3.web层

TypeController实现接受前端数据,和页面跳转

package com.guang.demo.web.admin;

import com.guang.demo.po.Type;
import com.guang.demo.service.TypeService;
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")
    public String type(@PageableDefault(size = 3, sort = {"id"}, direction = Sort.Direction.DESC)
                               Pageable pageable, Model model) {
        model.addAttribute("page", typeService.listType(pageable));
        return "admin/types";
    }

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

    //分类添加
    @PostMapping("/types/add")
    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.deleteType(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/types";
    }

    //跳转到更新
    @RequestMapping("/types/{id}/toUpdate")
    public String toUpdate(@PathVariable Long id, Model model){
        model.addAttribute("type",typeService.getType(id));
        return "admin/types-input";
    }

    //分类更新
    @PostMapping("/types/update/{id}")
    public String update(@PathVariable Long id,@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.updateType(id, type);
        if (type2!=null){
            attributes.addFlashAttribute("message","更新成功");
        }else {
            attributes.addFlashAttribute("message","更新失败");
        }
        return "redirect:/admin/types";
    }
}

4.成果展示

1.用户登录成果后进入分类模块界面
在这里插入图片描述
2.点击新增,填写新增分类信息,提交后,新增成功

在这里插入图片描述
在这里插入图片描述
3.点击编辑,进行对type信息的更新
在这里插入图片描述
在这里插入图片描述
4.点击删除,可以删除type
在这里插入图片描述

二.标签模块的功能实现

1.实体类设计

在po层中创建Tag实体类

package com.guang.demo.po;

import lombok.Data;
import lombok.NoArgsConstructor;

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

@Data
@NoArgsConstructor
@Entity
@Table(name = "t_tag")
public class Tag {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotBlank(message = "标签名称不能为空")
    private String name;

}

2.dao层

创建TagRepository接口

package com.guang.demo.dao;

import com.guang.demo.po.Tag;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TagRepository extends JpaRepository<Tag,Long> {
    Tag findByName(String name);
}

3.service层

创建TagService接口,定义功能实现方法

package com.guang.demo.service;

import com.guang.demo.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);

    //删除
    void deleteTag(Long id);

    //根据类型名称查找tag
    Tag getTagByName(String name);

    //根据id查询tag
    Tag getTag(Long id);

    //更新tag数据
    Tag updateTag(Long id, Tag tag);

}

创建TagServiceImpl类,实现TagService接口

package com.guang.demo.service;

import com.guang.demo.dao.TagRepository;
import com.guang.demo.po.Tag;
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 void deleteTag(Long id) {
        tagRepository.deleteById(id);

    }

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

    @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);
    }
}

4.web层

创建TagController获取前端的数据,页面的跳转

package com.guang.demo.web.admin;

import com.guang.demo.po.Tag;
import com.guang.demo.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", "不能添加重复的标签");
        }
        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}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/tags";
    }

    @RequestMapping("/tags/{id}/toUpdate")
    public String toUpdate(@PathVariable Long id, Model model){
        model.addAttribute("tag",tagService.getTag(id));
        return "admin/tags-input";
    }

    @PostMapping("/tags/update/{id}")
    public String update(@PathVariable Long id, @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/tags-input";
        }
        Tag tag2 = tagService.updateTag(id, tag);
        if (tag2!=null){
            attributes.addFlashAttribute("message","更新成功");
        }else {
            attributes.addFlashAttribute("message","更新失败");
        }
        return "redirect:/admin/tags";
    }


}

5.成果展示

1.上方点击标签进入标签模块,显示标签信息
在这里插入图片描述
2.点击新增,进入标签新增页面,输入标签内容,提交成功后,返回标签显示页
在这里插入图片描述
3.点击编辑按钮,进行对标签内容的更新
在这里插入图片描述
在这里插入图片描述
4.删除标签
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值