一个博客项目-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

相关文章

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx