当前位置: 首页 > news >正文

课设作业图书管理系统

用户注册,登录 

播放地址 课设作业图书管理系统_哔哩哔哩_bilibili

对图书进行增删改查

package com.xwr.controller;

import com.xwr.entity.Book;
import com.xwr.entity.Category;
import com.xwr.service.BookService;
import com.xwr.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
* 图书控制器
*/
@Controller
@RequestMapping("/book")
public class BookController {

private final BookService bookService;
private final CategoryService categoryService;

@Autowired
public BookController(BookService bookService, CategoryService categoryService) {
this.bookService = bookService;
this.categoryService = categoryService;
}

/**
* 显示图书列表页面
*/
@GetMapping("/list")
public String list(
@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "author", required = false) String author,
@RequestParam(value = "categoryId", required = false) Integer categoryId,
Model model,
HttpSession session) {

// 检查用户是否已登录
if (session.getAttribute("loginUser") == null) {
// 未登录,重定向到登录页面
return "redirect:/user/login";
}

// 查询分类列表
List<Category> categories = categoryService.findAll();
model.addAttribute("categories", categories);

// 查询条件不为空时,执行条件查询
if (title != null && !title.isEmpty() || author != null && !author.isEmpty() || categoryId != null) {
List<Book> books = bookService.findByCondition(title, author, categoryId);
model.addAttribute("books", books);
model.addAttribute("title", title);
model.addAttribute("author", author);
model.addAttribute("categoryId", categoryId);
} else {
// 执行分页查询
Map<String, Object> pageInfo = bookService.findByPage(pageNum, pageSize);
model.addAttribute("pageInfo", pageInfo);
}

return "book/list";
}

/**
* 显示图书详情页面
*/
@GetMapping("/detail/{id}")
public String detail(@PathVariable("id") Integer id, Model model) {
Book book = bookService.findById(id);
model.addAttribute("book", book);
return "book/detail";
}

/**
* 显示添加图书页面
*/
@GetMapping("/add")
public String showAddForm(Model model) {
List<Category> categories = categoryService.findAll();
model.addAttribute("categories", categories);
return "book/add";
}

/**
* 显示编辑图书页面
*/
@GetMapping("/edit/{id}")
public String showEditForm(@PathVariable("id") Integer id, Model model) {
Book book = bookService.findById(id);
List<Category> categories = categoryService.findAll();
model.addAttribute("book", book);
model.addAttribute("categories", categories);
return "book/edit";
}

/**
* 删除图书
*/
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
bookService.delete(id);
return "redirect:/book/list";
}
}

package com.xwr.controller;

import com.xwr.entity.Book;
import com.xwr.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* 图书REST控制器
* 用于处理AJAX请求和文件上传
*/
@RestController
@RequestMapping("/api/book")
public class BookRestController {

private final BookService bookService;

@Autowired
public BookRestController(BookService bookService) {
this.bookService = bookService;
}

/**
* 注册自定义日期编辑器
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

/**
* 添加图书
*/
@PostMapping("/add")
public Map<String, Object> add(Book book, @RequestParam(value = "coverImage", required = false) MultipartFile coverImage, HttpServletRequest request) {
Map<String, Object> result = new HashMap<>();

try {
// 处理上传图片
if (coverImage != null && !coverImage.isEmpty()) {
String uploadPath = request.getServletContext().getRealPath("/upload/");
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}

// 生成唯一文件名
String originalFilename = coverImage.getOriginalFilename();
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + extension;

// 保存文件
File destFile = new File(uploadPath + fileName);
coverImage.transferTo(destFile);

// 设置图片路径
book.setImageUrl("/upload/" + fileName);
}

// 保存图书信息
boolean success = bookService.add(book);
result.put("success", success);
result.put("message", success ? "添加成功" : "添加失败");
result.put("book", book);
} catch (IOException e) {
result.put("success", false);
result.put("message", "上传图片失败: " + e.getMessage());
} catch (Exception e) {
result.put("success", false);
result.put("message", "添加失败: " + e.getMessage());
}

return result;
}

