java实习_day08

中软国际实训第八天——分类管理+标签管理

分类管理
  1. dao层
    首先先定义接口的实现类updateType用于更新分类
@Transactional
@Override
public Type updateType(Long id, Type type) {
    // 首先验证给定id对应的类别是否存在,如果不存在直接抛异常
    Type type1 = typeRepository.getOne(id);
    if (type1 == null){
        throw new NotFoundException("不存在该类型");
    }
    // 否则执行更新操作
    BeanUtils.copyProperties(type, type1);
    return typeRepository.save(type1);
}
  1. service层进行业务逻辑操作,用于更新信息
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    TypeRepository typeRepository;
    
    @Transactional
    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }
}
@PostMapping("/types/{id}")
public String editPost(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes attributes) {
    // 根据前端传来的Type对象的name字段来验证是否是当前已有的类别
    Type type1 = typeService.getTypeByName(type.getName());
    if (type1 != null) {
        result.rejectValue("name","nameError","不能添加重复的分类");
    }
    // 如果输入不合法,重新跳转到输入页
    if (result.hasErrors()) {
        return "admin/types-input";
    }
    // 否则更新数据库中给定id的类别信息
    Type t = typeService.updateType(id,type);
    // 根据更新操作的结果给出提示信息
    if (t == null ) {
        attributes.addFlashAttribute("message", "更新失败");
    } else {
        attributes.addFlashAttribute("message", "更新成功");
    }
    
    // 最后重定向回类别管理页,显示最新的类别列表
    return "redirect:/admin/types";
  1. model层
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    TypeService typeService;

    @GetMapping("/types")
    public String types(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model){
        model.addAttribute("page",typeService.listType(pageable));
        return "/admin/types";
    }
}
标签管理
  1. 首先创建标签的对应实体类Tag
@Entity
@Table(name = "t_tag")
public class Tag {
    @Id
    @GeneratedValue
    private Long id;
    @NotBlank(message = "标签名称不能为空")
    private String name;
}
  1. service层创建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 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);
    }
}
  1. 具体的逻辑实现
package com.zr0726.news.web.admin;

import com.zr0726.news.po.Tag;
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);
        if(tag2!=null){
            attributes.addFlashAttribute("message","更新成功");
        }else{
            attributes.addFlashAttribute("message","更新失败");
        }
        return "redirect:/admin/tags";
    }
}

效果展示

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值