Java 仿天猫服装商城系统的实现流程
作者:qq_1334611189
读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+SSM+jsp+mysql+maven实现一个仿天猫服装商城系统,大家可以在过程中查缺补漏,提升水平
一、项目简述
功能:网上商城系统,前台+后台管理,用户注册,登录, 上哦展示,分组展示,搜索,收货地址管理,购物车管 理,添加,购买,个人信息修改。订单查询等等,后台商 品管理,分类管理,库存管理,订单管理,用户管理,信 息、修改等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。
权限控制代码:
/** * 产品详情页 */ @Controller public class ForeProductDetailsController extends BaseController { @Resource(name = "productService") private ProductService productService; @Resource(name = "userService") private UserService userService; @Resource(name = "productImageService") private ProductImageService productImageService; @Resource(name = "categoryService") private CategoryService categoryService; @Resource(name = "propertyValueService") private PropertyValueService propertyValueService; @Resource(name = "propertyService") private PropertyService propertyService; @Resource(name = "reviewService") private ReviewService reviewService; @Resource(name = "productOrderItemService") private ProductOrderItemService productOrderItemService; //转到前台天猫-产品详情页 @RequestMapping(value = "product/{pid}", method = RequestMethod.GET) public String goToPage(HttpSession session, Map<String, Object> map, @PathVariable("pid") String pid /*产品ID*/) { 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("获取产品ID"); Integer product_id = Integer.parseInt(pid); logger.info("获取产品信息"); Product product = productService.get(product_id); if (product == null || product.getProduct_isEnabled() == 1) { return "redirect:/404"; } logger.info("获取产品子信息-分类信息"); product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id())); logger.info("获取产品子信息-预览图片信息"); List<ProductImage> singleProductImageList = productImageService.getList(product_id, (byte) 0, null); product.setSingleProductImageList(singleProductImageList); logger.info("获取产品子信息-详情图片信息"); List<ProductImage> detailsProductImageList = productImageService.getList(product_id, (byte) 1, null); product.setDetailProductImageList(detailsProductImageList); logger.info("获取产品子信息-产品属性值信息"); List<PropertyValue> propertyValueList = propertyValueService.getList(new PropertyValue().setPropertyValue_product(product), null); logger.info("获取产品子信息-分类信息对应的属性列表"); List<Property> propertyList = propertyService.getList(new Property().setProperty_category(product.getProduct_category()), null); logger.info("属性列表和属性值列表合并"); for (Property property : propertyList) { for (PropertyValue propertyValue : propertyValueList) { if (property.getProperty_id().equals(propertyValue.getPropertyValue_property().getProperty_id())) { List<PropertyValue> property_value_item = new ArrayList<>(1); property_value_item.add(propertyValue); property.setPropertyValueList(property_value_item); break; } } } logger.info("获取产品子信息-产品评论信息"); product.setReviewList(reviewService.getListByProductId(product_id, null)); if (product.getReviewList() != null) { for (Review review : product.getReviewList()) { review.setReview_user(userService.get(review.getReview_user().getUser_id())); } } logger.info("获取猜你喜欢列表"); Integer category_id = product.getProduct_category().getCategory_id(); Integer total = productService.getTotal(new Product().setProduct_category(new Category().setCategory_id(category_id)), new Byte[]{0, 2}); logger.info("分类ID为{}的产品总数为{}条", category_id, total); //生成随机数 int i = new Random().nextInt(total); if (i + 2 >= total) { i = total - 3; } if (i < 0) { i = 0; } List<Product> loveProductList = productService.getList(new Product().setProduct_category( new Category().setCategory_id(category_id)), new Byte[]{0, 2}, null, new PageUtil().setCount(3).setPageStart(i) ); if (loveProductList != null) { logger.info("获取产品列表的相应的一张预览图片"); for (Product loveProduct : loveProductList) { loveProduct.setSingleProductImageList(productImageService.getList(loveProduct.getProduct_id(), (byte) 0, new PageUtil(0, 1))); } } logger.info("获取分类列表"); List<Category> categoryList = categoryService.getList(null, new PageUtil(0, 3)); map.put("loveProductList", loveProductList); map.put("categoryList", categoryList); map.put("propertyList", propertyList); map.put("product", product); map.put("guessNumber", i); map.put("pageUtil", new PageUtil(0, 10).setTotal(product.getProduct_review_count())); logger.info("转到前台-产品详情页"); return "fore/productDetailsPage"; } //按产品ID加载产品评论列表-ajax @Deprecated @ResponseBody @RequestMapping(value = "review/{pid}", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public String loadProductReviewList(@PathVariable("pid") String pid/*产品ID*/, @RequestParam Integer index/* 页数 */, @RequestParam Integer count/* 行数 */) { logger.info("获取产品ID"); Integer product_id = Integer.parseInt(pid); logger.info("获取产品评论列表"); List<Review> reviewList = reviewService.getListByProductId(product_id, new PageUtil(index, count)); JSONObject jsonObject = new JSONObject(); jsonObject.put("reviewList", JSONArray.parseArray(JSON.toJSONString(reviewList))); return jsonObject.toJSONString(); } //按产品ID加载产品属性列表-ajax @Deprecated @ResponseBody @RequestMapping(value = "property/{pid}", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public String loadProductPropertyList(@PathVariable("pid") String pid/*产品ID*/) { logger.info("获取产品ID"); Integer product_id = Integer.parseInt(pid); logger.info("获取产品详情-属性值信息"); Product product = new Product(); product.setProduct_id(product_id); List<PropertyValue> propertyValueList = propertyValueService.getList(new PropertyValue().setPropertyValue_product(product), null); logger.info("获取产品详情-分类信息对应的属性列表"); List<Property> propertyList = propertyService.getList(new Property().setProperty_category(product.getProduct_category()), null); logger.info("属性列表和属性值列表合并"); for (Property property : propertyList) { for (PropertyValue propertyValue : propertyValueList) { if (property.getProperty_id().equals(propertyValue.getPropertyValue_property().getProperty_id())) { List<PropertyValue> property_value_item = new ArrayList<>(1); property_value_item.add(propertyValue); property.setPropertyValueList(property_value_item); break; } } } JSONObject jsonObject = new JSONObject(); jsonObject.put("propertyList", JSONArray.parseArray(JSON.toJSONString(propertyList))); return jsonObject.toJSONString(); } //加载猜你喜欢列表-ajax @ResponseBody @RequestMapping(value = "guess/{cid}", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public String guessYouLike(@PathVariable("cid") Integer cid, @RequestParam Integer guessNumber) { logger.info("获取猜你喜欢列表"); Integer total = productService.getTotal(new Product().setProduct_category(new Category().setCategory_id(cid)), new Byte[]{0, 2}); logger.info("分类ID为{}的产品总数为{}条", cid, total); //生成随机数 int i = new Random().nextInt(total); if (i + 2 >= total) { i = total - 3; } if (i < 0) { i = 0; } while (i == guessNumber) { i = new Random().nextInt(total); if (i + 2 >= total) { i = total - 3; } if (i < 0) { i = 0; break; } } logger.info("guessNumber值为{},新guessNumber值为{}", guessNumber, i); List<Product> loveProductList = productService.getList(new Product().setProduct_category( new Category().setCategory_id(cid)), new Byte[]{0, 2}, null, new PageUtil().setCount(3).setPageStart(i) ); if (loveProductList != null) { logger.info("获取产品列表的相应的一张预览图片"); for (Product loveProduct : loveProductList) { loveProduct.setSingleProductImageList(productImageService.getList(loveProduct.getProduct_id(), (byte) 0, new PageUtil(0, 1))); } } JSONObject jsonObject = new JSONObject(); logger.info("获取数据成功!"); jsonObject.put("success", true); jsonObject.put("loveProductList", JSONArray.parseArray(JSON.toJSONString(loveProductList))); jsonObject.put("guessNumber", i); return jsonObject.toJSONString(); } }
用户信息管理控制层:
/** * 用户信息管理 */ @Controller public class ForeUserController extends BaseController{ @Resource(name = "addressService") private AddressService addressService; @Resource(name="userService") private UserService userService; //转到前台天猫-用户详情页 @RequestMapping(value = "userDetails", method = RequestMethod.GET) public String goToUserDetail(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("获取用户所在地区级地址"); String districtAddressId = user.getUser_address().getAddress_areaId(); Address districtAddress = addressService.get(districtAddressId); logger.info("获取市级地址信息"); Address cityAddress = addressService.get(districtAddress.getAddress_regionId().getAddress_areaId()); logger.info("获取其他地址信息"); List<Address> addressList = addressService.getRoot(); List<Address> cityList = addressService.getList( null,cityAddress.getAddress_regionId().getAddress_areaId() ); List<Address> districtList = addressService.getList(null,cityAddress.getAddress_areaId()); map.put("addressList", addressList); map.put("cityList", cityList); map.put("districtList", districtList); map.put("addressId", cityAddress.getAddress_regionId().getAddress_areaId()); map.put("cityAddressId", cityAddress.getAddress_areaId()); map.put("districtAddressId", districtAddressId); return "fore/userDetails"; } else { return "redirect:/login"; } } //前台天猫-用户更换头像 @ResponseBody @RequestMapping(value = "user/uploadUserHeadImage", method = RequestMethod.POST, produces = "application/json;charset=utf-8") public String uploadUserHeadImage(@RequestParam MultipartFile file, HttpSession session ){ String originalFileName = file.getOriginalFilename(); logger.info("获取图片原始文件名:{}", originalFileName); String extension = originalFileName.substring(originalFileName.lastIndexOf('.')); String fileName = UUID.randomUUID() + extension; String filePath = session.getServletContext().getRealPath("/") + "res/images/item/userProfilePicture/" + fileName; logger.info("文件上传路径:{}", filePath); JSONObject jsonObject = new JSONObject(); try { logger.info("文件上传中..."); file.transferTo(new File(filePath)); logger.info("文件上传成功!"); jsonObject.put("success", true); jsonObject.put("fileName", fileName); } catch (IOException e) { logger.warn("文件上传失败!"); e.printStackTrace(); jsonObject.put("success", false); } return jsonObject.toJSONString(); } //前台天猫-用户详情更新 @RequestMapping(value="user/update",method=RequestMethod.POST,produces ="application/json;charset=utf-8") public String userUpdate(HttpSession session, Map<String,Object> map, @RequestParam(value = "user_nickname") String user_nickname /*用户昵称 */, @RequestParam(value = "user_realname") String user_realname /*真实姓名*/, @RequestParam(value = "user_gender") String user_gender /*用户性别*/, @RequestParam(value = "user_birthday") String user_birthday /*用户生日*/, @RequestParam(value = "user_address") String user_address /*用户所在地 */, @RequestParam(value = "user_profile_picture_src", required = false) String user_profile_picture_src /* 用户头像*/, @RequestParam(value = "user_password") String user_password/* 用户密码 */ ) throws ParseException, UnsupportedEncodingException { logger.info("检查用户是否登录"); Object userId = checkUser(session); if (userId != null) { logger.info("获取用户信息"); User user = userService.get(Integer.parseInt(userId.toString())); map.put("user", user); } else { return "redirect:/login"; } logger.info("创建用户对象"); if (user_profile_picture_src != null && "".equals(user_profile_picture_src)) { user_profile_picture_src = null; } User userUpdate = new User() .setUser_id(Integer.parseInt(userId.toString())) .setUser_nickname(user_nickname) .setUser_realname(user_realname) .setUser_gender(Byte.valueOf(user_gender)) .setUser_birthday(new SimpleDateFormat("yyyy-MM-dd").parse(user_birthday)) .setUser_address(new Address().setAddress_areaId(user_address)) .setUser_profile_picture_src(user_profile_picture_src) .setUser_password(user_password); logger.info("执行修改"); if (userService.update(userUpdate)){ logger.info("修改成功!跳转到用户详情页面"); return "redirect:/userDetails"; } throw new RuntimeException(); } }
地址信息管理控制层:
/** * 地址信息管理 */ @RestController public class ForeAddressController extends BaseController { @Resource(name = "addressService") private AddressService addressService; //根据address_areaId获取地址信息-ajax @RequestMapping(value = "address/{areaId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8") protected String getAddressByAreaId(@PathVariable String areaId) { JSONObject object = new JSONObject(); logger.info("获取AreaId为{}的地址信息"); List<Address> addressList = addressService.getList(null, areaId); if (addressList == null || addressList.size() <= 0) { object.put("success", false); return object.toJSONString(); } logger.info("获取该地址可能的子地址信息"); List<Address> childAddressList = addressService.getList(null, addressList.get(0).getAddress_areaId()); object.put("success", true); object.put("addressList", addressList); object.put("childAddressList", childAddressList); return object.toJSONString(); } }
到此这篇关于Java 仿天猫服装商城系统的实现流程的文章就介绍到这了,更多相关Java 商城系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!