Java开发学习.Day8
基于Springboot实现 分类管理+标签管理
本文内容是基于Java开发学习.Day7内容上所实现的
分类管理
该功能主要包括对Type对象的增删改查
代码实现
在dao.TypeRepository中添加findByName方法,根据name查找到对应的Type对象
package com.xxxxbt.news.dao;
import com.xxxxbt.news.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
}
查找分类
在service.TypeService中添加查找分类getTypeByName的函数声明
public interface TypeService {
Page<Type> listType(Pageable pageable);
//查
Type getTypeByName(String name);
}
在service.impl.TypeServiceImpl 中实现修改后的TypeService
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository typeRepository;
@Override
public Page<Type> listType(Pageable pageable) {
return typeRepository.findAll(pageable); //findAll方法来自jpa
}
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
}
新增分类
在service.TypeService中添加新增分类的函数声明
public interface TypeService {
//增
Type saveType(Type type);
}
在service.impl.TypeServiceImpl 中实现修改后的TypeService
package com.xxxxbt.news.service.impl;
import com.xxxxbt.news.dao.TypeRepository;
import com.xxxxbt.news.po.Type;
import com.xxxxbt.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 TypeService typeService;
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
}
最后在web.admin.TypeController添加新增分类的路由
用户在types/input路由中创建待提交的type对象,在type/add中执行新增操作
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
@RequestMapping("/types/input")
public String input (Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@RequestMapping("/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";
}
}
删除分类
在service.TypeService中添加删除分类的函数声明
public interface TypeService {
//删
void delete(Long id);
}
在service.impl.TypeServiceImpl 中实现修改后的TypeService
@Service
public class TypeServiceImpl implements TypeService {
@Override
public void delete(Long id) {
typeRepository.deleteById(id);
}
}
最后在web.admin.TypeController添加删除类型路由
后台将根据type的id从数据库中执行删除
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
@RequestMapping("/types/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
typeService.delete(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
}
修改分类
在service.TypeService中添加修改分类的函数声明
修改包括两部分操作,先获取待修改Type的id,再执行修改操作
public interface TypeService {
//改
Type getType(Long id);
Type updateType(Long id, Type type);
}
在service.impl.TypeServiceImpl 中实现修改后的TypeService
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository typeRepository;
@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);
}
}
最后在web.admin.TypeController添加修改分类的新路由,
先获取用户待修改的type操作,再由后台将修改后的数据更新数据库
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
@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";
}
}
功能展示
查询
新增
修改
删除
标签管理
代码实现
由于代码实现思路方面和类型管理大致相同,在此就不在分功能赘述
现在po中创建tag实体类
@Entity
@Table(name = "t_tag")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //自增
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;
// @ManyToMany
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 + '\'' +
'}';
}
}
再在dao中创建TagRepository 接口
public interface TagRepository extends JpaRepository<Tag,Long> {
Tag findByName(String name);
}
在service中创建TagService 接口
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 update(Long id, Tag tag);
}
在Service.impl中实现TagService 接口
@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 update(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
@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";
}
@RequestMapping("/tags/input")
public String input(Model model){
model.addAttribute("tag",new Tag());
return "admin/tags-input";
}
@RequestMapping("/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","不能添加重复的分类");
}
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){
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.update(id,tag);
if(tag2!=null){
attributes.addFlashAttribute("message","更新成功");
}
else {
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/tags";
}
}
功能展示
查询及分页
新增
修改
删除
本文地址:https://blog.csdn.net/xxxxxbt/article/details/107656385