图书管理系统,增,删,改,查 后台实现

该博客介绍了采用三层架构设计的图书管理系统,利用MVC模式和JDBC操作MySQL数据库,实现了增、删、改、查功能。系统结构包括表现层、业务层和数据访问层。此外,还涉及了分页功能的实现,各功能模块通过不同的包组织,包括实体类、Servlet控制器、业务接口及实现、DAO接口及实现、分页响应、测试类和工具类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四、案例

  1. 采用三层架构设计程序架构

    表现层: MVC设计模式,主要用于与用户交互

    业务层 : 处理具体业务

    数据访问层 : 对数据库进行操作

  2. 数据库: MySQL关系型数据库

  3. JDBC: 操作数据库

功能需求:

图书管理系统 增、删、改、查功能

定义存放实体类的包 (数据bean)

com.xxx.pojo | com.xxx.beans | com.xxx.domain

package com.xxx.pojo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Book {
    private int bookId;
    private String bookName;
    private String author;
    private int price;
    private Date pubDate;
    private String publisher;
    private String pubDateStr;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Date getPubDate() {
        return pubDate;
    }

    public void setPubDate(Date pubDate) {
        this.pubDate = pubDate;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getPubDateStr() {
        return pubDateStr;
    }

    public void setPubDateStr(String pubDateStr) {
        this.pubDateStr = pubDateStr;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", pubDate=" + pubDate +
                ", publisher='" + publisher + '\'' +
                ", pubDateStr='" + pubDateStr + '\'' +
                '}';
    }
}

分页

package com.xxx.pojo;

import java.util.List;


public class PageInfo {
    private int curPage = 1;  // 记录当前页面
    private int pageSize = 5; // 记录每页显示的记录数
    private List<Book> books;  // 记录当前页的书的集合 数据库查询出来
    private int count;        // 书的总记录数  数据库查出来
    private int total;        // 记录总页数  计算出来

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getTotal() {
        total = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "curPage=" + curPage +
                ", pageSize=" + pageSize +
                ", books=" + books +
                ", count=" + count +
                ", total=" + getTotal() +
                '}';
    }
}

定义存放servlet的包(控制器)

com.xxx.web.servlet | com.xxx.web.controller

package com.xxx.servlet;

import com.xxx.pojo.Book;
import com.xxx.response.ResponseResult;
import com.xxx.service.IBookService;
import com.xxx.service.impl.BookServiceImpl;
import com.xxx.utils.StringDateConverter;

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 java.io.IOException;
import java.util.Date;


@WebServlet("/book/add")
public class BookAddServlet extends HttpServlet {

    IBookService service = new BookServiceImpl();


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 需要接受前端提交过来的数据
        // 1.设置请求正文的字符编码
        request.setCharacterEncoding("utf-8");
        // 2.获取请求参数
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String priceStr = request.getParameter("price");
        String pubDateStr = request.getParameter("pubDate");
        String publisher = request.getParameter("publisher");

        // 3.后台验证
        if(bookName == null || bookName.trim().length() == 0) {
            ResponseResult result = new ResponseResult(99999,  "非法的请求参数!");
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值