├── .gitignore ├── index.png ├── src ├── main │ ├── webapp │ │ ├── static │ │ │ ├── css │ │ │ │ ├── base.css │ │ │ │ ├── login.css │ │ │ │ └── index.css │ │ │ └── images │ │ │ │ └── background.png │ │ ├── WEB-INF │ │ │ └── web.xml │ │ ├── bookDetail.jsp │ │ ├── login.jsp │ │ ├── addBook.jsp │ │ ├── updateBook.jsp │ │ └── index.jsp │ ├── resources │ │ └── db.properties │ └── java │ │ └── com │ │ └── hu │ │ ├── service │ │ ├── UserService.java │ │ ├── BookService.java │ │ └── impl │ │ │ ├── UserServiceImpl.java │ │ │ └── BookServiceImpl.java │ │ ├── dao │ │ ├── UserDao.java │ │ ├── BookDao.java │ │ └── impl │ │ │ ├── UserDaoImpl.java │ │ │ └── BookDaoImpl.java │ │ ├── filter │ │ ├── EncodingFilter.java │ │ └── LoginFilter.java │ │ ├── servlet │ │ ├── LogoutServlet.java │ │ ├── DeleteServlet.java │ │ ├── BookDetailServlet.java │ │ ├── GetOneServlet.java │ │ ├── AddServlet.java │ │ ├── UpdateServlet.java │ │ ├── LoginServlet.java │ │ └── GetServlet.java │ │ ├── entiry │ │ ├── User.java │ │ └── Book.java │ │ └── utils │ │ └── JDBCUtil.java └── test │ └── java │ └── GetBookTest.java ├── README.md ├── pom.xml └── sql └── store.sql /.gitignore: -------------------------------------------------------------------------------- 1 | # 项目排除路径 2 | /target/ -------------------------------------------------------------------------------- /index.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/360595304/JavaWeb/HEAD/index.png -------------------------------------------------------------------------------- /src/main/webapp/static/css/base.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-image: url("../images/background.png"); 3 | } 4 | -------------------------------------------------------------------------------- /src/main/webapp/static/images/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/360595304/JavaWeb/HEAD/src/main/webapp/static/images/background.png -------------------------------------------------------------------------------- /src/main/resources/db.properties: -------------------------------------------------------------------------------- 1 | driver = com.mysql.cj.jdbc.Driver 2 | url = jdbc:mysql://localhost:3306/store?characterEncoding=utf8 3 | username = root 4 | password = su360595304 -------------------------------------------------------------------------------- /src/main/java/com/hu/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hu.service; 2 | 3 | import com.hu.entiry.User; 4 | 5 | public interface UserService { 6 | User login(String username, String password); 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaWeb 2 | servlet+jsp实现的简易图书管理系统
3 | 开发环境:Tomcat 9.0.37,IDEA2021,JDK 1.8,MySQL 8.0
4 | 5 | ### 运行步骤 6 | - 导入sql文件 7 | - 运行项目 8 | - 默认用户名密码都是admin 9 | ### 运行截图 10 | ![index](./index.png) 11 | -------------------------------------------------------------------------------- /src/main/java/com/hu/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.hu.dao; 2 | 3 | import com.hu.entiry.User; 4 | 5 | public interface UserDao { 6 | boolean login(String username, String password); 7 | User getByName(String username); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/webapp/static/css/login.css: -------------------------------------------------------------------------------- 1 | .form-login { 2 | margin: 50px auto; 3 | width: 420px; 4 | 5 | } 6 | .container { 7 | border: black 1px solid; 8 | text-align: center; 9 | margin: 50px auto 0 auto; 10 | width: 40%; 11 | border-radius: 10px; 12 | box-shadow: #59a9fa 0 0 30px 5px; 13 | } 14 | .header { 15 | margin-top: 150px; 16 | } 17 | .header h1 { 18 | text-align: center; 19 | } -------------------------------------------------------------------------------- /src/main/java/com/hu/dao/BookDao.java: -------------------------------------------------------------------------------- 1 | package com.hu.dao; 2 | 3 | import com.hu.entiry.Book; 4 | 5 | import java.util.List; 6 | 7 | public interface BookDao { 8 | 9 | List getBookList(int current, int size, String key); 10 | 11 | int getTotal(int current, int size, String key); 12 | 13 | void updateBook(Book book); 14 | 15 | void deleteBook(int id); 16 | 17 | void insertBook(Book book); 18 | 19 | Book getBookById(int id); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/hu/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.hu.service; 2 | 3 | import com.hu.entiry.Book; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author suhu 9 | * @createDate 2021/12/2 10 | */ 11 | public interface BookService { 12 | 13 | List getBookList(int current, int size, String key); 14 | 15 | int getTotal(int current, int size, String key); 16 | 17 | void addBook(Book book); 18 | 19 | void removeBook(int id); 20 | 21 | void updateBook(Book book); 22 | 23 | Book getById(int id); 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/GetBookTest.java: -------------------------------------------------------------------------------- 1 | import com.hu.dao.BookDao; 2 | import com.hu.dao.impl.BookDaoImpl; 3 | import com.hu.entiry.Book; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author suhu 10 | * @createDate 2021/11/21 11 | */ 12 | public class GetBookTest { 13 | private final BookDao bookDao = new BookDaoImpl(); 14 | 15 | @Test 16 | public void test() { 17 | List bookList = bookDao.getBookList(1, 1, null); 18 | bookList.forEach(System.out::println); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/hu/filter/EncodingFilter.java: -------------------------------------------------------------------------------- 1 | package com.hu.filter; 2 | 3 | import javax.servlet.*; 4 | import javax.servlet.annotation.*; 5 | import java.io.IOException; 6 | 7 | @WebFilter(filterName = "EncodingFilter") 8 | public class EncodingFilter implements Filter { 9 | public void init(FilterConfig config) throws ServletException { 10 | } 11 | 12 | public void destroy() { 13 | } 14 | 15 | @Override 16 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { 17 | request.setCharacterEncoding("UTF-8"); 18 | // response.setContentType("text/html;charset=utf-8"); 19 | chain.doFilter(request, response); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/LogoutServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import javax.servlet.*; 4 | import javax.servlet.http.*; 5 | import javax.servlet.annotation.*; 6 | import java.io.IOException; 7 | 8 | @WebServlet(name = "LogoutServlet", value = "/LogoutServlet") 9 | public class LogoutServlet extends HttpServlet { 10 | @Override 11 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 | request.getSession().removeAttribute("userInfo"); 13 | response.sendRedirect(request.getContextPath() + "/login.jsp"); 14 | } 15 | 16 | @Override 17 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/hu/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.service.impl; 2 | 3 | import com.hu.dao.UserDao; 4 | import com.hu.dao.impl.UserDaoImpl; 5 | import com.hu.entiry.User; 6 | import com.hu.service.UserService; 7 | 8 | /** 9 | * @author suhu 10 | * @createDate 2021/12/2 11 | */ 12 | public class UserServiceImpl implements UserService { 13 | 14 | private final UserDao userDao = new UserDaoImpl(); 15 | 16 | public User login(String username, String password) { 17 | if (userDao.getByName(username) == null) { 18 | throw new RuntimeException("用户名不存在"); 19 | } 20 | if (userDao.login(username, password)) { 21 | return userDao.getByName(username); 22 | } else { 23 | throw new RuntimeException("请输入正确的密码"); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | EncodingFilter 8 | com.hu.filter.EncodingFilter 9 | 10 | 11 | EncodingFilter 12 | /* 13 | 14 | 15 | 16 | css 17 | text/css;charset=utf-8 18 | 19 | 20 | js 21 | application/javascript 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/com/hu/entiry/User.java: -------------------------------------------------------------------------------- 1 | package com.hu.entiry; 2 | 3 | /** 4 | * @author suhu 5 | * @createDate 2021/11/25 6 | */ 7 | public class User { 8 | private int id; 9 | private String username; 10 | private String role; 11 | 12 | public int getId() { 13 | return id; 14 | } 15 | 16 | public void setId(int id) { 17 | this.id = id; 18 | } 19 | 20 | public String getUsername() { 21 | return username; 22 | } 23 | 24 | public void setUsername(String username) { 25 | this.username = username; 26 | } 27 | 28 | public String getRole() { 29 | return role; 30 | } 31 | 32 | public void setRole(String role) { 33 | this.role = role; 34 | } 35 | 36 | public User() { 37 | } 38 | 39 | public User(int id, String username, String role) { 40 | this.id = id; 41 | this.username = username; 42 | this.role = role; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/DeleteServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.service.BookService; 4 | import com.hu.service.impl.BookServiceImpl; 5 | 6 | import javax.servlet.*; 7 | import javax.servlet.http.*; 8 | import javax.servlet.annotation.*; 9 | import java.io.IOException; 10 | 11 | @WebServlet(name = "DeleteServlet", value = "/DeleteServlet") 12 | public class DeleteServlet extends HttpServlet { 13 | 14 | private final BookService bookService = new BookServiceImpl(); 15 | 16 | @Override 17 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 | String id = request.getParameter("id"); 19 | bookService.removeBook(Integer.parseInt(id)); 20 | response.sendRedirect(request.getContextPath() + "/GetServlet"); 21 | } 22 | 23 | @Override 24 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/BookDetailServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.entiry.Book; 4 | import com.hu.service.impl.BookServiceImpl; 5 | 6 | import javax.servlet.*; 7 | import javax.servlet.http.*; 8 | import javax.servlet.annotation.*; 9 | import java.io.IOException; 10 | 11 | @WebServlet(name = "BookDetailServlet", value = "/BookDetailServlet") 12 | public class BookDetailServlet extends HttpServlet { 13 | @Override 14 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 | String id = request.getParameter("id"); 16 | int bookId = id == null ? 0 : Integer.parseInt(id); 17 | BookServiceImpl bookService = new BookServiceImpl(); 18 | Book book = bookService.getById(bookId); 19 | request.setAttribute("book", book); 20 | request.getRequestDispatcher("/bookDetail.jsp").forward(request, response); 21 | } 22 | 23 | @Override 24 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/GetOneServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.entiry.Book; 4 | import com.hu.service.BookService; 5 | import com.hu.service.impl.BookServiceImpl; 6 | 7 | import javax.servlet.*; 8 | import javax.servlet.http.*; 9 | import javax.servlet.annotation.*; 10 | import java.io.IOException; 11 | 12 | @WebServlet(name = "GetOneServlet", value = "/GetOneServlet") 13 | public class GetOneServlet extends HttpServlet { 14 | 15 | private final BookService bookService = new BookServiceImpl(); 16 | 17 | @Override 18 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 | String id = request.getParameter("id"); 20 | if (id == null || "".equals(id)) { 21 | response.sendRedirect(request.getContextPath() + "/index.js"); 22 | } 23 | Book book = bookService.getById(Integer.parseInt(id)); 24 | request.getSession().setAttribute("editBook", book); 25 | response.sendRedirect(request.getContextPath() + "/updateBook.jsp"); 26 | } 27 | 28 | @Override 29 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/hu/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.service.impl; 2 | 3 | import com.hu.dao.BookDao; 4 | import com.hu.dao.impl.BookDaoImpl; 5 | import com.hu.entiry.Book; 6 | import com.hu.service.BookService; 7 | 8 | import java.util.List; 9 | import java.util.UUID; 10 | 11 | /** 12 | * @author suhu 13 | * @createDate 2021/12/2 14 | */ 15 | public class BookServiceImpl implements BookService { 16 | 17 | private final BookDao bookDao = new BookDaoImpl(); 18 | 19 | 20 | @Override 21 | public List getBookList(int current, int size, String key) { 22 | return bookDao.getBookList(current, size, key); 23 | } 24 | 25 | @Override 26 | public int getTotal(int current, int size, String key) { 27 | return bookDao.getTotal(current, size, key); 28 | } 29 | 30 | @Override 31 | public void addBook(Book book) { 32 | bookDao.insertBook(book); 33 | } 34 | 35 | @Override 36 | public void removeBook(int id) { 37 | bookDao.deleteBook(id); 38 | } 39 | 40 | @Override 41 | public void updateBook(Book book) { 42 | bookDao.updateBook(book); 43 | } 44 | 45 | @Override 46 | public Book getById(int id) { 47 | return bookDao.getBookById(id); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/AddServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.entiry.Book; 4 | import com.hu.service.BookService; 5 | import com.hu.service.impl.BookServiceImpl; 6 | 7 | import javax.servlet.*; 8 | import javax.servlet.http.*; 9 | import javax.servlet.annotation.*; 10 | import java.io.IOException; 11 | 12 | @WebServlet(name = "AddServlet", value = "/AddServlet") 13 | public class AddServlet extends HttpServlet { 14 | 15 | private final BookService bookService = new BookServiceImpl(); 16 | 17 | @Override 18 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 | String name = request.getParameter("name"); 20 | String author = request.getParameter("author"); 21 | String price_s = request.getParameter("price"); 22 | double price = Double.parseDouble(price_s); 23 | String press = request.getParameter("press"); 24 | String intro = request.getParameter("intro"); 25 | String isbn = request.getParameter("isbn"); 26 | Book book = new Book(); 27 | book.setName(name); 28 | book.setAuthor(author); 29 | book.setPrice(price); 30 | book.setPress(press); 31 | book.setIntro(intro); 32 | book.setIsbn(isbn); 33 | bookService.addBook(book); 34 | response.sendRedirect(request.getContextPath() + "/GetServlet"); 35 | } 36 | 37 | @Override 38 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/webapp/bookDetail.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | 书籍详情 5 | 26 | 27 | 28 |
29 |

