javaweb圖書(shū)商城設(shè)計(jì)之圖書(shū)模塊(4)
本文接著上一篇圖書(shū)商城分類(lèi)模塊進(jìn)行學(xué)習(xí),供大家參考,具體內(nèi)容如下
1、創(chuàng)建相關(guān)類(lèi)
cn.itcast.bookstore.book
domain:Book
dao:BookDao
service :BookService
web.servle:BookServlet
Book
public class Book {
private String bid;
private String bname;
private double price;
private String author;
private String image;
private Category category;
private boolean del;
}
BookDao
public class BookDao {
private QueryRunner qr = new TxQueryRunner();
/**
* 查詢所有圖書(shū)
* @return
*/
public List<Book> findAll() {
try {
String sql = "select * from book where del=false";
return qr.query(sql, new BeanListHandler<Book>(Book.class));
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 按分類(lèi)查詢
* @param cid
* @return
*/
public List<Book> findByCategory(String cid) {
try {
String sql = "select * from book where cid=? and del=false";
return qr.query(sql, new BeanListHandler<Book>(Book.class), cid);
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 加載方法
* @param bid
* @return
*/
public Book findByBid(String bid) {
try {
/*
* 我們需要在Book對(duì)象中保存Category的信息
*/
String sql = "select * from book where bid=?";
Map<String,Object> map = qr.query(sql, new MapHandler(), bid);
/*
* 使用一個(gè)Map,映射出兩個(gè)對(duì)象,再給這兩個(gè)對(duì)象建立關(guān)系!
*/
Category category = CommonUtils.toBean(map, Category.class);
Book book = CommonUtils.toBean(map, Book.class);
book.setCategory(category);
return book;
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 查詢指定分類(lèi)下的圖書(shū)本數(shù)
* @param cid
* @return
*/
public int getCountByCid(String cid) {
try {
String sql = "select count(*) from book where cid=?";
Number cnt = (Number)qr.query(sql, new ScalarHandler(), cid);
return cnt.intValue();
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 添加圖書(shū)
* @param book
*/
public void add(Book book) {
try {
String sql = "insert into book values(?,?,?,?,?,?)";
Object[] params = {book.getBid(), book.getBname(), book.getPrice(),
book.getAuthor(), book.getImage(), book.getCategory().getCid()};
qr.update(sql, params);
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 刪除圖書(shū)
* @param bid
*/
public void delete(String bid) {
try {
String sql = "update book set del=true where bid=?";
qr.update(sql, bid);
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
public void edit(Book book) {
try {
String sql = "update book set bname=?, price=?,author=?, image=?, cid=? where bid=?";
Object[] params = {book.getBname(), book.getPrice(),
book.getAuthor(), book.getImage(),
book.getCategory().getCid(), book.getBid()};
qr.update(sql, params);
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
}
BookService
public class BookService {
private BookDao bookDao = new BookDao();
/**
* 查詢所有圖書(shū)
* @return
*/
public List<Book> findAll() {
return bookDao.findAll();
}
/**
* 按分類(lèi)查詢圖書(shū)
* @param cid
* @return
*/
public List<Book> findByCategory(String cid) {
return bookDao.findByCategory(cid);
}
public Book load(String bid) {
return bookDao.findByBid(bid);
}
/**
* 添加圖書(shū)
* @param book
*/
public void add(Book book) {
bookDao.add(book);
}
public void delete(String bid) {
bookDao.delete(bid);
}
public void edit(Book book) {
bookDao.edit(book);
}
}
BookServlet
public class BookServlet extends BaseServlet {
private BookService bookService = new BookService();
public String load(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1. 得到參數(shù)bid
* 2. 查詢得到Book
* 3. 保存,轉(zhuǎn)發(fā)到desc.jsp
*/
request.setAttribute("book", bookService.load(request.getParameter("bid")));
return "f:/jsps/book/desc.jsp";
}
/**
* 查詢所有圖書(shū)
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String findAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("bookList", bookService.findAll());
return "f:/jsps/book/list.jsp";
}
/**
* 按分類(lèi)查詢
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String findByCategory(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cid = request.getParameter("cid");
request.setAttribute("bookList", bookService.findByCategory(cid));
return "f:/jsps/book/list.jsp";
}
}
2、查詢所有圖書(shū)
流程:left.jsp(全部分類(lèi)) -> BookServlet#findAll() -> /jsps/book/list.jsp

3、按分類(lèi)查詢圖書(shū)
流程:left.jsp -> BookServlet#findByCategory() -> list.jsp

4、查詢?cè)敿?xì)信息(加載)
流程:list.jsp(點(diǎn)擊某一本書(shū)) -> BookServlet#load() -> desc.jsp

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖書(shū)管理系統(tǒng)java版
- 一個(gè)簡(jiǎn)陋的java圖書(shū)管理系統(tǒng)
- JavaWeb搭建網(wǎng)上圖書(shū)商城畢業(yè)設(shè)計(jì)
- javaweb圖書(shū)商城設(shè)計(jì)之訂單模塊(5)
- javaweb圖書(shū)商城設(shè)計(jì)之購(gòu)物車(chē)模塊(3)
- Java設(shè)計(jì)模塊系列之書(shū)店管理系統(tǒng)單機(jī)版(一)
- java網(wǎng)上圖書(shū)商城(4)購(gòu)物車(chē)模塊1
- Java設(shè)計(jì)模塊系列之書(shū)店管理系統(tǒng)單機(jī)版(二)
- javaweb圖書(shū)商城設(shè)計(jì)之用戶模塊(1)
- 利用Java異常機(jī)制實(shí)現(xiàn)模擬借書(shū)系統(tǒng)
相關(guān)文章
IDEA2019.3配置Hibernate的詳細(xì)教程(未使用IDEA的自動(dòng)化)
這篇文章主要介紹了IDEA2019.3配置Hibernate的詳細(xì)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
springSecurity實(shí)現(xiàn)簡(jiǎn)單的登錄功能
這篇文章主要為大家詳細(xì)介紹了springSecurity實(shí)現(xiàn)簡(jiǎn)單的登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
springboot lua檢查redis庫(kù)存的實(shí)現(xiàn)示例
本文主要介紹了springboot lua檢查redis庫(kù)存的實(shí)現(xiàn)示例,為了優(yōu)化性能,通過(guò)Lua腳本實(shí)現(xiàn)對(duì)多個(gè)馬戲場(chǎng)次下的座位等席的庫(kù)存余量檢查,感興趣的可以了解一下2024-09-09
Spring Cloud Ribbon客戶端詳細(xì)介紹
Spring Cloud Ribbon 是一套基于 Netflix Ribbon 實(shí)現(xiàn)的客戶端負(fù)載均衡和服務(wù)調(diào)用工具。通過(guò)Spring Cloud的封裝,可以讓我們輕松地將面向服務(wù)的REST模版請(qǐng)求自動(dòng)轉(zhuǎn)換成客戶端負(fù)載均衡的服務(wù)調(diào)用2022-09-09
java簡(jiǎn)單實(shí)現(xiàn)多線程及線程池實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)單實(shí)現(xiàn)多線程,及java爬蟲(chóng)使用線程池實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
詳解Java如何通過(guò)Socket實(shí)現(xiàn)查詢IP
在本文中,我們來(lái)學(xué)習(xí)下如何找到連接到服務(wù)器的客戶端計(jì)算機(jī)的IP地址。我們將創(chuàng)建一個(gè)簡(jiǎn)單的客戶端-服務(wù)器場(chǎng)景,讓我們探索用于TCP/IP通信的java.net?API,感興趣的可以了解一下2022-10-10

