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

服務(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ù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一级特黄aa大片免费 | 厕所rxxx| 四虎在线网址 | 亚洲黄色成人 | 阿 好深 快点 老师受不了 | 午夜理论片YY4399影院 | 精品国产自在现线拍400部 | 2012年中文字幕在线看 | 色噜噜国产精品视频一区二区 | 国产一卡二卡3卡4卡四卡在线视频 | 激情亚洲天堂 | 日韩亚洲人成在线 | 国产一卡二卡四卡免费 | 亚洲成熟人网站 | 亚洲zooz人禽交xxxx | 午夜在线播放免费人成无 | 久久综合狠狠综合久久综合88 | 久久亚洲精品中文字幕60分钟 | 欧美高清在线不卡免费观看 | 好男人在线观看hd中字 | 国产欧美日韩精品高清二区综合区 | 亚洲国产精品自在在线观看 | 免费一区在线 | 媳妇和公公小说 | 操比网| 国产成人精品曰本亚洲78 | 韩国美女被的免费视频 | 国产成人精品高清在线 | 好大好硬好深好爽想要之黄蓉 | 俄罗斯毛片免费大全 | 激情综合站 | 美女扒开腿让男生捅 | 国产麻豆精品入口在线观看 | 美女被草出水 | 国产综合图区 | 日韩网新片免费 | 国产精品视频一区二区三区w | 成年人在线观看视频免费 | 男人捅女人动漫 | 91精品91久久久久久 | 都市风流贵妇激情 |