《${requestScope.book.name}》

30 |
31 |

${requestScope.book.intro}

32 |
33 |
34 | 作者:${requestScope.book.author} 35 |
36 |
37 | 出版社:${requestScope.book.press} 38 |
39 | 40 |
41 |
42 |
43 | ISBN:${requestScope.book.isbn} 44 |
45 |
46 | 单价:${requestScope.book.price} 47 |
48 |
49 |
50 | 51 |
52 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/main/webapp/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | 登陆 5 | 6 | 7 | 8 | 9 | 10 |
11 |

图书管理系统

12 |
13 |
14 | 15 | 16 | 38 |
39 | 40 | 41 | -------------------------------------------------------------------------------- /src/main/java/com/hu/filter/LoginFilter.java: -------------------------------------------------------------------------------- 1 | package com.hu.filter; 2 | 3 | import com.hu.entiry.User; 4 | 5 | import javax.servlet.*; 6 | import javax.servlet.annotation.*; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpSession; 10 | import java.io.IOException; 11 | 12 | @WebFilter(filterName = "LoginFilter", value = "/*") 13 | public class LoginFilter implements Filter { 14 | public void init(FilterConfig config) throws ServletException { 15 | } 16 | 17 | public void destroy() { 18 | } 19 | 20 | @Override 21 | public void doFilter(ServletRequest rq, ServletResponse resp, FilterChain chain) throws ServletException, IOException { 22 | HttpServletRequest request = (HttpServletRequest) rq; 23 | String uri = request.getRequestURI(); 24 | System.out.println("uri: " + uri); 25 | // 验证请求路径 26 | if (uri.contains("login.jsp") || uri.contains("Login") || uri.endsWith(".css") || uri.endsWith(".js") 27 | || uri.endsWith(".jpg") || uri.endsWith(".png")) { 28 | chain.doFilter(rq, resp); 29 | } else { 30 | HttpSession session = request.getSession(); 31 | User userInfo = (User) session.getAttribute("userInfo"); 32 | if (userInfo != null) { 33 | System.out.println("ROLE = " + userInfo.getRole()); 34 | chain.doFilter(rq, resp); 35 | } else { 36 | request.setAttribute("login_msg", "您尚未登陆,请先登陆!"); 37 | request.getRequestDispatcher("/login.jsp").forward(request, resp); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/UpdateServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.entiry.Book; 4 | import com.hu.service.BookService; 5 | import com.hu.service.impl.BookServiceImpl; 6 | 7 | import javax.servlet.*; 8 | import javax.servlet.http.*; 9 | import javax.servlet.annotation.*; 10 | import java.io.IOException; 11 | import java.util.List; 12 | 13 | @WebServlet(name = "UpdateServlet", value = "/UpdateServlet") 14 | public class UpdateServlet extends HttpServlet { 15 | 16 | private final BookService bookService = new BookServiceImpl(); 17 | 18 | @Override 19 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 | String id = request.getParameter("id"); 21 | String name = request.getParameter("name"); 22 | String author = request.getParameter("author"); 23 | String price = request.getParameter("price"); 24 | String press = request.getParameter("press"); 25 | String isbn = request.getParameter("isbn"); 26 | String intro = request.getParameter("intro"); 27 | Book book = new Book(Integer.parseInt(id), name, author, Double.parseDouble(price), press, isbn, intro); 28 | System.out.println(book); 29 | bookService.updateBook(book); 30 | List bookList = bookService.getBookList(1, 10, null); 31 | request.getSession().setAttribute("bookList", bookList); 32 | response.sendRedirect(request.getContextPath() + "/index.jsp"); 33 | } 34 | 35 | @Override 36 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/LoginServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.entiry.User; 4 | import com.hu.service.UserService; 5 | import com.hu.service.impl.UserServiceImpl; 6 | 7 | import javax.servlet.*; 8 | import javax.servlet.http.*; 9 | import javax.servlet.annotation.*; 10 | import java.io.IOException; 11 | import java.io.PrintWriter; 12 | 13 | @WebServlet(name = "LoginServlet", value = "/LoginServlet") 14 | public class LoginServlet extends HttpServlet { 15 | private final UserService userService = new UserServiceImpl(); 16 | 17 | @Override 18 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 | 20 | } 21 | 22 | @Override 23 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 | String username = request.getParameter("username"); 25 | String password = request.getParameter("password"); 26 | System.out.println("username: " + username); 27 | System.out.println("password: " + password); 28 | User user; 29 | try { 30 | user = userService.login(username, password); 31 | HttpSession session = request.getSession(); 32 | session.setAttribute("userInfo", user); 33 | response.sendRedirect(request.getContextPath() + "/GetServlet"); 34 | } catch (RuntimeException e) { 35 | String msg = e.getMessage(); 36 | System.out.println(msg); 37 | request.setAttribute("msg", msg); 38 | request.getRequestDispatcher("/login.jsp").forward(request, response); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/hu/servlet/GetServlet.java: -------------------------------------------------------------------------------- 1 | package com.hu.servlet; 2 | 3 | import com.hu.entiry.Book; 4 | import com.hu.service.BookService; 5 | import com.hu.service.impl.BookServiceImpl; 6 | 7 | import javax.servlet.*; 8 | import javax.servlet.http.*; 9 | import javax.servlet.annotation.*; 10 | import java.io.IOException; 11 | import java.util.List; 12 | 13 | @WebServlet(name = "GetServlet", value = "/GetServlet") 14 | public class GetServlet extends HttpServlet { 15 | 16 | private final BookService bookService = new BookServiceImpl(); 17 | 18 | @Override 19 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 | String current = request.getParameter("current"); 21 | String size = request.getParameter("size"); 22 | int c, s; 23 | if (current == null || "".equals(current)) { 24 | c = 1; 25 | } else { 26 | c = Integer.parseInt(current); 27 | } 28 | if (size == null || "".equals(size)) { 29 | s = 10; 30 | } else { 31 | s = Integer.parseInt(size); 32 | } 33 | String key = request.getParameter("key"); 34 | List bookList = bookService.getBookList(c, s, key); 35 | int total = bookService.getTotal(c, s, key); 36 | int page = (total - 1) / 10 + 1; 37 | request.getSession().setAttribute("bookList", bookList); 38 | request.getSession().setAttribute("page", page); 39 | request.getSession().setAttribute("current", c); 40 | response.sendRedirect(request.getContextPath() + "/index.jsp"); 41 | } 42 | 43 | @Override 44 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/webapp/static/css/index.css: -------------------------------------------------------------------------------- 1 | .container { 2 | text-align: center; 3 | } 4 | 5 | body { 6 | background-color: #F5F5F5; 7 | } 8 | 9 | #s1 { 10 | margin: 0 10px; 11 | color: #ccc; 12 | } 13 | 14 | #lead { 15 | height: 25px; 16 | background-color: #E2E3E4FF; 17 | text-align: right; 18 | } 19 | #lead a { 20 | margin-right: 20px; 21 | } 22 | .edit { 23 | display: none; 24 | } 25 | 26 | .edit input { 27 | width: 50px; 28 | 29 | } 30 | 31 | 32 | #p0 { 33 | display: block; 34 | margin-block-start: 1em; 35 | margin-block-end: 1em; 36 | margin-inline-start: 0; 37 | margin-inline-end: 0; 38 | font-size: 13px; 39 | margin-top: 2px; 40 | } 41 | 42 | #p0 a { 43 | color: #666; 44 | text-decoration: none; 45 | } 46 | 47 | .search { 48 | margin: 30px auto; 49 | } 50 | 51 | .right { 52 | float: right; 53 | } 54 | .search input:first-child { 55 | width: 400px; 56 | height: 30px; 57 | } 58 | 59 | .search button { 60 | width: 60px; 61 | height: 30px; 62 | } 63 | 64 | .bookList { 65 | margin: auto; 66 | text-align: left; 67 | height: 600px; 68 | } 69 | 70 | td { 71 | font-size: 16px; 72 | } 73 | 74 | 75 | .bookItem > img { 76 | height: 70%; 77 | width: 90%; 78 | margin-top: 5px; 79 | } 80 | 81 | .bookItem > .title { 82 | margin-top: 10px; 83 | font-family: 宋体, ui-monospace; 84 | } 85 | 86 | .bookItem > .author { 87 | margin-top: 5px; 88 | font-family: 宋体, ui-monospace; 89 | color: #a9a9a9; 90 | } 91 | 92 | .bookItem .discount { 93 | font-family: Arial, ui-monospace; 94 | color: red; 95 | } 96 | 97 | .bookItem .original { 98 | font-family: Arial, ui-monospace; 99 | color: #a9a9a9; 100 | text-decoration-line: line-through; 101 | } 102 | 103 | .footer { 104 | width: 100%; 105 | float: left; 106 | margin-top: 50px; 107 | margin-bottom: 20px; 108 | } -------------------------------------------------------------------------------- /src/main/java/com/hu/dao/impl/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.dao.impl; 2 | 3 | import com.hu.dao.UserDao; 4 | import com.hu.entiry.User; 5 | import com.hu.utils.JDBCUtil; 6 | 7 | import java.sql.Connection; 8 | import java.sql.PreparedStatement; 9 | import java.sql.ResultSet; 10 | import java.sql.SQLException; 11 | import java.util.Objects; 12 | 13 | /** 14 | * @author suhu 15 | * @createDate 2021/11/21 16 | */ 17 | public class UserDaoImpl implements UserDao { 18 | @Override 19 | public boolean login(String username, String password) { 20 | Connection connection = JDBCUtil.getConnection(); 21 | String sql = "select * from user where username=? and password=?"; 22 | PreparedStatement ps = null; 23 | ResultSet set = null; 24 | try { 25 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 26 | ps.setString(1, username); 27 | ps.setString(2, password); 28 | set = ps.executeQuery(); 29 | if (set.next()) return true; 30 | } catch (SQLException e) { 31 | e.printStackTrace(); 32 | } finally { 33 | JDBCUtil.closeAll(connection, ps, set); 34 | } 35 | return false; 36 | } 37 | 38 | @Override 39 | public User getByName(String username) { 40 | Connection connection = JDBCUtil.getConnection(); 41 | String sql = "select * from user where username=?"; 42 | PreparedStatement ps = null; 43 | ResultSet set = null; 44 | try { 45 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 46 | ps.setString(1, username); 47 | set = ps.executeQuery(); 48 | if (set.next()) { 49 | int id = set.getInt("id"); 50 | String role = set.getString("role"); 51 | return new User(id, username, role); 52 | } else 53 | return null; 54 | } catch (SQLException e) { 55 | e.printStackTrace(); 56 | } finally { 57 | JDBCUtil.closeAll(connection, ps, set); 58 | } 59 | return null; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.hu 8 | final 9 | 1.0-SNAPSHOT 10 | final 11 | war 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 5.7.1 18 | 19 | 20 | 21 | 22 | javax.servlet 23 | javax.servlet-api 24 | 4.0.1 25 | provided 26 | 27 | 28 | org.junit.jupiter 29 | junit-jupiter-api 30 | ${junit.version} 31 | test 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-engine 36 | ${junit.version} 37 | test 38 | 39 | 40 | mysql 41 | mysql-connector-java 42 | 8.0.27 43 | 44 | 45 | javax.servlet 46 | jstl 47 | 1.2 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.apache.maven.plugins 55 | maven-war-plugin 56 | 3.3.1 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/main/java/com/hu/entiry/Book.java: -------------------------------------------------------------------------------- 1 | package com.hu.entiry; 2 | 3 | /** 4 | * @author suhu 5 | * @createDate 2021/11/21 6 | */ 7 | public class Book { 8 | private int id; 9 | private String name; 10 | private String author; 11 | private double price; 12 | private String press; 13 | private String isbn; 14 | private String intro; 15 | 16 | public int getId() { 17 | return id; 18 | } 19 | 20 | public void setId(int id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public String getAuthor() { 33 | return author; 34 | } 35 | 36 | public void setAuthor(String author) { 37 | this.author = author; 38 | } 39 | 40 | public double getPrice() { 41 | return price; 42 | } 43 | 44 | public void setPrice(double price) { 45 | this.price = price; 46 | } 47 | 48 | public String getPress() { 49 | return press; 50 | } 51 | 52 | public void setPress(String press) { 53 | this.press = press; 54 | } 55 | 56 | public String getIntro() { 57 | return intro; 58 | } 59 | 60 | public void setIntro(String intro) { 61 | this.intro = intro; 62 | } 63 | 64 | public String getIsbn() { 65 | return isbn; 66 | } 67 | 68 | public void setIsbn(String isbn) { 69 | this.isbn = isbn; 70 | } 71 | 72 | public Book() { 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return "Book{" + 78 | "id=" + id + 79 | ", name='" + name + '\'' + 80 | ", author='" + author + '\'' + 81 | ", price=" + price + 82 | ", press='" + press + '\'' + 83 | ", isbn='" + isbn + '\'' + 84 | ", intro='" + intro + '\'' + 85 | '}'; 86 | } 87 | 88 | public Book(int id, String name, String author, double price, String press, String isbn, String intro) { 89 | this.id = id; 90 | this.name = name; 91 | this.author = author; 92 | this.price = price; 93 | this.press = press; 94 | this.isbn = isbn; 95 | this.intro = intro; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/hu/utils/JDBCUtil.java: -------------------------------------------------------------------------------- 1 | package com.hu.utils; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.sql.*; 6 | import java.util.Properties; 7 | 8 | /** 9 | * @author suhu 10 | * @createDate 2021/11/21 11 | */ 12 | public class JDBCUtil { 13 | private static String URL; 14 | private static String USERNAME; 15 | private static String PASSWORD; 16 | 17 | // 静态代码块,类加载时执行 18 | static { 19 | try { 20 | Properties properties = new Properties(); 21 | InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"); 22 | try { 23 | properties.load(in); 24 | } catch (IOException e) { 25 | e.printStackTrace(); 26 | } 27 | String DRIVER = properties.getProperty("driver"); 28 | URL = properties.getProperty("url"); 29 | USERNAME = properties.getProperty("username"); 30 | PASSWORD = properties.getProperty("password"); 31 | Class.forName(DRIVER); 32 | } catch (ClassNotFoundException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | 37 | static ThreadLocal local = new ThreadLocal<>(); 38 | 39 | public static Connection getConnection() { 40 | try { 41 | //从ThreadLocal中取对象 42 | Connection connection = local.get(); 43 | //判断ThreadLocal中是否为空 44 | if (connection == null) { 45 | connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); 46 | local.set(connection); 47 | return connection; 48 | } else 49 | return local.get(); 50 | } catch (SQLException throwables) { 51 | throwables.printStackTrace(); 52 | } 53 | return null; 54 | } 55 | 56 | // 关闭资源 57 | public static void closeAll(Connection conn, Statement stat, ResultSet rs) { 58 | try { 59 | if (conn != null) { 60 | conn.close(); 61 | local.remove(); 62 | } 63 | if (stat != null) stat.close(); 64 | if (rs != null) rs.close(); 65 | } catch (SQLException throwables) { 66 | throwables.printStackTrace(); 67 | } 68 | } 69 | 70 | //开启事务 71 | public static void begin() { 72 | Connection connection; 73 | try { 74 | connection = getConnection(); 75 | connection.setAutoCommit(false); 76 | } catch (SQLException throwables) { 77 | throwables.printStackTrace(); 78 | } 79 | } 80 | 81 | // 提交事务 82 | public static void commit() { 83 | Connection connection = null; 84 | try { 85 | connection = getConnection(); 86 | connection.commit(); 87 | } catch (SQLException throwables) { 88 | throwables.printStackTrace(); 89 | } finally { 90 | closeAll(connection, null, null); 91 | } 92 | } 93 | 94 | // 回滚 95 | public static void rollback() { 96 | Connection connection = null; 97 | try { 98 | connection = getConnection(); 99 | connection.rollback(); 100 | } catch (SQLException throwables) { 101 | throwables.printStackTrace(); 102 | } finally { 103 | closeAll(connection, null, null); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/webapp/addBook.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | 添加书籍 5 | 7 | 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 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /src/main/webapp/updateBook.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | 修改书籍 5 | 6 | 8 | 22 | 23 | 24 |
25 |
26 |

修改书籍

27 |
28 | 29 |
30 | 31 |
32 | 34 |
35 |
36 |
37 | 38 |
39 | 41 |
42 |
43 |
44 | 45 |
46 | 48 |
49 |
50 |
51 | 52 |
53 | 55 |
56 |
57 |
58 | 59 |
60 | 62 |
63 |
64 |
65 | 66 |
67 | 68 |
69 |
70 | <%--
--%> 71 |
72 |
73 | 74 | 75 | 76 |
77 |
78 |
79 |
80 |
81 | 82 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 | <%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %> 3 | 4 | <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 5 | 6 | 7 | 8 | 9 | 图书管理系统 10 | 11 | 12 | 14 | 15 | 16 |
17 | 注销 18 |
19 |

图书管理系统

20 |
21 | 22 | 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 | 61 | 62 | 63 | 64 |
序号书名作者价格出版社操作
${status.count + (sessionScope.current-1)*10}${book.name}${book.author}${book.price}${book.press} 56 | 删除 57 | | 58 | 编辑 | 59 | 查看详情 60 |
65 | 66 |
67 | 91 |
92 | 93 |
94 | 122 | 123 | -------------------------------------------------------------------------------- /sql/store.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat MySQL Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 80021 7 | Source Host : localhost:3306 8 | Source Schema : store 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 80021 12 | File Encoding : 65001 13 | 14 | Date: 18/12/2021 18:02:12 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for book 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `book`; 24 | CREATE TABLE `book` ( 25 | `id` int NOT NULL AUTO_INCREMENT, 26 | `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, 27 | `author` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, 28 | `price` float NULL DEFAULT NULL, 29 | `press` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, 30 | `isbn` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, 31 | `intro` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL, 32 | PRIMARY KEY (`id`) USING BTREE 33 | ) ENGINE = InnoDB AUTO_INCREMENT = 31 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 34 | 35 | -- ---------------------------- 36 | -- Records of book 37 | -- ---------------------------- 38 | INSERT INTO `book` VALUES (1, 'Python编程 从入门到实践 第2版', '[美]埃里克·马瑟斯', 105.1, '人民邮电出版社', '9787115546081', '零基础学Python编程教程书籍,数据分析、网络爬虫、深度学习技能,畅销经典蟒蛇书升级版,附赠源代码、练习答案、学习视频、学习速查地图读者交流群等资源。'); 39 | INSERT INTO `book` VALUES (8, 'C++ Primer Plus 第6版 中文版', '[美] 史蒂芬·普拉达', 59, '人民邮电出版社', '9787115521644', 'C++程序设计经典教程,畅销30年的C++大百科全书全新升级,经典C++入门教程十年新版再现,孟岩、高博倾力推荐,赠送价值99元的e读版电子书及在线实验环境,赠送大尺寸全书思维导图,赠199元训练营'); 40 | INSERT INTO `book` VALUES (9, '利用Python进行数据分析(原书第2版)', '[美]韦斯·麦金尼', 110.5, '机械工业出版社', '9787111603702', 'Python数据分析经典畅销书全新升级,第1版中文版累计销售100000册 Python pandas创始人亲自执笔,Python语言的核心开发人员鼎立推荐 针对Python 3.6进行全面修订和更新'); 41 | INSERT INTO `book` VALUES (10, 'JavaScript高级程序设计 第4版', '[美]马特·弗里斯比', 123.5, '人民邮电出版社', '9787115545381', 'web前端开发经典教程,JS“红宝书”全新升级,入门+实战,涵盖ECMAScript 2019,提供教学视频+配套编程环境,可直接在线运行随书代码'); 42 | INSERT INTO `book` VALUES (11, '深入理解Java虚拟机:JVM高级特性与实践(第3版)', '周志明', 119.8, '机械工业出版社', '9787111641247', '周志明虚拟机新作,第3版新增内容近50%,5个维度全面剖析JVM,大厂面试知识点全覆盖。与 Java编程思想、Effective Java、Java核心技术 堪称:Java四大名著'); 43 | INSERT INTO `book` VALUES (12, 'Head First Java(中文版)', '[美]塞若,[美]贝茨', 37.2, '中国电力出版社', '9787508344980', '10年畅销经典,累计印刷30多次,畅销近30万册,计算机图书十大好书之一。'); 44 | INSERT INTO `book` VALUES (13, 'Spring Cloud Alibaba 微服务架构实战派(上下册)', '胡弦', 118, '电子工业出版社', '9787121423130', '学完Springboot该学微服务,学这本了。不仅有Spring Cloud Alibaba,还有微服务涉及的相关技术(比如Seata、Skywalking、Redis、RocketMQ等),实战性强'); 45 | INSERT INTO `book` VALUES (14, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 46 | INSERT INTO `book` VALUES (15, 'C++ Primer中文版(第5版)', '[美]李普曼 等', 118.9, '电子工业出版社', '9787121155352', 'C++学习头牌 全球读者千万 全面采用新标 技术影响力图书冠军'); 47 | INSERT INTO `book` VALUES (16, 'C++并发编程实战(第2版)', '[英]安东尼·威廉姆斯', 133.8, '人民邮电出版社', '9787115573551', 'C++并发编程深度指南,C++标准委员会成员编写,囊括C++并发编程多个方面,代码附有中文注释简洁易懂,附赠配套代码文件。'); 48 | INSERT INTO `book` VALUES (17, 'Java核心技术 卷I 基础知识(原书第11版)', '[美],凯·S.霍斯特曼', 138.4, '机械工业出版社', '9787111636663', 'Core Java 第11版,Jolt大奖获奖作品,针对Java SE9、10、11全面更新,与Java编程思想、Effective Java、深入理解Java虚拟机 堪称:Java四大名著'); 49 | INSERT INTO `book` VALUES (18, '算法(第4版)', '[美]Robert Sedgewick Kevin Wayne', 124.3, '人民邮电出版社', '9787115293800', '【图灵程序设计丛书】算法领域的经典参考书 众多图例实现图解算法和算法导论 基于Java语言实现算法和数据结构 熟练掌握算法设计与分析'); 50 | INSERT INTO `book` VALUES (19, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 51 | INSERT INTO `book` VALUES (20, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 52 | INSERT INTO `book` VALUES (22, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 53 | INSERT INTO `book` VALUES (23, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 54 | INSERT INTO `book` VALUES (24, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 55 | INSERT INTO `book` VALUES (25, 'Java编程思想(第4版)', '[美] Bruce Eckel', 100.3, '机械工业出版社', '9787111213826', 'Java学习经典,殿堂级著作!赢得了全球程序员的广泛赞誉。'); 56 | INSERT INTO `book` VALUES (26, 'C++ Primer中文版(第5版)', '[美]李普曼 等', 118.9, '电子工业出版社', '9787121155352', 'C++学习头牌 全球读者千万 全面采用新标 技术影响力图书冠军'); 57 | INSERT INTO `book` VALUES (27, 'C++ Primer中文版(第5版)', '[美]李普曼 等', 118.9, '电子工业出版社', '9787121155352', 'C++学习头牌 全球读者千万 全面采用新标 技术影响力图书冠军'); 58 | INSERT INTO `book` VALUES (28, 'C++ Primer中文版(第5版)', '[美]李普曼 等', 118.9, '电子工业出版社', '9787121155352', 'C++学习头牌 全球读者千万 全面采用新标 技术影响力图书冠军'); 59 | INSERT INTO `book` VALUES (29, 'C++ Primer中文版(第5版)', '[美]李普曼 等', 118.9, '电子工业出版社', '9787121155352', 'C++学习头牌 全球读者千万 全面采用新标 技术影响力图书冠军'); 60 | 61 | -- ---------------------------- 62 | -- Table structure for user 63 | -- ---------------------------- 64 | DROP TABLE IF EXISTS `user`; 65 | CREATE TABLE `user` ( 66 | `id` int NOT NULL AUTO_INCREMENT, 67 | `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, 68 | `password` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, 69 | `role` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, 70 | PRIMARY KEY (`id`, `username`) USING BTREE 71 | ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 72 | 73 | -- ---------------------------- 74 | -- Records of user 75 | -- ---------------------------- 76 | INSERT INTO `user` VALUES (1, 'admin', 'admin', 'admin'); 77 | 78 | SET FOREIGN_KEY_CHECKS = 1; 79 | -------------------------------------------------------------------------------- /src/main/java/com/hu/dao/impl/BookDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.dao.impl; 2 | 3 | import com.hu.dao.BookDao; 4 | import com.hu.entiry.Book; 5 | import com.hu.utils.JDBCUtil; 6 | 7 | import java.sql.Connection; 8 | import java.sql.PreparedStatement; 9 | import java.sql.ResultSet; 10 | import java.sql.SQLException; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | import java.util.Objects; 14 | 15 | /** 16 | * @author suhu 17 | * @createDate 2021/11/21 18 | */ 19 | public class BookDaoImpl implements BookDao { 20 | 21 | @Override 22 | public List getBookList(int current, int size, String key) { 23 | List bookList = new ArrayList<>(); 24 | Connection connection = JDBCUtil.getConnection(); 25 | PreparedStatement ps = null; 26 | ResultSet rs = null; 27 | String sql = "select id, name, author, price, press, isbn, intro from book " + 28 | "where name like ? or author like ? or press like ? " + 29 | " limit ?,?"; 30 | if (key == null) { 31 | key = ""; 32 | } 33 | try { 34 | JDBCUtil.begin(); 35 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 36 | ps.setString(1, "%" + key + "%"); 37 | ps.setString(2, "%" + key + "%"); 38 | ps.setString(3, "%" + key + "%"); 39 | ps.setInt(4, (current - 1) * size); 40 | ps.setInt(5, size); 41 | rs = ps.executeQuery(); 42 | while (rs.next()) { 43 | Book book = new Book(); 44 | book.setId(rs.getInt("id")); 45 | book.setName(rs.getString("name")); 46 | book.setAuthor(rs.getString("author")); 47 | book.setPrice(rs.getDouble("price")); 48 | book.setPress(rs.getString("press")); 49 | book.setIntro(rs.getString("intro")); 50 | book.setIsbn(rs.getString("isbn")); 51 | bookList.add(book); 52 | } 53 | JDBCUtil.commit(); 54 | 55 | } catch (SQLException e) { 56 | e.printStackTrace(); 57 | JDBCUtil.rollback(); 58 | } finally { 59 | JDBCUtil.closeAll(connection, ps, rs); 60 | } 61 | return bookList; 62 | } 63 | 64 | @Override 65 | public int getTotal(int current, int size, String key) { 66 | Connection connection = JDBCUtil.getConnection(); 67 | PreparedStatement ps = null; 68 | ResultSet rs = null; 69 | String sql = "select count(1) from book " + 70 | "where name like ? or author like ? or press like ?"; 71 | if (key == null) { 72 | key = ""; 73 | } 74 | int total = 0; 75 | try { 76 | JDBCUtil.begin(); 77 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 78 | ps.setString(1, "%" + key + "%"); 79 | ps.setString(2, "%" + key + "%"); 80 | ps.setString(3, "%" + key + "%"); 81 | rs = ps.executeQuery(); 82 | if (rs.next()) { 83 | total = rs.getInt(1); 84 | } 85 | JDBCUtil.commit(); 86 | } catch (SQLException e) { 87 | e.printStackTrace(); 88 | JDBCUtil.rollback(); 89 | } finally { 90 | JDBCUtil.closeAll(connection, ps, rs); 91 | } 92 | return total; 93 | } 94 | 95 | @Override 96 | public void updateBook(Book book) { 97 | Connection connection = JDBCUtil.getConnection(); 98 | PreparedStatement ps = null; 99 | String sql = "update book set name=?, author=?, price=?, press=?, intro=?, isbn=? where id=?"; 100 | try { 101 | JDBCUtil.begin(); 102 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 103 | ps.setString(1, book.getName()); 104 | ps.setString(2, book.getAuthor()); 105 | ps.setDouble(3, book.getPrice()); 106 | ps.setString(4, book.getPress()); 107 | ps.setString(5, book.getIntro()); 108 | ps.setString(6, book.getIsbn()); 109 | ps.setInt(7, book.getId()); 110 | ps.executeUpdate(); 111 | JDBCUtil.commit(); 112 | 113 | } catch (SQLException e) { 114 | e.printStackTrace(); 115 | JDBCUtil.rollback(); 116 | } finally { 117 | JDBCUtil.closeAll(connection, ps, null); 118 | } 119 | } 120 | 121 | @Override 122 | public void deleteBook(int id) { 123 | Connection connection = JDBCUtil.getConnection(); 124 | PreparedStatement ps = null; 125 | String sql = "delete from book where id=?"; 126 | try { 127 | JDBCUtil.begin(); 128 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 129 | ps.setInt(1, id); 130 | ps.executeUpdate(); 131 | JDBCUtil.commit(); 132 | } catch (SQLException e) { 133 | e.printStackTrace(); 134 | JDBCUtil.rollback(); 135 | } finally { 136 | JDBCUtil.closeAll(connection, ps, null); 137 | } 138 | } 139 | 140 | @Override 141 | public void insertBook(Book book) { 142 | Connection connection = JDBCUtil.getConnection(); 143 | PreparedStatement ps = null; 144 | String sql = "insert into book (name, author, price, press, isbn, intro) values (?,?,?,?,?,?)"; 145 | try { 146 | JDBCUtil.begin(); 147 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 148 | ps.setString(1, book.getName()); 149 | ps.setString(2, book.getAuthor()); 150 | ps.setDouble(3, book.getPrice()); 151 | ps.setString(4, book.getPress()); 152 | ps.setString(5, book.getIsbn()); 153 | ps.setString(6, book.getIntro()); 154 | ps.executeUpdate(); 155 | JDBCUtil.commit(); 156 | } catch (SQLException e) { 157 | e.printStackTrace(); 158 | JDBCUtil.rollback(); 159 | } finally { 160 | JDBCUtil.closeAll(connection, ps, null); 161 | } 162 | } 163 | 164 | @Override 165 | public Book getBookById(int id) { 166 | Book book = new Book(); 167 | Connection connection = JDBCUtil.getConnection(); 168 | PreparedStatement ps = null; 169 | ResultSet rs = null; 170 | String sql = "select * from book where id=?"; 171 | try { 172 | JDBCUtil.begin(); 173 | ps = Objects.requireNonNull(connection).prepareStatement(sql); 174 | ps.setInt(1, id); 175 | rs = ps.executeQuery(); 176 | if (rs.next()) { 177 | book.setId(id); 178 | book.setName(rs.getString(2)); 179 | book.setAuthor(rs.getString(3)); 180 | book.setPrice(rs.getDouble(4)); 181 | book.setPress(rs.getString(5)); 182 | book.setIsbn(rs.getString(6)); 183 | book.setIntro(rs.getString(7)); 184 | } 185 | JDBCUtil.commit(); 186 | } catch (SQLException e) { 187 | e.printStackTrace(); 188 | JDBCUtil.rollback(); 189 | } finally { 190 | JDBCUtil.closeAll(connection, ps, rs); 191 | } 192 | return book; 193 | } 194 | } 195 | --------------------------------------------------------------------------------