您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何用Java代碼實現網上商城系統,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
功能: 主頁顯示熱銷商品;所有商品展示,可進行商品搜索;點 擊商品進入商品詳情頁,顯示庫存,具有立即購買和加入 購物車功能,可增減購買商品數量亦可手動輸入(同時驗證 庫存),熱銷商品展示。立即購買進入確認訂單頁面,可選 擇已經添加的地址,亦可新增地址。(同時驗證庫存),可 選擇購買哪些商品,可刪除不需要的商品。點擊結算進入 確認訂單頁面,確認后提交訂單,訂單重復提交給予響 應,庫存不足或商品下架給予響應。后臺管理:(修改密碼 等),商品管理(商品批量添加、上下架、庫存維護等), 訂單管理。
環境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
項目技術: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。
/** * 后臺管理-主頁 */ @Controller public class AdminHomeController extends BaseController { @Resource(name = "adminService") private AdminService adminService; @Resource(name = "productOrderService") private ProductOrderService productOrderService; @Resource(name = "productService") private ProductService productService; @Resource(name = "userService") private UserService userService; /** * 轉到后臺管理-主頁 * @param session session對象 * @param map 前臺傳入的Map * @return 響應數據 * @throws ParseException 轉換異常 */ @RequestMapping(value = "admin", method = RequestMethod.GET) public String goToPage(HttpSession session, Map<String, Object> map) throws ParseException { logger.info("獲取管理員信息"); Object adminId = checkAdmin(session); if (adminId == null) { return "redirect:/admin/login"; } Admin admin = adminService.get(null, Integer.parseInt(adminId.toString())); map.put("admin", admin); logger.info("獲取統計信息"); //產品總數 Integer productTotal = productService.getTotal(null, new Byte[]{0, 2}); //用戶總數 Integer userTotal = userService.getTotal(null); //訂單總數 Integer orderTotal = productOrderService.getTotal(null, new Byte[]{3}); logger.info("獲取圖表信息"); map.put("jsonObject", getChartData(null,null,7)); map.put("productTotal", productTotal); map.put("userTotal", userTotal); map.put("orderTotal", orderTotal); logger.info("轉到后臺管理-主頁"); return "admin/homePage"; } /** * 轉到后臺管理-主頁(ajax方式) * @param session session對象 * @param map 前臺傳入的Map * @return 響應數據 * @throws ParseException 轉換異常 */ @RequestMapping(value = "admin/home", method = RequestMethod.GET) public String goToPageByAjax(HttpSession session, Map<String, Object> map) throws ParseException { logger.info("獲取管理員信息"); Object adminId = checkAdmin(session); if (adminId == null) { return "admin/include/loginMessage"; } Admin admin = adminService.get(null, Integer.parseInt(adminId.toString())); map.put("admin", admin); logger.info("獲取統計信息"); Integer productTotal = productService.getTotal(null, new Byte[]{0, 2}); Integer userTotal = userService.getTotal(null); Integer orderTotal = productOrderService.getTotal(null, new Byte[]{3}); logger.info("獲取圖表信息"); map.put("jsonObject", getChartData(null, null,7)); logger.info("獲取圖表信息"); map.put("jsonObject", getChartData(null,null,7)); map.put("productTotal", productTotal); map.put("userTotal", userTotal); map.put("orderTotal", orderTotal); logger.info("轉到后臺管理-主頁-ajax方式"); return "admin/homeManagePage"; } /** * 按日期查詢圖表數據(ajax方式) * @param beginDate 開始日期 * @param endDate 結束日期 * @return 響應數據 * @throws ParseException 轉換異常 */ @ResponseBody @RequestMapping(value = "admin/home/charts", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public String getChartDataByDate(@RequestParam(required = false) String beginDate, @RequestParam(required = false) String endDate) throws ParseException { if (beginDate != null && endDate != null) { //轉換日期格式 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); return getChartData(simpleDateFormat.parse(beginDate), simpleDateFormat.parse(endDate),7).toJSONString(); } else { return getChartData(null, null,7).toJSONString(); } } /** * 按日期獲取圖表數據 * @param beginDate 開始日期 * @param endDate 結束日期 * @param days 天數 * @return 圖表數據的JSON對象 * @throws ParseException 轉換異常 */ private JSONObject getChartData(Date beginDate,Date endDate,int days) throws ParseException { JSONObject jsonObject = new JSONObject(); SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd", Locale.UK); SimpleDateFormat time2 = new SimpleDateFormat("MM/dd", Locale.UK); SimpleDateFormat timeSpecial = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK); //如果沒有指定開始和結束日期 if (beginDate == null || endDate == null) { //指定一周前的日期為開始日期 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1-days); beginDate = time.parse(time.format(cal.getTime())); //指定當前日期為結束日期 cal = Calendar.getInstance(); endDate = cal.getTime(); } else { beginDate = time.parse(time.format(beginDate)); endDate = timeSpecial.parse(time.format(endDate) + " 23:59:59"); } logger.info("根據訂單狀態分類"); //未付款訂單數統計數組 int[] orderUnpaidArray = new int[7]; //未發貨訂單數統計叔祖 int[] orderNotShippedArray = new int[7]; //未確認訂單數統計數組 int[] orderUnconfirmedArray = new int[7]; //交易成功訂單數統計數組 int[] orderSuccessArray = new int[7]; //總交易訂單數統計數組 int[] orderTotalArray = new int[7]; logger.info("從數據庫中獲取統計的訂單集合數據"); List<OrderGroup> orderGroupList = productOrderService.getTotalByDate(beginDate, endDate); //初始化日期數組 JSONArray dateStr = new JSONArray(days); //按指定的天數進行循環 for (int i = 0; i < days; i++) { //格式化日期串(MM/dd)并放入日期數組中 Calendar cal = Calendar.getInstance(); cal.setTime(beginDate); cal.add(Calendar.DATE, i); String formatDate = time2.format(cal.getTime()); dateStr.add(formatDate); //該天的訂單總數 int orderCount = 0; //循環訂單集合數據的結果集 for(int j = 0; j < orderGroupList.size(); j++){ OrderGroup orderGroup = orderGroupList.get(j); //如果該訂單日期與當前日期一致 if(orderGroup.getProductOrder_pay_date().equals(formatDate)){ //從結果集中移除數據 orderGroupList.remove(j); //根據訂單狀態將統計結果存入對應的訂單狀態數組中 switch (orderGroup.getProductOrder_status()) { case 0: //未付款訂單 orderUnpaidArray[i] = orderGroup.getProductOrder_count(); break; case 1: //未發貨訂單 orderNotShippedArray[i] = orderGroup.getProductOrder_count(); break; case 2: //未確認訂單 orderUnconfirmedArray[i] = orderGroup.getProductOrder_count(); break; case 3: //交易成功訂單 orderSuccessArray[i] = orderGroup.getProductOrder_count(); break; } //累加當前日期的訂單總數 orderCount += orderGroup.getProductOrder_count(); } } //將統計的訂單總數存入總交易訂單數統計數組 orderTotalArray[i] = orderCount; } logger.info("返回結果集map"); jsonObject.put("orderTotalArray", orderTotalArray); jsonObject.put("orderUnpaidArray", orderUnpaidArray); jsonObject.put("orderNotShippedArray", orderNotShippedArray); jsonObject.put("orderUnconfirmedArray", orderUnconfirmedArray); jsonObject.put("orderSuccessArray", orderSuccessArray); jsonObject.put("dateStr",dateStr); return jsonObject; } }
/** * 主頁 */ @Controller public class ForeHomeController extends BaseController { @Resource(name = "userService") private UserService userService; @Resource(name="categoryService") private CategoryService categoryService; @Resource(name="productService") private ProductService productService; @Resource(name="productImageService") private ProductImageService productImageService; //轉到前臺天貓-主頁 @RequestMapping(value = "/", method = RequestMethod.GET) public String goToPage(HttpSession session, Map<String, Object> map) { logger.info("檢查用戶是否登錄"); Object userId = checkUser(session); if (userId != null) { logger.info("獲取用戶信息"); User user = userService.get(Integer.parseInt(userId.toString())); map.put("user", user); } logger.info("獲取產品分類列表"); List<Category> categoryList = categoryService.getList(null,null); logger.info("獲取每個分類下的產品列表"); for(Category category : categoryList){ List<Product> productList = productService.getList( new Product().setProduct_category(category), new Byte[]{0, 2}, new OrderUtil("product_sale_count", true), new PageUtil(0, 8) ); if (productList != null) { for (Product product : productList) { Integer product_id = product.getProduct_id(); product.setSingleProductImageList( productImageService.getList( product_id, (byte) 0, new PageUtil(0, 1) ) ); } } category.setProductList(productList); } map.put("categoryList",categoryList); logger.info("獲取促銷產品列表"); List<Product> specialProductList = productService.getList( null, new Byte[]{2}, null, new PageUtil(0, 6) ); map.put("specialProductList", specialProductList); logger.info("轉到前臺主頁"); return "fore/homePage"; } //轉到前臺天貓-錯誤頁 @RequestMapping(value = "error", method = RequestMethod.GET) public String goToErrorPage() { return "fore/errorPage"; } //獲取主頁分類下產品信息-ajax @ResponseBody @RequestMapping(value = "product/nav/{category_id}", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public String getProductByNav(@PathVariable("category_id") Integer category_id) { JSONObject object = new JSONObject(); if (category_id == null) { object.put("success", false); return object.toJSONString(); } logger.info("獲取分類ID為{}的產品標題數據", category_id); List<Product> productList = productService.getTitle( new Product().setProduct_category(new Category().setCategory_id(category_id)), new PageUtil(0, 40) ); List<List<Product>> complexProductList = new ArrayList<>(8); List<Product> products = new ArrayList<>(5); for (int i = 0; i < productList.size(); i++) { //如果臨時集合中產品數達到5個,加入到產品二維集合中,并重新實例化臨時集合 if (i % 5 == 0) { complexProductList.add(products); products = new ArrayList<>(5); } products.add(productList.get(i)); } complexProductList.add(products); Category category = new Category().setCategory_id(category_id).setComplexProductList(complexProductList); object.put("success", true); object.put("category", category); return object.toJSONString(); } }
關于如何用Java代碼實現網上商城系統就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。