0728_分类管理+标签管理
程序员文章站
2024-03-04 12:04:17
...
1.分类管理
(1)添加分类
①编写TypeService类
Page<Type> listType(Pageable pageable);
Type saveType(Type type);
②编写TypeServiceImpl类
@Override
public Page<Type> listType(Pageable pageable){
return typeRepository.findAll(pageable);
}
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
③编写TypeController类
@PostMapping("/types/add")
public String add(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";
}
④界面展示
(2)删除分类
①编写TypeService类
Type getTypeByName(String name);
void delete(Long id);
②编写TypeServiceImpl类
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
@Override
public void delete(Long id){
typeRepository.deleteById(id);
}
③编写TypeController类
@RequestMapping("/types/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes){
typeService.delete(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
④界面展示
(3)更新分类
①编写TypeService类
Type getType(Long id);
Type updateType(Long id,Type type);
②编写TypeServiceImpl类
@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类
@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";
}
④界面展示
2.标签管理
(1)编写tag实体类
package com.zr0726.myspring.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 + '\'' +
'}';
}
}
(2)编写TagRepository类
package com.zr0726.myspring.dao;
import com.zr0726.myspring.po.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TagRepository extends JpaRepository<Tag,Long> {
Tag findByName(String name);
}
(3)编写TagService类
package com.zr0726.myspring.service;
import com.zr0726.myspring.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);
}
(4)编写TagServiceImpl类
package com.zr0726.myspring.service.impl;
import com.zr0726.myspring.dao.TagRepository;
import com.zr0726.myspring.po.Tag;
import com.zr0726.myspring.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.deleteById(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);
}
}
(5)增加标签
①TagService类
Page<Tag> listTag(Pageable pageable);
Tag saveTag(Tag tag);
②TagServiceImpl类
@Override
public Page<Tag> listTag(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Override
public Tag saveTag(Tag tag) {
return tagRepository.save(tag);
}
③TagController类
@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";
}
④界面展示
(6)更新标签
①TagService类
Tag getTag(Long id);
Tag updateTag(Long id,Tag tag);
②TagServiceImpl类
@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类
@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";
}
④界面展示
上一篇: Java创建线程三种方式的优缺点
推荐阅读
-
0728_分类管理+标签管理
-
SpringBoot后端分类管理+标签管理
-
springboot模拟天猫整站,分类管理系统后端设计实现(查询)
-
spring boot项目标签管理+分类管理
-
Springboot个人博客系统 6 分类管理
-
ASP.NET MVC+LINQ开发一个图书销售站点(7):图书分类管理
-
在Eclipse中用TODO标签管理任务(Task) 博客分类: java Eclipse项目管理WindowsBlogHTML
-
在Eclipse中用TODO标签管理任务(Task) 博客分类: java Eclipse项目管理WindowsBlogHTML
-
C#微信开发之微信公众号标签管理功能
-
使用vue-meta如何管理头部标签