javaweb图书商城设计之图书模块(4)

时间:2021-07-16 10:25:27

本文接着上一篇图书商城分类模块进行学习,供大家参考,具体内容如下

1、创建相关类

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);
 /*
 * 使用一个Map,映射出两个对象,再给这两个对象建立关系!
 */
 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);
 }
 }
 
 /**
 * 查询指定分类下的图书本数
 * @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. 得到参数bid
 * 2. 查询得到Book
 * 3. 保存,转发到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图书商城设计之图书模块(4)

3、按分类查询图书

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

javaweb图书商城设计之图书模块(4)

4、查询详细信息(加载)

流程:list.jsp(点击某一本书) -> BookServlet#load() -> desc.jsp

javaweb图书商城设计之图书模块(4)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。