spring boot第二次作业

使用SpringBoot实现分类管理和标签管理
1.在TypeRepository中添加接口,用来控制不能添加重复的分类:

Type findByName(String name);

2.在TypeService中增加方法接口:
Type saveType(Type type);
Type getTypeByName(String name);
void delete(Long id);
Type getType(Long id);
Type updateType(Long id,Type type);

在TypeServiceImpl中实现接口:
@Override
public Page 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
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.在TypeController中编写方法:
@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.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);
    System.out.println("type2:"+type2);

    if(type2!=null){
        attributes.addFlashAttribute("message","更新成功");
    } else {
        attributes.addFlashAttribute("message","更新失败");
    }
    return "redirect:/admin/types";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值