图书馆管理系统
一、项目介绍
基于Spring + Spring MVC + MyBatis的图书馆管理系统。主要功能包括:图
书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。
二、系统运行界面图
三、系统部分功能截图
四、部分代码展示
package com.library.controller;
import com.library.bean.Book;
import com.library.bean.Lend;
import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@Autowired
private LendService lendService;
private Date getDate(String pubstr) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(pubstr);
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
@RequestMapping("/querybook.html")
public ModelAndView queryBookDo(String searchWord) {
if (bookService.matchBook(searchWord)) {
ArrayList<Book> books = bookService.queryBook(searchWord);
ModelAndView modelAndView = new ModelAndView("admin_books");
modelAndView.addObject("books", books);
return modelAndView;
} else {
return new ModelAndView("admin_books", "error", "没有匹配的图书");
}
}
@RequestMapping("/reader_querybook_do.html")
public ModelAndView readerQueryBookDo(String searchWord) {
if (bookService.matchBook(searchWord)) {
ArrayList<Book> books = bookService.queryBook(searchWord);
ModelAndView modelAndView = new ModelAndView("reader_books");
modelAndView.addObject("books", books);
return modelAndView;
} else {
return new ModelAndView("reader_books", "error", "没有匹配的图书");
}
}
@RequestMapping("/admin_books.html")
public ModelAndView adminBooks() {
ArrayList<Book> books = bookService.getAllBooks();
ModelAndView modelAndView = new ModelAndView("admin_books");
modelAndView.addObject("books", books);
return modelAndView;
}
@RequestMapping("/book_add.html")
public ModelAndView addBook() {
return new ModelAndView("admin_book_add");
}
@RequestMapping("/book_add_do.html")
public String addBookDo(@RequestParam(value = "pubstr") String pubstr, Book book, RedirectAttributes redirectAttributes) {
book.setPubdate(getDate(pubstr));
if (bookService.addBook(book)) {
redirectAttributes.addFlashAttribute("succ", "图书添加成功!");
} else {
redirectAttributes.addFlashAttribute("succ", "图书添加失败!");
}
return "redirect:/admin_books.html";
}
@RequestMapping("/updatebook.html")
public ModelAndView bookEdit(HttpServletRequest request) {
long bookId = Long.parseLong(request.getParameter("bookId"));
Book book = bookService.getBook(bookId);
ModelAndView modelAndView = new ModelAndView("admin_book_edit");
modelAndView.addObject("detail", book);
return modelAndView;
}
@RequestMapping("/book_edit_do.html")
public String bookEditDo(@RequestParam(value = "pubstr") String pubstr, Book book, RedirectAttributes redirectAttributes) {
book.setPubdate(getDate(pubstr));
if (bookService.editBook(book)) {
redirectAttributes.addFlashAttribute("succ", "图书修改成功!");
} else {
redirectAttributes.addFlashAttribute("error", "图书修改失败!");
}
return "redirect:/admin_books.html";
}
@RequestMapping("/admin_book_detail.html")
public ModelAndView adminBookDetail(HttpServletRequest request) {
long bookId = Long.parseLong(request.getParameter("bookId"));
Book book = bookService.getBook(bookId);
ModelAndView modelAndView = new ModelAndView("admin_book_detail");
modelAndView.addObject("detail", book);
return modelAndView;
}
@RequestMapping("/reader_book_detail.html")
public ModelAndView readerBookDetail(HttpServletRequest request) {
long bookId = Long.parseLong(request.getParameter("bookId"));
Book book = bookService.getBook(bookId);
ModelAndView modelAndView = new ModelAndView("reader_book_detail");
modelAndView.addObject("detail", book);
return modelAndView;
}
@RequestMapping("/admin_header.html")
public ModelAndView admin_header() {
return new ModelAndView("admin_header");
}
@RequestMapping("/reader_header.html")
public ModelAndView reader_header() {
return new ModelAndView("reader_header");
}
@RequestMapping("/reader_books.html")
public ModelAndView readerBooks(HttpServletRequest request) {
ArrayList<Book> books = bookService.getAllBooks();
ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
ArrayList<Lend> myAllLendList = lendService.myLendList(readerCard.getReaderId());
ArrayList<Long> myLendList = new ArrayList<>();
for (Lend lend : myAllLendList) {
// 是否已归还
if (lend.getBackDate() == null) {
myLendList.add(lend.getBookId());
}
}
ModelAndView modelAndView = new ModelAndView("reader_books");
modelAndView.addObject("books", books);
modelAndView.addObject("myLendList", myLendList);
return modelAndView;
}
}
package com.library.controller;
import com.library.bean.Lend;
import com.library.bean.LendDate;
import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@Controller
public class LendController {
@Autowired
private LendService lendService;
@Autowired
private BookService bookService;
@RequestMapping("/deletebook.html")
public String deleteBook(HttpServletRequest request, RedirectAttributes redirectAttributes) {
long bookId = Long.parseLong(request.getParameter("bookId"));
if (bookService.deleteBook(bookId)) {
redirectAttributes.addFlashAttribute("succ", "图书删除成功!");
} else {
redirectAttributes.addFlashAttribute("error", "图书删除失败!");
}
return "redirect:/admin_books.html";
}
@RequestMapping("/lendlist.html")
public ModelAndView lendList(HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("admin_lend_list");
ArrayList<Lend> lends = lendService.lendList();
ArrayList<LendDate> lendDates = new ArrayList<>();
for (Lend lend : lends) {
LendDate lendDate = new LendDate();
BeanUtils.copyProperties(lend,lendDate);
if (lend.getLendDate()!=null)
lendDate.setLendDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getLendDate()));
if (lend.getBackDate()!=null)
lendDate.setBackDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getBackDate()));
lendDates.add(lendDate);
}
modelAndView.addObject("list", lendDates);
return modelAndView;
}
@RequestMapping("/mylend.html")
public ModelAndView myLend(HttpServletRequest request) {
ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
ModelAndView modelAndView = new ModelAndView("reader_lend_list");
ArrayList<Lend> lends = lendService.myLendList(readerCard.getReaderId());
ArrayList<LendDate> lendDates = new ArrayList<>();
for (Lend lend : lends) {
LendDate lendDate = new LendDate();
BeanUtils.copyProperties(lend,lendDate);
if (lend.getLendDate()!=null)
lendDate.setLendDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getLendDate()));
if (lend.getBackDate()!=null)
lendDate.setBackDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getBackDate()));
lendDates.add(lendDate);
}
modelAndView.addObject("list", lendDates);
return modelAndView;
}
@RequestMapping("/deletelend.html")
public String deleteLend(HttpServletRequest request, RedirectAttributes redirectAttributes) {
long serNum = Long.parseLong(request.getParameter("serNum"));
if (lendService.deleteLend(serNum) > 0) {
redirectAttributes.addFlashAttribute("succ", "记录删除成功!");
} else {
redirectAttributes.addFlashAttribute("error", "记录删除失败!");
}
return "redirect:/lendlist.html";
}
@RequestMapping("/lendbook.html")
public String bookLend(HttpServletRequest request, RedirectAttributes redirectAttributes) {
long bookId = Long.parseLong(request.getParameter("bookId"));
long readerId = ((ReaderCard) request.getSession().getAttribute("readercard")).getReaderId();
if (lendService.lendBook(bookId, readerId)) {
redirectAttributes.addFlashAttribute("succ", "图书借阅成功!");
} else {
redirectAttributes.addFlashAttribute("succ", "图书借阅成功!");
}
return "redirect:/reader_books.html";
}
@RequestMapping("/returnbook.html")
public String bookReturn(HttpServletRequest request, RedirectAttributes redirectAttributes) {
long bookId = Long.parseLong(request.getParameter("bookId"));
long readerId = ((ReaderCard) request.getSession().getAttribute("readercard")).getReaderId();
if (lendService.returnBook(bookId, readerId)) {
redirectAttributes.addFlashAttribute("succ", "图书归还成功!");
} else {
redirectAttributes.addFlashAttribute("error", "图书归还失败!");
}
return "redirect:/reader_books.html";
}
}
package com.library.controller;
import com.library.bean.Admin;
import com.library.bean.ReaderCard;
import com.library.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@Controller
public class LoginController {
private LoginService loginService;
@Autowired
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
@RequestMapping(value = {"/", "/login.html"})
public String toLogin(HttpServletRequest request) {
request.getSession().invalidate();
return "index";
}
@RequestMapping("/logout.html")
public String logout(HttpServletRequest request) {
request.getSession().invalidate();
return "redirect:/login.html";
}
//负责处理loginCheck.html请求
//请求参数会根据参数名称默认契约自动绑定到相应方法的入参中
@RequestMapping(value = "/api/loginCheck", method = RequestMethod.POST)
public @ResponseBody
Object loginCheck(HttpServletRequest request) {
long id = Long.parseLong(request.getParameter("id"));
String passwd = request.getParameter("passwd");
boolean isReader = loginService.hasMatchReader(id, passwd);
boolean isAdmin = loginService.hasMatchAdmin(id, passwd);
HashMap<String, String> res = new HashMap<>();
if (isAdmin) {
Admin admin = new Admin();
admin.setAdminId(id);
admin.setPassword(passwd);
String username = loginService.getAdminUsername(id);
admin.setUsername(username);
request.getSession().setAttribute("admin", admin);
res.put("stateCode", "1");
res.put("msg", "管理员登陆成功!");
} else if (isReader) {
ReaderCard readerCard = loginService.findReaderCardByReaderId(id);
request.getSession().setAttribute("readercard", readerCard);
res.put("stateCode", "2");
res.put("msg", "读者登陆成功!");
} else {
res.put("stateCode", "0");
res.put("msg", "账号或密码错误!");
}
return res;
}
@RequestMapping("/admin_main.html")
public ModelAndView toAdminMain(HttpServletResponse response) {
return new ModelAndView("admin_main");
}
@RequestMapping("/reader_main.html")
public ModelAndView toReaderMain(HttpServletResponse response) {
return new ModelAndView("reader_main");
}
@RequestMapping("/admin_repasswd.html")
public ModelAndView reAdminPasswd() {
return new ModelAndView("admin_repasswd");
}
@RequestMapping("/admin_repasswd_do")
public String reAdminPasswdDo(HttpServletRequest request, String oldPasswd, String newPasswd, String reNewPasswd, RedirectAttributes redirectAttributes) {
Admin admin = (Admin) request.getSession().getAttribute("admin");
long id = admin.getAdminId();
String password = loginService.getAdminPassword(id);
if (password.equals(oldPasswd)) {
if (loginService.adminRePassword(id, newPasswd)) {
redirectAttributes.addFlashAttribute("succ", "密码修改成功!");
return "redirect:/admin_repasswd.html";
} else {
redirectAttributes.addFlashAttribute("error", "密码修改失败!");
return "redirect:/admin_repasswd.html";
}
} else {
redirectAttributes.addFlashAttribute("error", "旧密码错误!");
return "redirect:/admin_repasswd.html";
}
}
@RequestMapping("/reader_repasswd.html")
public ModelAndView reReaderPasswd() {
return new ModelAndView("reader_repasswd");
}
@RequestMapping("/reader_repasswd_do")
public String reReaderPasswdDo(HttpServletRequest request, String oldPasswd, String newPasswd, String reNewPasswd, RedirectAttributes redirectAttributes) {
ReaderCard reader = (ReaderCard) request.getSession().getAttribute("readercard");
long id = reader.getReaderId();
String password = loginService.getReaderPassword(id);
if (password.equals(oldPasswd)) {
if (loginService.readerRePassword(id, newPasswd)) {
redirectAttributes.addFlashAttribute("succ", "密码修改成功!");
return "redirect:/reader_repasswd.html";
} else {
redirectAttributes.addFlashAttribute("error", "密码修改失败!");
return "redirect:/reader_repasswd.html";
}
} else {
redirectAttributes.addFlashAttribute("error", "旧密码错误!");
return "redirect:/reader_repasswd.html";
}
}
//配置404页面
@RequestMapping("*")
public String notFind() {
return "404";
}
}
五、底部获取完整项目(9.9¥带走)
有问题,或者需要协助调试运行项目的也可联系