1. 程式人生 > >JavaWeb專案練習--分類模組、圖書模組

JavaWeb專案練習--分類模組、圖書模組

分類模組

1 分類模組的相關類建立	
	ywnxbx.bookstore.category
			domain:Category
			dao:CategoryDao
			service:CategoryService
			web.servlet:CategoryServlet

2 查詢所有分類
	流程:main.jsp(< iframe >) --> CategoryService#findAll() --> left.jsp

domain:

public class Category {
	private String cid;
	private String cname;
}

dao:

public class CategoryDao {
	private QueryRunner qr = new TxQueryRunner();
	
	/**
	 * 查詢所有分類
	 * @return
	 */
	public List<Category> findAllCategory() {
		String sql = "select * from category";
		try {
			return qr.query(sql, new BeanListHandler<Category>(Category.class));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 新增分類
	 * @param category
	 */
	public void addCategory(Category category) {
		String sql = "insert into category values(?,?)";
		try {
			qr.update(sql,category.getCid(),category.getCname());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 刪除分類
	 * @param cid
	 */
	public void deleteCategory(String cid) {
		String sql = "delete from category where cid = ?";
		try {
			qr.update(sql,cid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 載入分類
	 * @param cid
	 * @return
	 */
	public Category load(String cid) {
		String sql = "select * from category where cid = ?";
		try {
			return qr.query(sql, new BeanHandler<Category>(Category.class),cid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 修改分類名稱
	 * @param category
	 */
	public void edit(Category category) {
		String sql = "update category set cname = ? where cid = ?";
		try {
			qr.update(sql,category.getCname(),category.getCid());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}

service:

public class CategoryService {
	private CategoryDao categoryDao = new CategoryDao();
	private BookDao bookDao = new BookDao();
	/**
	 * 查詢所有分類
	 * @return
	 */
	public List<Category> findAllCategory() {
		return categoryDao.findAllCategory();
	}
	
	/**
	 * 新增分類
	 * @param category
	 */
	public void addCategory(Category category) {
		categoryDao.addCategory(category);
	}
	
	/**
	 * 刪除分類
	 * @param string
	 * @throws CategoryException 
	 */
	public void deleteCategory(String cid) throws CategoryException {
		/*
		 * 檢查此分類下是否含有圖書
		 * 		若有:不能刪除此分類,丟擲異常
		 * 呼叫方法刪除
		 */
		int count = bookDao.getCountByCid(cid);
		if(count > 0) throw new CategoryException("該分類下還有圖書,不能刪除");
		categoryDao.deleteCategory(cid);
 	}
	
	/**
	 * 載入分類
	 * @param cid
	 * @return
	 */
	public Category load(String cid) {
		return categoryDao.load(cid);
	}

	/**
	 * 修改分類
	 * @param category
	 */
	public void edit(Category category) {
		categoryDao.edit(category);
	}
}

web.servlet

public class CategoryServlet extends BaseServlet {
	private static final long serialVersionUID = 3352017120523237513L;
	
	private CategoryService categoryService = new CategoryService();
	
	/**
	 * 查詢所有分類
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//向request域中儲存CategoryList
		request.setAttribute("categoryList", categoryService.findAllCategory());
		return "f:/jsps/left.jsp";
	}
}

圖書模組

1 建立相關類

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

2 查詢所有圖書
	流程:left.jsp(全部分類)  BookServlet#findAll()  /jsps/book/list.jsp

在這裡插入圖片描述

3 按分類查詢圖書
	流程:left.jsp  BookServlet#findByCategory()  list.jsp

在這裡插入圖片描述

4 查詢詳細資訊(載入)
	流程:list.jsp(點選某一本書)  BookServlet#load()  desc.jsp

在這裡插入圖片描述

domain

public class Book {
	private String bid;
	private String bname;
	private double price;
	private String author;
	private String image;
	private Category category;  //多方(書)關聯一方(分類編號)
	private boolean del;
}

dao


public class BookDao {
	private QueryRunner qr = new TxQueryRunner();
	
	/**
	 * 查詢所有圖書
	 * @return
	 */
	public List<Book> findAllBook(){
		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 {	
			String sql = "select * from book where bid = ?";
			
			/*
			 * 資料庫表中cid無法與Book中Category完成對映,導致不能查詢到cid
			 * 
			 * 解決:
			 * 1.使用map做結果集  目的:將所有查詢結果用鍵值對的方式儲存
			 * 2.分別用map映射出Category和Book物件
			 * 3.將Category設定進Book中
			 */
			Map<String,Object> map = qr.query(sql,new MapHandler(),bid);
			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 count = (Number) qr.query(sql,new ScalarHandler(),cid);
			return count.intValue();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} 
	}
	
	/**
	 * 新增圖書
	 * @param book
	 */
	
	public void add(Book book) {
		try {
			String sql = "insert into book values(?,?,?,?,?,?,false)";
			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);
		}
	}
	
	/**
	 * 編輯圖書
	 * @param book
	 */
	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);
		}
	}
}

service

public class BookService {
	private BookDao bookDao = new BookDao();
	
	/**
	 * 查詢所有圖書
	 * @return
	 */
	public List<Book> findAllBook(){
		return bookDao.findAllBook();
	}
	
	/**
	 * 按類別查詢圖書
	 * @param cid
	 * @return
	 */
	public List<Book> findByCategory(String cid) {
		return bookDao.findByCategory(cid);
	}

	/**
	 * 載入指定圖書
	 * @param bid
	 * @return
	 */
	public Book load(String bid) {
		return bookDao.findByBid(bid);
	}
	
	/**
	 * 新增圖書
	 * @param book
	 */
	public void add(Book book) {
		bookDao.add(book);
	}
	
	/**
	 * 刪除圖書
	 * @param bid
	 */
	public void delete(String bid){
		bookDao.delete(bid);
	}
	
	/**
	 * 編輯圖書
	 * @param book
	 */
	public void edit(Book book) {
		bookDao.edit(book);
	}
}

servlet

public class BookServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	private BookService bookService = new BookService();
	/**
	 * 查詢所有圖書
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//查詢所有圖書,將查詢結果儲存到request中
		request.setAttribute("bookList", bookService.findAllBook());
		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中
		request.setAttribute("bookList", bookService.findByCategory(cid));
		return "f:/jsps/book/list.jsp";
	}
	
	/**
	 * 載入指定圖書
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String load(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到圖書bid
		String bid = request.getParameter("bid");
		//通過bid查詢,將結果儲存到request中
		request.setAttribute("book", bookService.load(bid));
		return "f:/jsps/book/desc.jsp";	
	}
}

jsps:

1)desc.jsp   顯示圖書資訊 

	<style type="text/css">
		body {
			font-size: 10pt;
		}
		div {
			margin:20px;
			border: solid 2px gray;
			width: 150px;
			height: 150px;
			text-align: center;
		}
		li {
			margin: 10px;
		}
		a {
			background: url(<c:url value='/images/all.png'/>) no-repeat;
			display: inline-block;
			
			background-position: 0 -70px;
			margin-left: 30px;
			height: 36px;
			width: 146px;
		}
		a:HOVER {
			background: url(<c:url value='/images/all.png'/>) no-repeat;
			display: inline-block;
			
			background-position: 0 -106px;
			margin-left: 30px;
			height: 36px;
			width: 146px;
		}
	</style>
	  </head>
	  
	  <body>
	  
	  <div>
	    <img src="<c:url value='/${book.image }'/>" border="0"/>
	  </div>
	  
	  <ul>
	    <li>書名:${book.bname }</li>
	    <li>作者:${book.author }</li>
	    <li>單價:${book.price }</li>
	  </ul>
	  
	
	  <form id="form" action="<c:url value='/CartServlet'/>" method="post">
		  	<%--指定要呼叫的方法 --%>
		  	<input type="hidden" name="method" value="add"/>
		  	<input type="hidden" name="bid" value="${book.bid }"/>
		  	<input type="text" size="3" name="count" value="1"/>
		  </form>
		  <a href="javascript:document.getElementById('form').submit();"></a>
	  
	  </body>

2)list.jsp  遍歷顯示所有圖書

	<style type="text/css">
		body {
			font-size: 10pt;
		}
		.icon {
			margin:10px;
			border: solid 2px gray;
			width: 160px;
			height: 180px;
			text-align: center;
			float: left;
		}
	</style>
	  </head>
	  
	  <body>
	
	<c:forEach items="${bookList }" var="book">
	 
	 <div class="icon">
	    <a href="<c:url value='/BookServlet?method=load&bid=${book.bid }'/>">
	    <img src="<c:url value='/${book.image }'/>" border="0"/></a> <br/>
	   	
	   	<a href="<c:url value='/BookServlet?method=load&bid=${book.bid }'/>">${book.bname }</a>
	  </div>
	
	</c:forEach>
	
	  </body>