javaWeb
MVC
一种编程思想
C(Controller)
控制、派发、Servlet
M(Model)
业务数据、service、entity、repository
V(View)
视图、jsp
图书管理系统简单模拟
开发人员:1人
开发环境:windows10/jdk1.8/mysql5.7
开发工具:Idea/Datagrip/
项目介绍:图书管理系统模拟、首先分为读者和图书管理员、读者可登录进行图书借阅、归还、等待图书管理员审批、图书管理员可登录对读者进行审批、记录等操作
项目技术:JSP+Jquery+Java+JavaWeb
数据库
读者表 管理员表 图书表 图书分类表 借阅表 归还表
后端结构
Controller Entity Filter Repository Service Utils C3P0-config.xml web css images js WEB-INF jsp
Controller
Entity
Filter
Repository
Service
Uitls
C3P0-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="UTF-8"?> <c3p0-config>
<named-config name="testc3p0"> <property name="user">root</property> <property name="password">xiaobo</property> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_library?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">5</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
|
web
Code
BookServlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
@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"); Reader reader = (Reader)req.getSession().getAttribute("object");
if (null == method){ method = "findAll"; } switch (method){ case "findAll": String pageStr = req.getParameter("page"); Integer page = Integer.parseInt(pageStr);
req.setAttribute("list",bookService.findAll(page)); req.setAttribute("dataPrePage",5); req.setAttribute("currentPage",page); req.setAttribute("pages",bookService.getPages()); req.setAttribute("reader",reader);
req.getRequestDispatcher("index.jsp").forward(req,resp); break; case "addBorrow": String bookidStr = req.getParameter("bookid"); Integer bookid = Integer.parseInt(bookidStr); bookService.addBorrow(bookid,reader.getId()); resp.sendRedirect("/book?method=findAllBorrow&page=1"); break; case"findAllBorrow": pageStr = req.getParameter("page"); page = Integer.parseInt(pageStr);
req.setAttribute("list",bookService.findBorrowById(reader.getId(),page)); req.setAttribute("dataPrePage",5); req.setAttribute("currentPage",page); req.setAttribute("pages",bookService.getBorrowPages(reader.getId()));
req.setAttribute("reader",reader); req.getRequestDispatcher("borrow.jsp").forward(req,resp); break; default: break; } } }
|
BookServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
public class BookServiceImpl implements BookService {
private BookRepository bookRepository = new BookRepositoryImpl(); private BorrowRepository borrowRepository = new BorrowRepositoryImpl(); private final int LIMIT = 5;
@Override public List<Book> findAll(int page) {
int index = (page - 1) * LIMIT;
return bookRepository.findAll(index,LIMIT); }
@Override public int getPages() { int count = bookRepository.count(); int page = 0; if(count / LIMIT == 0){ page = count/LIMIT; }else{ page = count/LIMIT + 1; } return page; }
@Override public void addBorrow(Integer bookId, Integer readerId) { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dates = simpleDateFormat.format(date);
Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 14); Date time = calendar.getTime(); String returnTime = simpleDateFormat.format(time); borrowRepository.insert(bookId,readerId,dates,returnTime,null,0); }
@Override public List<Borrow> findBorrowById(Integer id,Integer page) { int index = (page - 1) * LIMIT; return borrowRepository.findAllById(id,index,LIMIT); }
@Override public int getBorrowPages(Integer id) { int count = borrowRepository.count(id); int page = 0; if (count % LIMIT == 0){ page = count / LIMIT; }else{ page = count / LIMIT + 1; } return page; }
@Override public List<Borrow> findBorrowByState(Integer state, Integer page) { int index = (page - 1) * LIMIT; return borrowRepository.findAllByState(state,index,LIMIT); }
@Override public int getBorrowStatePages(Integer state) { int count = borrowRepository.countState(state); int page = 0; if(count % LIMIT == 0){ page = count / LIMIT; }else{ page = count / LIMIT + 1; } return page; } }
|
BookRepositoryImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
public class BookRepositoryImpl implements BookRepository {
private static ComboPooledDataSource dataSource = null; private Connection connection = null; private PreparedStatement preparedStatement = null; private ResultSet resultSet = null; private List<Book> list = null;
@Override public List<Book> findAll(int index,int limit) { try { connection = JDBCTools.getConnection();
String sql = "SELECT * FROM book,bookcase WHERE book.bookcaseid = bookcase.id limit ?,?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,index); preparedStatement.setInt(2,limit); resultSet = preparedStatement.executeQuery(); list = new ArrayList<>(); while (resultSet.next()){ list.add(new Book(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getInt(5), resultSet.getDouble(6), new BookCase(resultSet.getInt(9),resultSet.getString(10)) )); } } catch (SQLException e) { e.printStackTrace(); }finally{ JDBCTools.release(connection,preparedStatement,resultSet); }
return list; }
@Override public int count() {
int count = 0;
try { connection = JDBCTools.getConnection(); String sql = "SELECT count(*) FROM book,bookcase WHERE book.bookcaseid = bookcase.id"; preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ count = resultSet.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }finally { JDBCTools.release(connection,preparedStatement,resultSet); } return count; } }
|
JDBCTools
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public class JDBCTools {
private static Connection connection = null; private static ComboPooledDataSource dataSource;
static{ dataSource = new ComboPooledDataSource("testc3p0"); }
public static Connection getConnection(){
try {
connection = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return connection; }
public static void release(Connection connection, Statement statement, ResultSet resultSet){
try { if(connection != null){ connection.close(); } if(statement != null){ statement.close(); } if(resultSet != null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
|
Book
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
public class Book {
private Integer id; private String name; private String author; private String publish; private Integer pages; private Double price; private BookCase bookCase;
public Book(String name, String author, String publish) { this.name = name; this.author = author; this.publish = publish; }
public Book(Integer id, String name, String author, String publish, Integer pages, Double price, BookCase bookCase) { this.id = id; this.name = name; this.author = author; this.publish = publish; this.pages = pages; this.price = price; this.bookCase = bookCase; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public String getPublish() { return publish; }
public void setPublish(String publish) { this.publish = publish; }
public Integer getPages() { return pages; }
public void setPages(Integer pages) { this.pages = pages; }
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
public BookCase getBookCase() { return bookCase; }
public void setBookCase(BookCase bookCase) { this.bookCase = bookCase; } }
|
效果展示
…
功能欠缺
管理员图书添加功能
管理员图书修改功能
管理员图书删除功能
借阅状态更新 已借出的图书不允许再借、未借出的图书允许借阅
小博正在完善功能、后续更新……
正确的开始、微小的长进、然后持续、嘿、我是小博、带你一起看我目之所及的世界……