【正文】
m product where id id stmt DBgetStatement conn 使用 Statement 對(duì)象接收 sql 語(yǔ)句 DBexecuteUpdate stmt sql 執(zhí)行 sql 語(yǔ)句 finally DBclose stmt DBclose conn 關(guān)閉連接 getProducts List products int pageNo int pageSize boolean lazy 獲取商品列表分頁(yè)顯示方法此方法有 4個(gè)參數(shù)第一個(gè)參數(shù)是 List 集合第二個(gè)參數(shù)是第幾頁(yè)第三個(gè)參數(shù)是每頁(yè)顯示多少條第四個(gè)參數(shù)是是否初始化對(duì)象模擬hiber public List getProducts Connection conn DBgetConn 獲得連接 Statement stmt DBgetStatement conn String sql select from product order by pdate desc ResultSet rs DBgetResultSet stmt sql 使用 ResultSet 對(duì)象接收返回結(jié)果集 List products new ArrayList try while rsnext 遍歷獲得的結(jié)果集 Product p getProductFromRs rs productsadd p 添加到一個(gè) List 集合中 catch SQLException e eprintStackTrace finally DBclose stmt DBclose rs DBclose conn return products 返回 List 集合 實(shí)現(xiàn)效果如下 圖 47 產(chǎn)品列表 update 方法通過(guò)傳入 id 值來(lái)修改用戶的信息 SQL 語(yǔ)句是使用 update product set name descr normalprice memberprice categoryid where id Delete 0 方法和 loadById 方法類似 public void update Product p Connection conn null PreparedStatement pstmt null try conn DBgetConn 獲取數(shù)據(jù)庫(kù)連接 String sql update product set name descr normalprice memberprice categoryid where id pstmt DBprepare conn sql pstmtsetString 1 pgetName 設(shè)置的值 pstmtsetString 2 pgetDescr pstmtsetDouble 3 pgetNormalPrice pstmtsetDouble 4 pgetMemberPrice pstmtsetInt 5 pgetCategoryId pstmtsetInt 6 pgetId pstmtexecuteUpdate catch SQLException e eprintStackTrace finally DBclose pstmt DBclose conn find 訪法產(chǎn)品搜索產(chǎn)品的復(fù)雜搜索 產(chǎn)品的復(fù)雜搜索首先用到了一個(gè)輔助對(duì)象表單對(duì)象ProductSearchFormBeanjava 這個(gè)類中有以下這些屬性 1 private int categoryId 類型 ID 2 private String name 商品名 3 private double lowNormalPrice 最低市場(chǎng)價(jià)格 4 private double highNormalPrice 最高市場(chǎng)價(jià)格 5 private double lowMemberPrice 最低會(huì)員價(jià)格 6 private double highMemberPrice 最高會(huì)員價(jià)格 7 private String startDate 開始日期 8 private String endDate 結(jié)束日期 ComplexSerchResult 初始化 searchBean page language java import javautil contentType texthtmlcharset gbk pageEncoding GB18030 page import bjsxtshoppingproduct jspuseBean id searchBean class bjsxtshoppingproductProductSearchFormBean jspsetProperty name searchBean property jspuseBean Data Transfer Object 驗(yàn)證 jspgetProperty name searchBean property categoryId nbsp jspgetProperty name searchBean property name nbsp jspgetProperty name searchBean property lowNormalPrice nbsp jspgetProperty name searchBean property highNormalPrice nbsp jspgetProperty name searchBean property lowMemberPrice nbsp jspgetProperty name searchBean property highMemberPrice nbsp jspgetProperty name searchBean property startDate nbsp jspgetProperty name searchBean property endDate nbsp 接下來(lái)在 find 方法中拼湊了一段完整的 sql語(yǔ)句查詢方法可以通過(guò)類別產(chǎn)品價(jià)格區(qū)間產(chǎn)品 ID 產(chǎn)品名來(lái)匹配查找方法的代碼如下 public int find List products int pageNo int pageSize ProductSearchFormBean bean 拿到 searchBean String queryStr where 1 1 where 1 1 為了 and 添加方便 if beangetCategoryId 1 queryStr and pcategoryid beangetCategoryId if beangetName null beangetName trim equals queryStr and pname like beangetName if beangetLowNormalPrice 00 queryStr and pnormalprice beangetLowNormalPrice if beangetHighNormalPrice 00 queryStr and pnormalprice beangetHighNormalPrice if beangetLowMemberPrice 00 queryStr and pmemberprice beangetLowMemberPrice if beangetHighMemberPrice 00 queryStr and pmemberprice beangetHighMemberPrice if beangetStartDate null beangetStartDate trim equals queryStr and ppdate beangetStartDate 000000 if beangetEndDate null beangetEndDate trim equals queryStr and ppdate beangetEndDate 000000 最后合成一條完整的 sql 語(yǔ)句 queryStr 傳送給 dao 層 return daofind products pageNo pageSize queryStr 實(shí)現(xiàn)效果如下 圖 48 商品檢索 434 產(chǎn)品模塊前端 Indexjsp中的生成動(dòng)態(tài)產(chǎn)品類別效果首先要取得所有的 Category然后從其中取出級(jí)別最高的 Category 也就是第一級(jí)的種類目錄 private List getTopCategories List categories List topCategories new ArrayList for int i 0 i categoriessize i Category c categoriesget i if cgetGrade 1 topCategoriesadd c return topCategories private List getChilds Category parent List categories List childs new ArrayList for int i 0 i categoriessize i Category c categoriesget i if cgetPid parentgetId childsadd c return childs private String getChildsStr List childs StringBuffer buf new StringBuffer for int i 0 i childssize i Category c childsget i bufappend a href ShowProductsjspcategoryId cgetId append cgetName append append return buftoString 拿所有的種類 List categories CategoryServicegetInstance getCategories 拿到最頂層目錄第一級(jí)的目錄 List topCategories getTopCategories categories 然后通過(guò)以下代碼并排顯示到頁(yè)面中 for int i 0 i topCategoriessize i Category c topCategoriesget i a href onMouseOver on_trview cgetId in img src images00gif name Image0 border 0 span style colorFFFFFF cgetName 其中用到的 on_trview 為 function on_trview trnameenforce for int i 0 i topCategoriessize i Category c topCategoriesget i documentallpma_ cgetId styledisplay none 而其中的 pma_ cgetId 來(lái)源與 for int