一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|

服務(wù)器之家 - 編程語言 - JAVA教程 - javaweb圖書商城設(shè)計(jì)之圖書模塊(4)

javaweb圖書商城設(shè)計(jì)之圖書模塊(4)

2020-07-04 10:41Android-Dev JAVA教程

這篇文章主要介紹了javaweb圖書商城設(shè)計(jì)之圖書模塊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文接著上一篇圖書商城分類模塊進(jìn)行學(xué)習(xí),供大家參考,具體內(nèi)容如下

1、創(chuàng)建相關(guān)類

cn.itcast.bookstore.book
domain:Book
dao:BookDao
service :BookService
web.servle:BookServlet

Book

?
1
2
3
4
5
6
7
8
9
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

?
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
105
106
107
108
109
public class BookDao {
 private QueryRunner qr = new TxQueryRunner();
 
 /**
 * 查詢所有圖書
 * @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);
 }
 }
 
 /**
 * 按分類查詢
 * @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對象中保存Category的信息
 */
 String sql = "select * from book where bid=?";
 Map<String,Object> map = qr.query(sql, new MapHandler(), bid);
 /*
 * 使用一個(gè)Map,映射出兩個(gè)對象,再給這兩個(gè)對象建立關(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);
 }
 }
 
 /**
 * 查詢指定分類下的圖書本數(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);
 }
 }
 
 /**
 * 添加圖書
 * @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);
 }
 }
 
 /**
 * 刪除圖書
 * @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

 

?
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
public class BookService {
 private BookDao bookDao = new BookDao();
 
 /**
 * 查詢所有圖書
 * @return
 */
 public List<Book> findAll() {
 return bookDao.findAll();
 }
 
 /**
 * 按分類查詢圖書
 * @param cid
 * @return
 */
 public List<Book> findByCategory(String cid) {
 return bookDao.findByCategory(cid);
 }
 
 public Book load(String bid) {
 return bookDao.findByBid(bid);
 }
 
 /**
 *  添加圖書
 * @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

 

?
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
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";
 }
 
 /**
 * 查詢所有圖書
 * @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";
 }
 
 /**
 * 按分類查詢
 * @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、查詢所有圖書

流程:left.jsp(全部分類) -> BookServlet#findAll() -> /jsps/book/list.jsp

javaweb圖書商城設(shè)計(jì)之圖書模塊(4)

3、按分類查詢圖書

流程:left.jsp -> BookServlet#findByCategory() -> list.jsp

javaweb圖書商城設(shè)計(jì)之圖書模塊(4)

4、查詢詳細(xì)信息(加載)

流程:list.jsp(點(diǎn)擊某一本書) -> BookServlet#load() -> desc.jsp

javaweb圖書商城設(shè)計(jì)之圖書模塊(4)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本大片在线 | 国产精品久久久久久影视 | 暖暖免费高清完整版观看日本 | 久久AV国产麻豆HD真实 | 精品久久成人免费第三区 | 我与肥熟老妇的性事 | 国内自拍视频在线观看 | 视频一区二区在线 | 国产精品视频第一页 | 久久久WWW免费人成精品 | 欧美一级专区免费大片俄罗斯 | 国产精品视频自拍 | 国内久久久 | 99视频网址 | 91久久偷偷做嫩草影院免费 | 成人精品mv视频在线观看 | 色老头综合网 | 四虎在线免费播放 | 国内视频一区二区 | 果冻传媒在线视频播放观看 | 高清视频在线播放ww | 不知火舞被c视频在线播放 不卡一区二区三区卡 | 国产盗摄wc女厕所 | 国产精品第页 | 百合互慰吃奶互揉漫画 | 91av爱爱 | 99re这里只有精品在线观看 | 茄子香蕉视频 | 日韩欧美高清一区 | 国产在线观看精品香蕉v区 国产在线观看a | 四虎成人免费视频 | 日韩一区二区在线视频 | 欧美日韩国产一区二区三区欧 | 5x社区发源地最新地址 | 国产色视频一区二区三区 | 国产午夜精品久久理论片小说 | 四虎精品免费视频 | 欧美性f | 18性夜影院午夜寂寞影院免费 | 亚洲精品久久久久69影院 | 日韩v|