1、技术栈
Spring Boot图书管理系统是一个基于Spring Boot框架开发的应用程序,旨在实现对图书的增删改查(CRUD)操作
- Springboot
- Mybatis,MybatisPlus
2、成品图
登录页面:一共有三个身份
用户
管理员
超级管理员
3、核心代码
controller
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.southwind.entity.Book;
import com.southwind.entity.Borrow;
import com.southwind.entity.Sort;
import com.southwind.mapper.BorrowMapper;
import com.southwind.service.BookService;
import com.southwind.service.BorrowService;
import com.southwind.service.SortService;
import com.southwind.vo.BookVO;
import com.southwind.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
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.stereotype.Controller;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
*/
/**
* 控制器类,用于处理与书籍相关的请求,包括列表展示、搜索、新增、修改和删除操作。
*/
@Controller
@RequestMapping("/book")
public class BookController {
/**
* 注入BookService,用于处理书籍业务逻辑
*/
@Autowired
private BookService bookService;
/**
* 注入SortService,用于处理分类业务逻辑
*/
@Autowired
private SortService sortService;
/**
* 注入BorrowService,用于处理借阅业务逻辑
*/
@Autowired
private BorrowService borrowService;
/**
* 显示书籍列表页面,根据当前页数获取分页数据并传入Model对象
*
* @param current 当前页数
* @param model 存储数据的Model对象
* @return 返回视图名称,这里是/user/list页面
*/
@GetMapping("/list/{current}")
public String list(@PathVariable("current") Integer current, Model model){
PageVO pageVO = this.bookService.pageList(current);
model.addAttribute("page", pageVO);
model.addAttribute("sortList", this.sortService.list());
return "/user/list";
}
/**
* 根据关键词或分类ID进行书籍搜索,返回搜索结果页面
*
* @param keyWord 搜索关键词
* @param current 当前页数
* @param sid 分类ID,0表示按关键词搜索
* @param model 存储数据的Model对象
* @return 返回视图名称,这里是/user/list页面
*/
@PostMapping("/search")
public String search(String keyWord,Integer current,Integer sid,Model model){
PageVO pageVO = null;
//类别检索
if(!sid.equals(0)){
// 根据分类ID搜索
pageVO = this.bookService.searchBySort(sid, current);
} else {
//关键字检索带分页
pageVO = this.bookService.searchByKeyWord(keyWord, current);
}
model.addAttribute("page", pageVO);
model.addAttribute("sortList", this.sortService.list());
return "/user/list";
}
/**
* 根据关键词查找书籍,返回管理界面的书籍列表
*
* @param key 关键词
* @param model 存储数据的Model对象
* @return 返回视图名称,这里是/sysadmin/book页面
*/
@PostMapping("/findByKey")
public String findByKey(String key,Model model){
QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(key), "name", key)
.or()
.like(StringUtils.isNotBlank(key), "author", key)
.or()
.like(StringUtils.isNotBlank(key), "publish", key);
List<Book> list = this.bookService.list(queryWrapper);
List<BookVO> bookVOList = new ArrayList<>();
for (Book book : list) {
BookVO bookVO = new BookVO();
BeanUtils.copyProperties(book, bookVO);
Sort sort = this.sortService.getById(book.getSid());
bookVO.setSname(sort.getName());
bookVOList.add(bookVO);
}
model.addAttribute("list", bookVOList);
return "/sysadmin/book";
}
/**
* 新增书籍
*
* @param book 要新增的书籍对象
* @return 重定向到/sysadmin/bookList页面
*/
@PostMapping("/add")
public String add(Book book){
this.bookService.save(book);
return "redirect:/sysadmin/bookList";
}
/**
* 根据书籍ID获取书籍详情,用于更新操作
*
* @param id 书籍ID
* @param model 存储数据的Model对象
* @return 返回视图名称,这里是/sysadmin/updateBook页面
*/
@GetMapping("/findById/{id}")
public String findById(@PathVariable("id") Integer id,Model model){
Book book = this.bookService.getById(id);
model.addAttribute("book", book);
model.addAttribute("list", this.sortService.list());
return "/sysadmin/updateBook";
}
/**
* 更新书籍信息
*
* @param book 更新后的书籍对象
* @return 重定向到/sysadmin/bookList页面
*/
@PostMapping("/update")
public String update(Book book){
this.bookService.updateById(book);
return "redirect:/sysadmin/bookList";
}
/**
* 删除书籍,同时删除与该书籍相关的所有借阅记录
*
* @param id 书籍ID
* @return 重定向到/sysadmin/bookList页面
*/
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id){
QueryWrapper<Borrow> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("bid", id);
this.borrowService.remove(queryWrapper);
this.bookService.removeById(id);
return "redirect:/sysadmin/bookList";
}
}
service
package com.southwind.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.entity.Book;
import com.southwind.entity.Sort;
import com.southwind.mapper.BookMapper;
import com.southwind.mapper.SortMapper;
import com.southwind.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.vo.BookVO;
import com.southwind.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
@Autowired
private BookMapper bookMapper;
@Autowired
private SortMapper sortMapper;
@Override
public PageVO pageList(Integer currentPage) {
QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
bookQueryWrapper.gt("number", 0);
Page<Book> page = new Page<>(currentPage, 5);
Page<Book> resultPage = this.bookMapper.selectPage(page, bookQueryWrapper);
PageVO pageVO = new PageVO();
pageVO.setCurrentPage(resultPage.getCurrent());
pageVO.setTotalPage(resultPage.getPages());
List<BookVO> result = new ArrayList<>();
for (Book book : resultPage.getRecords()) {
BookVO bookVO = new BookVO();
BeanUtils.copyProperties(book, bookVO);
QueryWrapper<Sort> sortQueryWrapper = new QueryWrapper<>();
sortQueryWrapper.eq("id", book.getSid());
Sort sort = this.sortMapper.selectOne(sortQueryWrapper);
bookVO.setSname(sort.getName());
result.add(bookVO);
}
pageVO.setData(result);
return pageVO;
}
@Override
public PageVO searchByKeyWord(String keyWord,Integer currentPage) {
QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
bookQueryWrapper.gt("number", 0);
bookQueryWrapper.like(StringUtils.isNotBlank(keyWord), "name", keyWord)
.or()
.like(StringUtils.isNotBlank(keyWord), "author", keyWord)
.or()
.like(StringUtils.isNotBlank(keyWord), "publish", keyWord);
Page<Book> page = new Page<>(currentPage, 5);
Page<Book> resultPage = this.bookMapper.selectPage(page, bookQueryWrapper);
PageVO pageVO = new PageVO();
pageVO.setCurrentPage(resultPage.getCurrent());
pageVO.setTotalPage(resultPage.getPages());
List<BookVO> result = new ArrayList<>();
for (Book book : resultPage.getRecords()) {
BookVO bookVO = new BookVO();
BeanUtils.copyProperties(book, bookVO);
QueryWrapper<Sort> sortQueryWrapper = new QueryWrapper<>();
sortQueryWrapper.eq("id", book.getSid());
Sort sort = this.sortMapper.selectOne(sortQueryWrapper);
bookVO.setSname(sort.getName());
result.add(bookVO);
}
pageVO.setData(result);
return pageVO;
}
@Override
public PageVO searchBySort(Integer sid, Integer currentPage) {
QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
bookQueryWrapper.gt("number", 0);
bookQueryWrapper.eq("sid",sid);
Page<Book> page = new Page<>(currentPage, 5);
Page<Book> resultPage = this.bookMapper.selectPage(page, bookQueryWrapper);
PageVO pageVO = new PageVO();
pageVO.setCurrentPage(resultPage.getCurrent());
pageVO.setTotalPage(resultPage.getPages());
List<BookVO> result = new ArrayList<>();
for (Book book : resultPage.getRecords()) {
BookVO bookVO = new BookVO();
BeanUtils.copyProperties(book, bookVO);
QueryWrapper<Sort> sortQueryWrapper = new QueryWrapper<>();
sortQueryWrapper.eq("id", book.getSid());
Sort sort = this.sortMapper.selectOne(sortQueryWrapper);
bookVO.setSname(sort.getName());
result.add(bookVO);
}
pageVO.setData(result);
return pageVO;
}
@Override
public List<Book> selectBook(){
System.out.println("进入了");
List<Book> list = this.lambdaQuery().list();
return list;
}
}