一个博客项目-3分类管理

2024-02-07 00:18
文章标签 项目 博客 分类管理

本文主要是介绍一个博客项目-3分类管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 分类管理页面
  • 分类列表分页
  • 分类新增、修改、删除

  • 分类管理页面

    • types.html可以在blogs基础上进行修改
    • types-input.html可以再blogs-input基础上进行修改
  • 分类列表分页
    首先要做dao层和service层

TypeRepository.java

package com.lmr.blog.dao;import com.lmr.blog.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;public interface TypeRepository extends JpaRepository<Type, Long> {Type findByName(String name);
}

TypeService.java

package com.lmr.blog.service;import com.lmr.blog.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;/*** Created by limi on 2017/10/16.*/
public interface TypeService {Type saveType(Type type);Type getType(Long id);Type getTypeByName(String name);Page<Type> listType(Pageable pageable);Type updateType(Long id,Type type);void deleteType(Long id);
}
  • pageable是Spring Data库中定义的一个接口,用于构造翻页查询,是所有相关分页信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息(例如pageNumber、pageSize等),这样,Jpa就能通过pageable参数来得到一个带分页信息的SQL语句。
    pageable参考

TypeServiceImpl.java

package com.lmr.blog.service;import com.lmr.blog.NotFoundException;
import com.lmr.blog.dao.TypeRepository;
import com.lmr.blog.po.Type;
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;
import org.springframework.transaction.annotation.Transactional;/*** Created by limi on 2017/10/16.*/
@Service
public class TypeServiceImpl implements TypeService {@Autowired //先进行注入private TypeRepository typeRepository;@Transactional  //增删改查都放在一个事务中@Overridepublic Type saveType(Type type) {return typeRepository.save(type);}@Transactional@Overridepublic Type getType(Long id) {//  Spring boot新版本中findByid,旧版本是findOne(id)return typeRepository.findById(id).orElse(null);}@Overridepublic Type getTypeByName(String name) {return typeRepository.findByName(name);}@Transactional@Overridepublic Page<Type> listType(Pageable pageable) {return typeRepository.findAll(pageable);}@Transactional@Overridepublic Type updateType(Long id, Type type) {Type t = typeRepository.findById(id).orElse(null);if (t == null) {throw new NotFoundException("不存在该类型");}BeanUtils.copyProperties(type,t);return typeRepository.save(t);}@Transactional@Overridepublic void deleteType(Long id) {typeRepository.deleteById(id);}
}

TypeController.java

package com.lmr.blog.web.admin;import com.lmr.blog.po.Type;
import com.lmr.blog.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.validation.annotation.Validated;
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;/*** Created by limi on 2017/10/16.*/@Controller
@RequestMapping("/admin")
public class TypeController {@Autowiredprivate TypeService typeService;@GetMapping("/types")public String types(@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";}@GetMapping("/types/{id}/input")public String editInput(@PathVariable Long id, Model model) {model.addAttribute("type", typeService.getType(id));return "admin/types-input";}@PostMapping("/types")public String post(@Validated Type type,BindingResult result, RedirectAttributes attributes) {Type type1 = typeService.getTypeByName(type.getName());//  使用了后端校验,"name"必须与后端一致if (type1 != null) {result.rejectValue("name","nameError","不能添加重复的分类");}if (result.hasErrors()) {return "admin/types-input";}Type t = typeService.saveType(type);if (t == null ) {attributes.addFlashAttribute("message", "新增失败");} else {attributes.addFlashAttribute("message", "新增成功");}return "redirect:/admin/types";}@PostMapping("/types/{id}")public String editPost(@Validated 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 t = typeService.updateType(id,type);if (t == null ) {attributes.addFlashAttribute("message", "更新失败");} else {attributes.addFlashAttribute("message", "更新成功");}return "redirect:/admin/types";}@GetMapping("/types/{id}/delete")public String delete(@PathVariable Long id,RedirectAttributes attributes) {typeService.deleteType(id);attributes.addFlashAttribute("message", "删除成功");return "redirect:/admin/types";}}

这篇关于一个博客项目-3分类管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/685973

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv