您的位置:首页 > 编程语言 > Java开发

中软国际实训八: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 标签管理

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: