中软国际实训八:SpringBoot项目——分类管理+标签管理
2020-07-30 19:07
134 查看
中软国际实训八:SpringBoot项目——分类管理+标签管理
1.项目结构
2.分类管理
2.1实体设计
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.2 功能实现
TypeRepository
package com.zr0726.news.dao; import com.zr0726.news.po.Type; import org.springframework.data.jpa.repository.JpaRepository; public interface TypeRepository extends JpaRepository<Type,Long> { Type findByName(String name); }
TypeService
package com.zr0726.news.service; import com.zr0726.news.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 getTypeByName(String name); void delete(Long id); Type getType(Long id); Type updateType(Long id,Type type); }
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; @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 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); } }
TypeController
package com.zr0726.news.web.admin; import com.zr0726.news.po.Type; import com.zr0726.news.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"; } @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){ model.addAttribute("type",typeService.getType(id)); return "admin/types-input"; } @RequestMapping("/types/update/{id}") public String update(@Valid Type type,BindingResult result,@PathVariable Long id,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"; } }
3.标签管理
3.1实体设计
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 + '\'' + '}'; } }
3.2 功能实现
-TagRepository
package com.zr0726.news.dao; import com.zr0726.news.po.Tag; import org.springframework.data.jpa.repository.JpaRepository; public interface TagRepository extends JpaRepository<Tag,Long> { Tag findByName(String name); }
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); void deleteTag(Long id); Tag getTagByName(String name); Tag getTag(Long id); Tag updateTag(Long id,Tag tag); }
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); } }
TagController
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}/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 ){ System.out.println("id"+id); model.addAttribute("tag",tagService.getTag(id)); return "admin/tags-input"; } @RequestMapping("/tags/update/{id}") public String update(@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/tags-input"; } Tag tag2 = tagService.updateTag(id,tag); if (tag2!=null){ attributes.addFlashAttribute("message","更新成功"); } else{ attributes.addFlashAttribute("message","更新失败"); } return "redirect:/admin/tags"; } }
4 功能测试
4.1分类管理
4.2 标签管理
相关文章推荐
- 中软国际实训十一:SpringBoot项目——评论+分类、标签页面实现
- spring boot项目标签管理+分类管理
- spring boot新闻管理项目——评论功能和按标签、类型显示新闻
- [个人笔记]基于SpringBoot新闻系统的分类管理和标签管理
- 中软国际实训十:SpringBoot项目——主页信息展示+详情页
- 项目管理-标签和分类 推荐
- 基于SpringBoot+Mybatis+Thymeleaf商品信息管理系统项目学习《第一天》
- Springboot项目RZSpider3.3.8版本发布-网页爬虫后台管理
- springBoot项目工程代码结构规则及如何交给spring管理?
- 使用Intellij Idea创建第一个SpringBoot+MyBatis+MySql项目并运行(附安装教程),使用Maven构建项目并管理第三方jar
- Spring Boot项目之⽤户管理系统
- springboot快速开发框架(脚手架)-iris后台管理项目
- 关于Springboot项目的Pom.xml文件第一排标签报错原因
- Spring Boot 小白级-最初级入门项目工程之三(分析为什么使用spring框架管理bean,它赋予了bean什么特性?)
- spring boot项目:新闻类别和标签的增删改查实现
- 酒店管理平台(element+vuejs+springboot+mybatis项目)
- Spring Boot项目利用Redis实现session管理
- Spring Boot项目利用Redis实现session管理实例
- Springboot开发:新闻管理系统评论、分页以及标签页
- springboot项目整合shiro实现权限管理