/**
* 更新图书
*/
@PostMapping("/update")
public Map<String, Object> update(Book book, @RequestParam(value = "coverImage", required = false) MultipartFile coverImage, HttpServletRequest request) {
Map<String, Object> result = new HashMap<>();

try {
// 如果有新上传的图片
if (coverImage != null && !coverImage.isEmpty()) {
String uploadPath = request.getServletContext().getRealPath("/upload/");
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}

// 生成唯一文件名
String originalFilename = coverImage.getOriginalFilename();
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + extension;

// 保存文件
File destFile = new File(uploadPath + fileName);
coverImage.transferTo(destFile);

// 设置新的图片路径
book.setImageUrl("/upload/" + fileName);
}

// 更新图书信息
boolean success = bookService.update(book);
result.put("success", success);
result.put("message", success ? "更新成功" : "更新失败");
} catch (IOException e) {
result.put("success", false);
result.put("message", "上传图片失败: " + e.getMessage());
} catch (Exception e) {
result.put("success", false);
result.put("message", "更新失败: " + e.getMessage());
}

return result;
}

/**
* 删除图书
*/
@PostMapping("/delete/{id}")
public Map<String, Object> delete(@PathVariable("id") Integer id) {
Map<String, Object> result = new HashMap<>();

try {
// 删除图书
boolean success = bookService.delete(id);
result.put("success", success);
result.put("message", success ? "删除成功" : "删除失败");
} catch (Exception e) {
result.put("success", false);
result.put("message", "删除失败: " + e.getMessage());
}

return result;
}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/253175.html

相关文章:

  • UWB协议精读:IEEE 802.15.4z-2020,15. HRP UWB PHY, STS, HRP-ERDEV, BPRF, HPRF,
  • 探秘卷积神经网络(CNN):从原理到实战的深度解析
  • 红队攻防渗透技术实战流程:信息打点-Web应用源码泄漏开源闭源指纹识别GITSVNDS备份
  • 生成对抗网络(GAN)与深度生成模型实战
  • CppCon 2016 学习:Rainbow Six Siege: Quest for Performance
  • 移动端 WebView 页面性能调试实战:WebDebugX等工具协同与优化
  • 【pytest进阶】Pytest之conftest详解
  • MCP(模型上下文协议)协议和Http协议对比
  • 窄带和宽带谁略谁优
  • python web开发-Flask 重定向与URL生成完全指南
  • 3.时间序列数据准备
  • @PostConstruct,@PreDestroy 典型用法
  • 如何科学测算AI业务场景所需算力服务器?——以Qwen3 32B模型与海光K100为例
  • 从C++编程入手设计模式——策略设计模式
  • 外包团队介入多个项目时如何确保协同一致
  • 服务器带宽小优化建议以及实战操作
  • 代码生成器使用原理以及使用方法
  • 缓存系统-基本概述
  • 在Docker上安装Mongo及Redis-NOSQL数据库
  • 从 Acrobat 到 LiveCycle 的英语词源
  • 如何使用postman
  • FPGA 44 ,SDC 时序约束标准( 深度解析 SDC 标准 )
  • FPGA 43 ,UDP 协议详细解析( FPGA 中的 UDP 协议 )
  • 数据结构排序算法合集
  • Docker 快速搭建一个基于 GPT-Vis 组件的统计图表生成服务
  • Python 目录操作详解
  • 【学习笔记】nlohmannjsoncjson
  • 【Manus第三篇-Prompt优化】两周实战,一套注意力视角的prompt优化框架,真的有用!
  • 安装VSCode的时候没勾选将“通过Code打开”操作添加到Windows资源管理器目录上下文菜单(右键VSCODE打开)
  • 计算机视觉课程笔记-机器学习中典型的有监督与无监督学习方法的详细分类、标签空间性质、解释说明,并以表格形式进行总结