Java全栈web网页技术:15.书城项目实战四:管理端图书的增删改查(后台)

Posted new nm个对象

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java全栈web网页技术:15.书城项目实战四:管理端图书的增删改查(后台)相关的知识,希望对你有一定的参考价值。

1.需求澄清

管理端图书管理页面【book_manager.jsp】页面原型如下:
需要实现图书的添加、删除、修改、分页显示等操作

2.前期准备

2.1 创建数据表books

create TABLE books(
id INT PRIMARY KEY auto_increment,
title varchar(50),
author VARCHAR(50),
price DOUBLE(10,2),
sales INT,
stock INT,
img_path VARCHAR(50)
) charset "utf8";

数据表结构如下:

添加初始数据:


INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('解忧杂货店','东野圭吾',27.20,100,100,'static/uploads/jieyouzahuodian.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('边城','沈从文',23.00,100,100,'static/uploads/biancheng.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('中国哲学史','冯友兰',44.5,100,100,'static/uploads/zhongguozhexueshi.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('忽然七日',' 劳伦',19.33,100,100,'static/uploads/huranqiri.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('苏东坡传','林语堂',19.30,100,100,'static/uploads/sudongpozhuan.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('百年孤独','马尔克斯',29.50,100,100,'static/uploads/bainiangudu.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('扶桑','严歌苓',19.8,100,100,'static/uploads/fusang.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('给孩子的诗','北岛',22.20,100,100,'static/uploads/geihaizideshi.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('为奴十二年','所罗门',16.5,100,100,'static/uploads/weinushiernian.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('平凡的世界','路遥',55.00,100,100,'static/uploads/pingfandeshijie.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('悟空传','今何在',14.00,100,100,'static/uploads/wukongzhuan.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('硬派健身','斌卡',31.20,100,100,'static/uploads/yingpaijianshen.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('从晚清到民国','唐德刚',39.90,100,100,'static/uploads/congwanqingdaominguo.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('三体','刘慈欣',56.5,100,100,'static/uploads/santi.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('看见','柴静',19.50,100,100,'static/uploads/kanjian.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('活着','余华',11.00,100,100,'static/uploads/huozhe.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('小王子','安托万',19.20,100,100,'static/uploads/xiaowangzi.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('我们仨','杨绛',11.30,100,100,'static/uploads/womensa.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('生命不息,折腾不止','罗永浩',25.20,100,100,'static/uploads/shengmingbuxi.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('皮囊','蔡崇达',23.90,100,100,'static/uploads/pinang.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('恰到好处的幸福','毕淑敏',16.40,100,100,'static/uploads/qiadaohaochudexingfu.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('大数据预测','埃里克',37.20,100,100,'static/uploads/dashujuyuce.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('人月神话','布鲁克斯',55.90,100,100,'static/uploads/renyueshenhua.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('C语言入门经典','霍尔顿',45.00,100,100,'static/uploads/cyuyanrumenjingdian.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('数学之美','吴军',29.90,100,100,'static/uploads/shuxuezhimei.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('Java编程思想','埃史尔',70.50,100,100,'static/uploads/Javabianchengsixiang.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('设计模式之禅','秦小波',20.20,100,100,'static/uploads/shejimoshizhichan.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('图解机器学习','杉山将',33.80,100,100,'static/uploads/tujiejiqixuexi.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('艾伦图灵传','安德鲁',47.20,100,100,'static/uploads/ailuntulingzhuan.jpg');
INSERT INTO books (title, author ,price, sales , stock , img_path) VALUES('教父','马里奥普佐',29.00,100,100,'static/uploads/jiaofu.jpg');

效果如下:

2.2 创建JavaBean Book类

package com.bookstore.bean;

/**
 * 图书类
 */
public class Book 
    private Integer id;
    private String title; //书名
    private String author; //作者
    private double price; //价格
    private Integer sales; //销量
    private Integer stock; //库存
    private String imgPath = "static//img//default.jpg"; //封面图片的路径,因为存在用户未上传封面的情况,所以添加一个默认封面

    public Book(Integer id, String title, String author, double price, Integer sales, Integer stock, String imgPath) 
        this.id = id;
        this.title = title;
        this.author = author;
        this.price = price;
        this.sales = sales;
        this.stock = stock;
        this.imgPath = "static//img//default.jpg"; //封面图片的路径,因为存在用户未上传封面的情况,所以添加一个默认封面
    

    public Book() 
    

    @Override
    public String toString() 
        return "Book" +
                "id=" + id +
                ", title='" + title + '\\'' +
                ", author='" + author + '\\'' +
                ", price=" + price +
                ", sales=" + sales +
                ", stock=" + stock +
                ", imgPath='" + imgPath + '\\'' +
                '';
    

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getTitle() 
        return title;
    

    public void setTitle(String title) 
        this.title = title;
    

    public String getAuthor() 
        return author;
    

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

    public double getPrice() 
        return price;
    

    public void setPrice(double price) 
        this.price = price;
    

    public Integer getSales() 
        return sales;
    

    public void setSales(Integer sales) 
        this.sales = sales;
    

    public Integer getStock() 
        return stock;
    

    public void setStock(Integer stock) 
        this.stock = stock;
    

    public String getImgPath() 
        return imgPath;
    

    public void setImgPath(String imgPath) 
        this.imgPath = imgPath;
    

2.代码实现

2.1 查询所有图书需求

我们从首页点击后台管理,可以进入图书后台管理页面,再该页面会展示所有的图书,并且可以对图书进行增删改查操作。现在我们就先实现,查询所有图书并展示在页面上

(1)Dao层实现

创建BookDao接口及实现类,并实现方法public List<Book> getBookList(); 获取所有的图书

  • BookDao接口如下:

    package com.bookstore.dao;
    
    import com.bookstore.bean.Book;
    import java.util.List;
    
    public interface BookDao 
    
        /**
         * 获取数据库中的所有book
         * @return List<Book>
         */
        public List<Book> getBookList();
    
    
  • BookDaoImpl类实现如下:

    package com.bookstore.dao.impl;
    
    import com.bookstore.bean.Book;
    import com.bookstore.dao.BaseDao;
    import com.bookstore.dao.BookDao;
    
    import java.util.List;
    
    /**
     * 获取数据库中的所有book
     */
    public class BookDaoImpl extends BaseDao<Book> implements BookDao 
        @Override
        public List<Book> getBookList() 
            String sql = "SELECT id,title,author,price,sales,stock,img_path as imgPath FROM books";
            List<Book> bookList = getBeanList(Book.class, sql);
            return bookList;
        
    
    
  • 在test目录下,写个单元测试来测试一下代码

    package com.bookstore.test;
    
    import com.bookstore.bean.Book;
    import com.bookstore.dao.impl.BookDaoImpl;
    import org.junit.Test;
    
    import java.util.List;
    
    public class BookDaoTest 
    
        @Test
        public void getAllBooktest()
            BookDaoImpl bookDao = new BookDaoImpl();
            List<Book> bookList = bookDao.getBookList();
            bookList.forEach(System.out::println);
        
    
    

(2)service层实现

创建BookService接口及实现类,并实现方法public List<Book> getBookList(); 获取所有的图书