github项目地址:链接地址
基于servlet+jsp+mysql+c3p0+jdbc+前端三大件的图书管理系统
(只适合新手练习,有些地方还是不够严谨)
一个适合JavaWeb新手的项目,没有使用任何框架,但是尽可能利用了MVC模式。实现用户对数据库中的图书进行借阅的功能,管理员能够对用户借书的这个请求作出相应的回答。
建表脚本和jar包
链接:https://pan.baidu.com/s/16fL-tRhPcXUu0QEfZLs7sg
提取码:yorf
复制这段内容后打开百度网盘手机App,操作更方便哦
package com.chenchangjie.controller;
import com.chenchangjie.entity.Admin;
import com.chenchangjie.entity.Reader;
import com.chenchangjie.service.BookService;
import com.chenchangjie.service.LoginService;
import com.chenchangjie.service.impl.BookServiceImpl;
import com.chenchangjie.service.impl.LoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static LoginService loginService = new LoginServiceImpl();
private BookService bookService = new BookServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String type = req.getParameter("type");
//数据交给LoginService进行逻辑处理
Object object = loginService.login(username,password,type);
if(object == null){
resp.sendRedirect("/login.jsp"); //登录失败
}else{
//有数据,登录成功,检查是用户还是管理员
HttpSession session = req.getSession();
switch (type){
case "reader":
Reader reader = (Reader) object;
session.setAttribute("reader",reader);
resp.sendRedirect("/book?page=1");
/** 解耦合
//跳转到读者的首页
List list = bookService.FindAll(1);
req.setAttribute("list",list);
//每页显示数据条数就是list的size
req.setAttribute("dataPrePage",list.size());
//显示第一页
req.setAttribute("currentPage",1);
//总页数
req.setAttribute("pages",bookService.count());
//跳转
req.getRequestDispatcher("/index.jsp").forward(req,resp);*/
break;
case "admin":
Admin admin = (Admin) object;
session.setAttribute("admin",admin);
//跳转到管理员的首页
resp.sendRedirect("/admin?page=1");
break;
}
}
}
}
LogoutServlet.java接收用户注销请求
package com.chenchangjie.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
//销毁存在session中的用户对象
session.invalidate();
resp.sendRedirect("/login.jsp");
}
}
BookServlet.java接收用户借书,展示可借图书,展示用户借书情况的请求
package com.chenchangjie.controller;
import com.chenchangjie.entity.Book;
import com.chenchangjie.entity.Borrow;
import com.chenchangjie.entity.Reader;
import com.chenchangjie.service.BookService;
import com.chenchangjie.service.impl.BookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/book")
public class BookServlet extends HttpServlet {
private BookService bookService = new BookServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
HttpSession session = req.getSession();
Reader reader = (Reader) session.getAttribute("reader");
if(method == null){
method = "findAll";
}
switch (method){
case "findAll":
String pageStr = req.getParameter("page");
Integer page = Integer.valueOf(pageStr);
List<Book> list = bookService.FindAll(page);
req.setAttribute("list",list);
req.setAttribute("dataPrePage",list.size());
req.setAttribute("currentPage",page);
req.setAttribute("pages",bookService.getPages());
req.getRequestDispatcher("/index.jsp").forward(req,resp);
break;
case "borrow":
int bookId = Integer.valueOf(req.getParameter("bookid"));
bookService.Borrow(bookId,reader.getId());
resp.sendRedirect("/book?method=findAll&page=1");
break;
case "findAllBorrow":
pageStr = req.getParameter("page");
page = Integer.valueOf(pageStr);
List<Borrow>list1 = bookService.ShowBorrowBookByReadId(reader.getId(),page);
req.setAttribute("list",list1);
req.setAttribute("dataPrePage",list1.size());
req.setAttribute("currentPage",page);
req.setAttribute("pages",bookService.getBorrowBooksPages(reader.getId()));
req.getRequestDispatcher("/borrow.jsp").forward(req,resp);
break;
}
}
}
AdminServlet.java接收管理员待处理业务,管理员同意/拒绝用户请求,归还图书请求
package com.chenchangjie.controller;
import com.chenchangjie.entity.Admin;
import com.chenchangjie.entity.Borrow;
import com.chenchangjie.service.BookService;
import com.chenchangjie.service.impl.BookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
private BookService bookService = new BookServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if(method == null){
method = "findAllBorrowByState";
}
switch (method){
case "findAllBorrowByState":
String pageStr = req.getParameter("page");
Integer page = Integer.valueOf(pageStr);
List<Borrow> list = bookService.findAllBorrowByState(page,0);
req.setAttribute("list",list);
req.setAttribute("dataPrePage",list.size());
req.setAttribute("currentPage",page);
req.setAttribute("pages",bookService.getBorrowPagesByState(0));
req.getRequestDispatcher("/admin.jsp").forward(req,resp);
break;
case "handle":
HttpSession session = req.getSession();
Admin admin = (Admin) session.getAttribute("admin");
String borrowIdStr = req.getParameter("borrowId");
String stateStr = req.getParameter("state");
Integer borrowId = Integer.parseInt(borrowIdStr);
Integer state = Integer.parseInt(stateStr);
bookService.handleBorrow(borrowId,admin.getId(),state);
if(state == 3)
resp.sendRedirect("/admin?method=getBorrowed&page=1");
else if(state == 1 || state == 2)
resp.sendRedirect("/admin?page=1");
break;
case "getBorrowed":
pageStr = req.getParameter("page");
page = Integer.valueOf(pageStr);
List<Borrow> list1 = bookService.findAllBorrowByState(page,1);
req.setAttribute("list",list1);
req.setAttribute("dataPrePage",list1.size());
req.setAttribute("currentPage",page);
req.setAttribute("pages",bookService.getBorrowPagesByState(1));
req.getRequestDispatcher("/return.jsp").forward(req,resp);
break;
}
}
}
service层接收controller发送来的数据和请求做相应的逻辑处理,如果需要数据库中的信息就把关键字传给repository层,repository层做出响应把数据返回给是service(只列出接口了,实现类下载就直接看到了)
LoginService.java实现处理登录的逻辑
package com.chenchangjie.service;
public interface LoginService {
/**
* type对应用户和管理员,username和 password传给repository检测数据库中是否有数据
* @param username
* @param password
* @param type
* @return
*/
Object login(String username,String password,String type);
}
BookService.java响应借书,还书,同意借书,不同意借书…一系列的请求
package com.chenchangjie.service;
import com.chenchangjie.entity.Book;
import com.chenchangjie.entity.Borrow;
import java.util.List;
public interface BookService {
/**
* 根据page,计算出相应的开始下标
* @param page
* @return
*/
List<Book> FindAll(int page);
/**
* 根据repository返回总的记录条数计算总的页数
* @return
*/
int getPages();
/**
* 借书
*/
void Borrow(int bookId,int readerId);
/**
* 根据用户id和需要展示的页数返回该用户借书的列表
* @param readerID
* @param page
* @return
*/
List<Borrow> ShowBorrowBookByReadId(int readerID,int page);
/**
* readerId传给repository层,根据repository返回总的记录条数计算总的借书页数
* @return
*/
int getBorrowBooksPages(int readerId);
/**
* 返回给管理员未审核的集合
* @param state
* @return
*/
List<Borrow> findAllBorrowByState(int page,int state);
/**
* 返回给管理员未审核的页数
* @return
*/
int getBorrowPagesByState(int state);
/**
* 管理员处理借书请求
* @param borrowId
* @param adminId
* @param state
*/
void handleBorrow(Integer borrowId,Integer adminId,Integer state);
}
repository层响应service层所需要的数据
AdminRepository.java 返回数据库中管理员记录
package com.chenchangjie.repository;
import com.chenchangjie.entity.Admin;
public interface AdminRepository {
/**
* 连接数据库检测是否有管理员数据
* @param username
* @param password
* @return
*/
Admin login(String username,String password);
}
ReaderRepository.java 返回数据库中用户的数据
package com.chenchangjie.repository;
import com.chenchangjie.entity.Reader;
public interface ReaderRepository {
/**
* 连接数据库检查是否有相应的用户数据
* @param username
* @param password
* @return
*/
Reader login(String username,String password);
}
BookRepository.java 返回给首页需要的数据
package com.chenchangjie.repository;
import com.chenchangjie.entity.Book;
import java.util.List;
public interface BookRepository {
/**
* 根据index 和 limit显示对应的数据
* index 是开始的下标,limit 是限制的显示的记录条数
* @param index
* @return
*/
List<Book> FindAll(int index,int limit);
/**
* 返回总记录条数
* @return
*/
int count();
}
BorrowRepository.java 返回用户借书的数据,管理员待处理的数据
package com.chenchangjie.repository;
import com.chenchangjie.entity.Borrow;
import java.util.List;
public interface BorrowRepository {
/**
* 通过service传来的数据进行表的插入操作
* @param bookId
* @param readerId
* @param borrowTime
* @param returnTime
* @param adminId
* @param state
*/
void Borrow(Integer bookId,Integer readerId,String borrowTime,String returnTime,Integer adminId,Integer state);
/**
* 根据用户的id返回用户借了哪些书
* @return
*/
List<Borrow> findBorrowBookByReadId(int readerID,int index,int limit);
/**
* 返回某用户借书的总数
* @return
*/
int countBorrow(int readerId);
/**
* 返回给管理员未审核的状态列表
* @return
*/
List<Borrow> findAllBorrowByState(int index,int limit,int state);
/**
* 返回给service借书的总页数
* @param state
* @return
*/
int countByState(int state);
/**
* 根据borrowId改变数据库borrow表中state的值,并且添加adminID
* @param borrowId
* @param adminId
* @param state
*/
void handle(Integer borrowId,Integer adminId,Integer state);
}
总结:项目中虽然没有什么特别的技术框架,但是做完这个项目之后对MVC设计模式有了大体上的了解。并且后端有很重要的三层Controller->Service->Repository,分这三层之后我们就可以发现,处理后端业务不是一下就处理完的,而是有着很严格的分工。Controller层接收前端的数和请求,然后数据传给Service做相应的逻辑,最后repository返回给Service所需要的数据。