1.分类管理
在dao层创建TypeRepository的接口
package com.zr0726.news.dao;
import com.zr0726.news.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
//指定要使用实体的类型为Type,和主键的类型为Long
public interface TypeRepository extends JpaRepository<Type,Long> {
//不允许添加同样的标签 查数据库 如果查到了就提示已存在
Type findByName(String name);
}
在service层的impl层下创建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;
//引用dao层的接口
@Service
public class TypeServiceImpl implements TypeService {
@Autowired//TypeRepository自己定义分页的类型
private TypeRepository typeRepository;
@Override//找到索要的页面 findAll是利用TypeRepository继承的方法来的
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//拿到Type
public Type getType(Long id) {
return typeRepository.findById(id).orElse(null);//findOne(id)已过时
}
@Override
public Type updateType(Long id, Type type) {
Type type1 = typeRepository.findById(id).orElse(null);
if (type1==null){
System.out.println("未获得更新对象");
return null;
}
//复制外面传进来的对象type 给type1
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 net.bytebuddy.TypeCache;
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")//根据id来进行分类排序 使用降序 只是定义数据显示的方式 DESC后面是传入对象
public String type(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
//指定一个接受字段pages 利用typeservice的方法 传入pageable
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") //Valid为校验
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);
if (type2!=null){
attributes.addFlashAttribute("message", "更新成功");
}
else {
attributes.addFlashAttribute("message", "更新失败");
}
//无论页面更新成功还是失败 根据提交按钮后提交的数据
return "redirect:/admin/types";
}
}
然后在原本的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.标签管理
创建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 + '\'' +
'}';
}
}
创建service层下创建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);
//更新操作 根据
Tag getTagByName(String name);
//删除标签
void deleteTag(Long id);
//获取标签
Tag getTag(Long id);
//更新标签 后保存
Tag updateTag(Long id,Tag tag);
}
然后在impl中新建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 Tag getTagByName(String name) {
return tagRepository.findByName(name);
}
@Override
public void deleteTag(Long id) {
tagRepository.findById(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);
}
}
在web层下的admin层下创建TagController类 其实跟TypeController类是差不多的
package com.zr0726.news.web.admin;
import com.zr0726.news.po.Tag;
import com.zr0726.news.po.Type;
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);
System.out.println("tag2:"+tag2);
if (tag2!=null){
attributes.addFlashAttribute("message", "更新成功");
}
else {
attributes.addFlashAttribute("message", "更新失败");
}
//无论页面更新成功还是失败 根据提交按钮后提交的数据
return "redirect:/admin/tags";
}
}
成功结果如下: