Java專案:網上商城專案(java+SSM+jsp+mysql+maven)
阿新 • • 發佈:2021-11-23
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等等。
許可權控制程式碼:
- /**
- * 產品詳情頁
- */
- public class ForeProductDetailsController extends BaseController {
- private ProductService productService;
- private UserService userService;
- private ProductImageService productImageService;
- private CategoryService categoryService;
- private PropertyValueService propertyValueService;
- private PropertyService propertyService;
- private ReviewService reviewService;
- private ProductOrderItemService productOrderItemService;
- //轉到前臺天貓-產品詳情頁
- public String goToPage(HttpSession session, Map<String, Object> map,
- /*產品ID*/) { String pid
- 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
- public String loadProductReviewList(/*產品ID*/, String pid
- /* 頁數 */, Integer index
- /* 行數 */) { 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
- public String loadProductPropertyList(/*產品ID*/) { String pid
- 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
- public String guessYouLike( { Integer cid, 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();
- }
- }
使用者資訊管理控制層:
- /**
- * 使用者資訊管理
- */
- public class ForeUserController extends BaseController{
- "addressService") (name =
- private AddressService addressService;
- "userService") (name=
- private UserService userService;
- //轉到前臺天貓-使用者詳情頁
- "userDetails", method = RequestMethod.GET) (value =
- 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";
- }
- }
- //前臺天貓-使用者更換頭像
- "user/uploadUserHeadImage", method = RequestMethod.POST, produces = "application/json;charset=utf-8") (value =
- public String uploadUserHeadImage( 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();
- }
- //前臺天貓-使用者詳情更新
- "user/update",method=RequestMethod.POST,produces ="application/json;charset=utf-8") (value=
- public String userUpdate(HttpSession session, Map<String,Object> map,
- "user_nickname") String user_nickname /*使用者暱稱 */, (value =
- "user_realname") String user_realname /*真實姓名*/, (value =
- "user_gender") String user_gender /*使用者性別*/, (value =
- "user_birthday") String user_birthday /*使用者生日*/, (value =
- "user_address") String user_address /*使用者所在地 */, (value =
- "user_profile_picture_src", required = false) (value =
- String user_profile_picture_src /* 使用者頭像*/,
- "user_password") String user_password/* 使用者密碼 */ (value =
- ) 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();
- }
- }
地址資訊管理控制層:
- /**
- * 地址資訊管理
- */
- public class ForeAddressController extends BaseController {
- "addressService") (name =
- private AddressService addressService;
- //根據address_areaId獲取地址資訊-ajax
- "address/{areaId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8") (value =
- protected String getAddressByAreaId( 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();
- }
- }