Java项目:网上商城项目(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等等。

权限控制代码:

  1.    
  2.   /**
  3.   * 产品详情页
  4.   */
  5.   @Controller
  6.   public class ForeProductDetailsController extends BaseController {
  7.   @Resource(name = "productService")
  8.   private ProductService productService;
  9.   @Resource(name = "userService")
  10.   private UserService userService;
  11.   @Resource(name = "productImageService")
  12.   private ProductImageService productImageService;
  13.   @Resource(name = "categoryService")
  14.   private CategoryService categoryService;
  15.   @Resource(name = "propertyValueService")
  16.   private PropertyValueService propertyValueService;
  17.   @Resource(name = "propertyService")
  18.   private PropertyService propertyService;
  19.   @Resource(name = "reviewService")
  20.   private ReviewService reviewService;
  21.   @Resource(name = "productOrderItemService")
  22.   private ProductOrderItemService productOrderItemService;
  23.    
  24.   //转到前台天猫-产品详情页
  25.   @RequestMapping(value = "product/{pid}", method = RequestMethod.GET)
  26.   public String goToPage(HttpSession session, Map map,
  27.   @PathVariable("pid") String pid /*产品ID*/) {
  28.   logger.info("检查用户是否登录");
  29.   Object userId = checkUser(session);
  30.   if (userId != null) {
  31.   logger.info("获取用户信息");
  32.   User user = userService.get(Integer.parseInt(userId.toString()));
  33.   map.put("user", user);
  34.   }
  35.   logger.info("获取产品ID");
  36.   Integer product_id = Integer.parseInt(pid);
  37.   logger.info("获取产品信息");
  38.   Product product = productService.get(product_id);
  39.   if (product == null || product.getProduct_isEnabled() == 1) {
  40.   return "redirect:/404";
  41.   }
  42.   logger.info("获取产品子信息-分类信息");
  43.   product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
  44.   logger.info("获取产品子信息-预览图片信息");
  45.   List singleProductImageList = productImageService.getList(product_id, (byte) 0, null);
  46.   product.setSingleProductImageList(singleProductImageList);
  47.   logger.info("获取产品子信息-详情图片信息");
  48.   List detailsProductImageList = productImageService.getList(product_id, (byte) 1, null);
  49.   product.setDetailProductImageList(detailsProductImageList);
  50.   logger.info("获取产品子信息-产品属性值信息");
  51.   List propertyValueList = propertyValueService.getList(new PropertyValue().setPropertyValue_product(product), null);
  52.   logger.info("获取产品子信息-分类信息对应的属性列表");
  53.   List propertyList = propertyService.getList(new Property().setProperty_category(product.getProduct_category()), null);
  54.   logger.info("属性列表和属性值列表合并");
  55.   for (Property property : propertyList) {
  56.   for (PropertyValue propertyValue : propertyValueList) {
  57.   if (property.getProperty_id().equals(propertyValue.getPropertyValue_property().getProperty_id())) {
  58.   List property_value_item = new ArrayList<>(1);
  59.   property_value_item.add(propertyValue);
  60.   property.setPropertyValueList(property_value_item);
  61.   break;
  62.   }
  63.   }
  64.   }
  65.   logger.info("获取产品子信息-产品评论信息");
  66.   product.setReviewList(reviewService.getListByProductId(product_id, null));
  67.   if (product.getReviewList() != null) {
  68.   for (Review review : product.getReviewList()) {
  69.   review.setReview_user(userService.get(review.getReview_user().getUser_id()));
  70.   }
  71.   }
  72.    
  73.   logger.info("获取猜你喜欢列表");
  74.   Integer category_id = product.getProduct_category().getCategory_id();
  75.   Integer total = productService.getTotal(new Product().setProduct_category(new Category().setCategory_id(category_id)), new Byte[]{0, 2});
  76.   logger.info("分类ID为{}的产品总数为{}条", category_id, total);
  77.   //生成随机数
  78.   int i = new Random().nextInt(total);
  79.   if (i + 2 >= total) {
  80.   i = total - 3;
  81.   }
  82.   if (i < 0) {
  83.   i = 0;
  84.   }
  85.   List loveProductList = productService.getList(new Product().setProduct_category(
  86.   new Category().setCategory_id(category_id)),
  87.   new Byte[]{0, 2},
  88.   null,
  89.   new PageUtil().setCount(3).setPageStart(i)
  90.   );
  91.   if (loveProductList != null) {
  92.   logger.info("获取产品列表的相应的一张预览图片");
  93.   for (Product loveProduct : loveProductList) {
  94.   loveProduct.setSingleProductImageList(productImageService.getList(loveProduct.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
  95.   }
  96.   }
  97.   logger.info("获取分类列表");
  98.   List categoryList = categoryService.getList(null, new PageUtil(0, 3));
  99.    
  100.   map.put("loveProductList", loveProductList);
  101.   map.put("categoryList", categoryList);
  102.   map.put("propertyList", propertyList);
  103.   map.put("product", product);
  104.   map.put("guessNumber", i);
  105.   map.put("pageUtil", new PageUtil(0, 10).setTotal(product.getProduct_review_count()));
  106.   logger.info("转到前台-产品详情页");
  107.   return "fore/productDetailsPage";
  108.   }
  109.    
  110.   //按产品ID加载产品评论列表-ajax
  111.   @Deprecated
  112.   @ResponseBody
  113.   @RequestMapping(value = "review/{pid}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
  114.   public String loadProductReviewList(@PathVariable("pid") String pid/*产品ID*/,
  115.   @RequestParam Integer index/* 页数 */,
  116.   @RequestParam Integer count/* 行数 */) {
  117.   logger.info("获取产品ID");
  118.   Integer product_id = Integer.parseInt(pid);
  119.   logger.info("获取产品评论列表");
  120.   List reviewList = reviewService.getListByProductId(product_id, new PageUtil(index, count));
  121.   JSONObject jsonObject = new JSONObject();
  122.   jsonObject.put("reviewList", JSONArray.parseArray(JSON.toJSONString(reviewList)));
  123.    
  124.   return jsonObject.toJSONString();
  125.   }
  126.    
  127.   //按产品ID加载产品属性列表-ajax
  128.   @Deprecated
  129.   @ResponseBody
  130.   @RequestMapping(value = "property/{pid}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
  131.   public String loadProductPropertyList(@PathVariable("pid") String pid/*产品ID*/) {
  132.   logger.info("获取产品ID");
  133.   Integer product_id = Integer.parseInt(pid);
  134.    
  135.   logger.info("获取产品详情-属性值信息");
  136.   Product product = new Product();
  137.   product.setProduct_id(product_id);
  138.   List propertyValueList = propertyValueService.getList(new PropertyValue().setPropertyValue_product(product), null);
  139.    
  140.   logger.info("获取产品详情-分类信息对应的属性列表");
  141.   List propertyList = propertyService.getList(new Property().setProperty_category(product.getProduct_category()), null);
  142.    
  143.   logger.info("属性列表和属性值列表合并");
  144.   for (Property property : propertyList) {
  145.   for (PropertyValue propertyValue : propertyValueList) {
  146.   if (property.getProperty_id().equals(propertyValue.getPropertyValue_property().getProperty_id())) {
  147.   List property_value_item = new ArrayList<>(1);
  148.   property_value_item.add(propertyValue);
  149.   property.setPropertyValueList(property_value_item);
  150.   break;
  151.   }
  152.   }
  153.   }
  154.   JSONObject jsonObject = new JSONObject();
  155.   jsonObject.put("propertyList", JSONArray.parseArray(JSON.toJSONString(propertyList)));
  156.    
  157.   return jsonObject.toJSONString();
  158.   }
  159.    
  160.   //加载猜你喜欢列表-ajax
  161.   @ResponseBody
  162.   @RequestMapping(value = "guess/{cid}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
  163.   public String guessYouLike(@PathVariable("cid") Integer cid, @RequestParam Integer guessNumber) {
  164.   logger.info("获取猜你喜欢列表");
  165.   Integer total = productService.getTotal(new Product().setProduct_category(new Category().setCategory_id(cid)), new Byte[]{0, 2});
  166.   logger.info("分类ID为{}的产品总数为{}条", cid, total);
  167.   //生成随机数
  168.   int i = new Random().nextInt(total);
  169.   if (i + 2 >= total) {
  170.   i = total - 3;
  171.   }
  172.   if (i < 0) {
  173.   i = 0;
  174.   }
  175.   while (i == guessNumber) {
  176.   i = new Random().nextInt(total);
  177.   if (i + 2 >= total) {
  178.   i = total - 3;
  179.   }
  180.   if (i < 0) {
  181.   i = 0;
  182.   break;
  183.   }
  184.   }
  185.    
  186.   logger.info("guessNumber值为{},新guessNumber值为{}", guessNumber, i);
  187.   List loveProductList = productService.getList(new Product().setProduct_category(
  188.   new Category().setCategory_id(cid)),
  189.   new Byte[]{0, 2},
  190.   null,
  191.   new PageUtil().setCount(3).setPageStart(i)
  192.   );
  193.   if (loveProductList != null) {
  194.   logger.info("获取产品列表的相应的一张预览图片");
  195.   for (Product loveProduct : loveProductList) {
  196.   loveProduct.setSingleProductImageList(productImageService.getList(loveProduct.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
  197.   }
  198.   }
  199.    
  200.   JSONObject jsonObject = new JSONObject();
  201.   logger.info("获取数据成功!");
  202.   jsonObject.put("success", true);
  203.   jsonObject.put("loveProductList", JSONArray.parseArray(JSON.toJSONString(loveProductList)));
  204.   jsonObject.put("guessNumber", i);
  205.   return jsonObject.toJSONString();
  206.   }
  207.   }

用户信息管理控制层:

  1.   /**
  2.   * 用户信息管理
  3.   */
  4.   @Controller
  5.   public class ForeUserController extends BaseController{
  6.   @Resource(name = "addressService")
  7.   private AddressService addressService;
  8.   @Resource(name="userService")
  9.   private UserService userService;
  10.    
  11.   //转到前台天猫-用户详情页
  12.   @RequestMapping(value = "userDetails", method = RequestMethod.GET)
  13.   public String goToUserDetail(HttpSession session, Map<String,Object> map){
  14.   logger.info("检查用户是否登录");
  15.   Object userId = checkUser(session);
  16.   if (userId != null) {
  17.   logger.info("获取用户信息");
  18.   User user = userService.get(Integer.parseInt(userId.toString()));
  19.   map.put("user", user);
  20.    
  21.   logger.info("获取用户所在地区级地址");
  22.   String districtAddressId = user.getUser_address().getAddress_areaId();
  23.   Address districtAddress = addressService.get(districtAddressId);
  24.   logger.info("获取市级地址信息");
  25.   Address cityAddress = addressService.get(districtAddress.getAddress_regionId().getAddress_areaId());
  26.   logger.info("获取其他地址信息");
  27.   List<Address> addressList = addressService.getRoot();
  28.   List<Address> cityList = addressService.getList(
  29.   null,cityAddress.getAddress_regionId().getAddress_areaId()
  30.   );
  31.   List<Address> districtList = addressService.getList(null,cityAddress.getAddress_areaId());
  32.    
  33.   map.put("addressList", addressList);
  34.   map.put("cityList", cityList);
  35.   map.put("districtList", districtList);
  36.   map.put("addressId", cityAddress.getAddress_regionId().getAddress_areaId());
  37.   map.put("cityAddressId", cityAddress.getAddress_areaId());
  38.   map.put("districtAddressId", districtAddressId);
  39.   return "fore/userDetails";
  40.   } else {
  41.   return "redirect:/login";
  42.   }
  43.   }
  44.   //前台天猫-用户更换头像
  45.   @ResponseBody
  46.   @RequestMapping(value = "user/uploadUserHeadImage", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
  47.   public String uploadUserHeadImage(@RequestParam MultipartFile file, HttpSession session
  48.   ){
  49.   String originalFileName = file.getOriginalFilename();
  50.   logger.info("获取图片原始文件名:{}", originalFileName);
  51.   String extension = originalFileName.substring(originalFileName.lastIndexOf('.'));
  52.   String fileName = UUID.randomUUID() + extension;
  53.   String filePath = session.getServletContext().getRealPath("/") + "res/images/item/userProfilePicture/" + fileName;
  54.   logger.info("文件上传路径:{}", filePath);
  55.   JSONObject jsonObject = new JSONObject();
  56.   try {
  57.   logger.info("文件上传中...");
  58.   file.transferTo(new File(filePath));
  59.   logger.info("文件上传成功!");
  60.   jsonObject.put("success", true);
  61.   jsonObject.put("fileName", fileName);
  62.   } catch (IOException e) {
  63.   logger.warn("文件上传失败!");
  64.   e.printStackTrace();
  65.   jsonObject.put("success", false);
  66.   }
  67.   return jsonObject.toJSONString();
  68.   }
  69.   //前台天猫-用户详情更新
  70.   @RequestMapping(value="user/update",method=RequestMethod.POST,produces ="application/json;charset=utf-8")
  71.   public String userUpdate(HttpSession session, Map<String,Object> map,
  72.   @RequestParam(value = "user_nickname") String user_nickname /*用户昵称 */,
  73.   @RequestParam(value = "user_realname") String user_realname /*真实姓名*/,
  74.   @RequestParam(value = "user_gender") String user_gender /*用户性别*/,
  75.   @RequestParam(value = "user_birthday") String user_birthday /*用户生日*/,
  76.   @RequestParam(value = "user_address") String user_address /*用户所在地 */,
  77.   @RequestParam(value = "user_profile_picture_src", required = false)
  78.   String user_profile_picture_src /* 用户头像*/,
  79.   @RequestParam(value = "user_password") String user_password/* 用户密码 */
  80.   ) throws ParseException, UnsupportedEncodingException {
  81.   logger.info("检查用户是否登录");
  82.   Object userId = checkUser(session);
  83.   if (userId != null) {
  84.   logger.info("获取用户信息");
  85.   User user = userService.get(Integer.parseInt(userId.toString()));
  86.   map.put("user", user);
  87.   } else {
  88.   return "redirect:/login";
  89.   }
  90.   logger.info("创建用户对象");
  91.   if (user_profile_picture_src != null && "".equals(user_profile_picture_src)) {
  92.   user_profile_picture_src = null;
  93.   }
  94.   User userUpdate = new User()
  95.   .setUser_id(Integer.parseInt(userId.toString()))
  96.   .setUser_nickname(user_nickname)
  97.   .setUser_realname(user_realname)
  98.   .setUser_gender(Byte.valueOf(user_gender))
  99.   .setUser_birthday(new SimpleDateFormat("yyyy-MM-dd").parse(user_birthday))
  100.   .setUser_address(new Address().setAddress_areaId(user_address))
  101.   .setUser_profile_picture_src(user_profile_picture_src)
  102.   .setUser_password(user_password);
  103.   logger.info("执行修改");
  104.   if (userService.update(userUpdate)){
  105.   logger.info("修改成功!跳转到用户详情页面");
  106.   return "redirect:/userDetails";
  107.   }
  108.   throw new RuntimeException();
  109.   }
  110.   }

地址信息管理控制层:

  1.   /**
  2.   * 地址信息管理
  3.   */
  4.   @RestController
  5.   public class ForeAddressController extends BaseController {
  6.   @Resource(name = "addressService")
  7.   private AddressService addressService;
  8.    
  9.   //根据address_areaId获取地址信息-ajax
  10.   @RequestMapping(value = "address/{areaId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
  11.   protected String getAddressByAreaId(@PathVariable String areaId) {
  12.   JSONObject object = new JSONObject();
  13.   logger.info("获取AreaId为{}的地址信息");
  14.   List<Address> addressList = addressService.getList(null, areaId);
  15.   if (addressList == null || addressList.size() <= 0) {
  16.   object.put("success", false);
  17.   return object.toJSONString();
  18.   }
  19.   logger.info("获取该地址可能的子地址信息");
  20.   List<Address> childAddressList = addressService.getList(null, addressList.get(0).getAddress_areaId());
  21.   object.put("success", true);
  22.   object.put("addressList", addressList);
  23.   object.put("childAddressList", childAddressList);
  24.   return object.toJSONString();
  25.   }
  26.   }