Java畢業設計實戰之二手書商城系統的實現

一、項目簡述

功能: 主頁顯示商品; 所有二手書商品展示,可進行商品搜索; 點擊商品進入商品詳情頁,具有立即購買和加入購物車功能,可增減購買商品數量亦可手動輸入,熱銷商品展示。 立即購買進入確認訂單頁面,可選擇已經添加的地址,亦可新增地址。 可選擇購買哪些商品,可刪除不需要的商品。 點擊結算進入確認訂單頁面,確認後提交訂單。後臺管理:(修改密碼等),商品管理(商品批量添加、上下架等),訂單管理。

二、項目運行

環境配置:

Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)

項目技術:

JSP +Springboot+ SpringMVC + MyBatis + ThymeLeaf + FTP+ JavaScript + JQuery + Ajax + maven等等

商品管理控制層:

@Controller
public class GoodController {
 
	private final GoodService goodService;
	private final TypeService typeService;
	private final ReviewService reviewService;
	private final UserService userService;
	private final ImageService imageService;
	private final CollectService collectService;
 
	private static String message = "";
 
	@Autowired
	public GoodController(GoodService goodService, TypeService typeService,
			ReviewService reviewService, UserService userService,
			ImageService imageService, CollectService collectService) {
		this.goodService = goodService;
		this.typeService = typeService;
		this.reviewService = reviewService;
		this.userService = userService;
		this.imageService = imageService;
		this.collectService = collectService;
	}
 
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String getHomeGoods(
			ModelMap model,
			@RequestParam(required = false, defaultValue = "") String searchText,
			@RequestParam(required = false) Integer secondTypeId,
			@RequestParam(required = false, defaultValue = "0") int offset,
			@RequestParam(required = false, defaultValue = "40") int limit) {
		List<Good> goods = goodService.getGoodsBySearchAndType(searchText,
				secondTypeId, offset, limit);
		double goodsNum = goodService.getGoodsBySearchAndTypeCount(searchText,
				secondTypeId);
		List<FirstType> firstTypes = typeService.getAllFirstType();
		for (FirstType firstType : firstTypes) {
			firstType.setSecondType(typeService
					.getSecondTypeByFirstTypeId(firstType.getId()));
		}
		model.addAttribute("firstTypes", firstTypes);
		model.addAttribute("goods", goods);
		model.addAttribute("pages", Math.ceil(goodsNum / limit));
		model.addAttribute("goodsNum", goodsNum);
		model.addAttribute("offset", offset);
		model.addAttribute("limit", limit);
		return "home/homeGoods";
	}
 
	@RequestMapping(value = "/goods/goodInfo", method = RequestMethod.GET)
	public String getGoodInfo(ModelMap model, HttpSession httpSession,
			@RequestParam(required = false) Integer goodId) {
		Good goodInfo = goodService.getGoodById(goodId);
		if (goodInfo == null) {
			return "goods/error";
		}
		Integer collect = 1;
		User user = (User) httpSession.getAttribute("user");
		if (user == null) {
			collect = 0;
		} else {
			if (collectService.getCollect(goodId, user.getId())) {
				collect = 2;
			}
		}
		List<Image> images = imageService.getImageByGoodId(goodId);
		User goodUser = userService.getUserById(goodInfo.getUserId());
		goodInfo.setGoodUser(userService.getUserById(goodInfo.getUserId()));
		goodInfo.setGoodSecondType(typeService.getSecondTypeById(goodInfo
				.getSecondTypeId()));
		List<Review> reviews = reviewService.gerReviewByGoodId(goodId);
		for (Review review : reviews) {
			review.setReplys(reviewService.gerReplyByReviewId(review.getId()));
		}
		List<Good> goods = goodService.getRECGoods(goodInfo.getSecondTypeId(),
				goodInfo.getId());
		model.addAttribute("message", message);
		model.addAttribute("reviews", reviews);
		model.addAttribute("goodInfo", goodInfo);
		model.addAttribute("images", images);
		model.addAttribute("goodUser", goodUser);
		model.addAttribute("goods", goods);
		model.addAttribute("collect", collect);
		message = "";
		return "goods/goodInfo";
	}
 
	@RequestMapping(value = "/goods/goodInfo", method = RequestMethod.POST)
	public String putReview(
			@RequestParam(value = "goodId", required = false) Integer goodId,
			@RequestParam(value = "reviewId", required = false) Integer reviewId,
			@RequestParam(value = "fromUserId", required = false) Integer fromUserId,
			@RequestParam(value = "toUserId", required = false) Integer toUserId,
			@RequestParam(value = "fromUser", required = false) String fromUser,
			@RequestParam(value = "toUser", required = false) String toUser,
			@RequestParam(value = "replyText", required = false, defaultValue = "") String replyText,
			@RequestParam(value = "reviewText", required = false, defaultValue = "") String reviewText) {
		if (reviewText.equals("")) {
			if (replyText.equals("")) {
				message = "內容不能為空!";
				return "redirect:/goods/goodInfo?goodId=" + goodId;
			} else {
				Reply reply = new Reply();
				reply.setReviewId(reviewId);
				reply.setFromUser(fromUser);
				reply.setFromUserId(fromUserId);
				reply.setToUser(toUser);
				reply.setToUserId(toUserId);
				reply.setText(replyText);
				if (reviewService.insertReply(reply) == 1) {
					message = "回復成功!";
					return "redirect:/goods/goodInfo?goodId=" + goodId;
				} else {
					message = "回復失敗!";
					return "redirect:/goods/goodInfo?goodId=" + goodId;
				}
			}
		} else {
			Review review = new Review();
			review.setGoodId(goodId);
			review.setFromUser(fromUser);
			review.setFromUserId(fromUserId);
			review.setToUserId(toUserId);
			review.setText(reviewText);
			if (reviewService.insertReview(review) == 1) {
				message = "評論成功!";
				return "redirect:/goods/goodInfo?goodId=" + goodId;
			} else {
				message = "評論失敗!";
				return "redirect:/goods/goodInfo?goodId=" + goodId;
			}
		}
	}
 
	@RequestMapping(value = "/goods/publishGood", method = RequestMethod.GET)
	public String getPublishGood(ModelMap model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:/";
		}
		Good good = new Good();
		List<FirstType> firstTypes = typeService.getAllFirstType();
		List<Good> goods = goodService.getAllGoods(0, 5);
		model.addAttribute("goods", goods);
		model.addAttribute("good", good);
		model.addAttribute("firstTypes", firstTypes);
		return "goods/publishGood";
	}
 
	@RequestMapping(value = "/goods/publishGood", method = RequestMethod.POST)
	public String getGoodId(ModelMap model, HttpSession session,
			@Valid Good good) {
		List<FirstType> firstTypes = typeService.getAllFirstType();
		User user = (User) session.getAttribute("user");
		List<Good> goods = goodService.getAllGoods(0, 5);
		good.setUserId(user.getId());
		good.setPhotoUrl("/statics/image/goods/default/nophoto.png");
		if (goodService.insertGood(good) != 1) {
			System.out.println("插入物品失敗!");
		}
		model.addAttribute("goods", goods);
		model.addAttribute("good", good);
		model.addAttribute("firstTypes", firstTypes);
		return "goods/publishGood";
	}
 
	@RequestMapping(value = "/goods/publishGood/uploadImage", method = RequestMethod.POST)
	public String uploadImage(
			HttpSession session,
			@RequestParam(value = "goodId", required = false) Integer goodId,
			@RequestParam(value = "mainFile", required = false) MultipartFile mainFile,
			@RequestParam(value = "file", required = false) MultipartFile[] file)
			throws IOException {
		User user = (User) session.getAttribute("user");
		FileCheck fileCheck = new FileCheck();
		RandomString randomString = new RandomString();
		String filePath = "/statics/image/goods/" + user.getId() + "/" + goodId;
		String pathRoot = fileCheck.checkGoodFolderExist(filePath);
		String name;
		if (!mainFile.isEmpty()) {
			String fileName = goodId + randomString.getRandomString(10);
			String contentType = mainFile.getContentType();
			String imageName = contentType
					.substring(contentType.indexOf("/") + 1);
			name = fileName + "." + imageName;
			mainFile.transferTo(new File(pathRoot + name));
			String photoUrl = filePath + "/" + name;
			goodService.updateGoodPhotoUrl(photoUrl, goodId);
		}
		for (MultipartFile mf : file) {
			if (!mf.isEmpty()) {
				// 生成uuid作為文件名稱
				String fileName = goodId + randomString.getRandomString(10);
				// 獲得文件類型(可以判斷如果不是圖片,禁止上傳)
				String contentType = mf.getContentType();
				// 獲得文件後綴名稱
				String imageName = contentType.substring(contentType
						.indexOf("/") + 1);
				name = fileName + "." + imageName;
				System.out.println("name:" + name);
				mf.transferTo(new File(pathRoot + name));
				Image image = new Image();
				image.setGoodId(goodId);
				image.setName(name);
				image.setUrl(filePath + "/" + name);
				imageService.insertImage(image);
			} else {
				System.out.println("文件為空!");
			}
		}
		return "redirect:/goods/goodInfo?goodId=" + goodId;
	}
 
	@RequestMapping(value = "/goods/userGoods", method = RequestMethod.GET)
	public String getUserGoods(ModelMap model,
			@RequestParam(value = "userId", required = false) Integer userId) {
		User user = userService.getUserById(userId);
		List<Good> userGoods = goodService.getGoodStatusByUserId(userId);
		List<Good> goods = goodService.getAllGoods(0, 4);
		model.addAttribute("user", user);
		model.addAttribute("userGoods", userGoods);
		model.addAttribute("goods", goods);
		return "goods/userGood";
	}
 
	@RequestMapping(value = "/goods/userGoodEdit", method = RequestMethod.GET)
	public String getUserGoodEdit(ModelMap model,
			@RequestParam(value = "goodId", required = false) Integer goodId,
			HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:/";
		}
		Good good = goodService.getGoodById(goodId);
		List<FirstType> firstTypes = typeService.getAllFirstType();
		List<Good> goods = goodService.getAllGoods(0, 5);
		List<Image> goodImages = imageService.getImageByGoodId(goodId);
		model.addAttribute("goods", goods);
		model.addAttribute("good", good);
		model.addAttribute("goodImages", goodImages);
		model.addAttribute("firstTypes", firstTypes);
		return "goods/userGoodEdit";
	}
 
	@RequestMapping(value = "/goods/userGoodEdit", method = RequestMethod.POST)
	public String postGoodEdit(ModelMap model, HttpSession session,
			@Valid Good good) {
		List<FirstType> firstTypes = typeService.getAllFirstType();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:/";
		}
		List<Good> goods = goodService.getAllGoods(0, 5);
		if (!(goodService.updateGood(good) > 0)) {
			System.out.println("修改物品失敗!");
		}
		List<Image> goodImages = imageService.getImageByGoodId(good.getId());
		model.addAttribute("goods", goods);
		model.addAttribute("good", good);
		model.addAttribute("goodImages", goodImages);
		model.addAttribute("firstTypes", firstTypes);
		return "goods/userGoodEdit";
	}
 
	@RequestMapping(value = "/goods/userGoodEdit/updateImage", method = RequestMethod.POST)
	public String updateImage(
			HttpSession session,
			@RequestParam(value = "goodId", required = false) Integer goodId,
			@RequestParam(value = "mainFile", required = false) MultipartFile mainFile,
			@RequestParam(value = "file", required = false) MultipartFile[] file)
			throws IOException {
		User user = (User) session.getAttribute("user");
		FileCheck fileCheck = new FileCheck();
		imageService.deleteImage(goodId);
		RandomString randomString = new RandomString();
		String filePath = "/statics/image/goods/" + user.getId() + "/" + goodId;
		String pathRoot = fileCheck.checkGoodFolderExist(filePath);
		String name;
		if (!mainFile.isEmpty()) {
			String contentType = mainFile.getContentType();
			String fileName = goodId + randomString.getRandomString(10);
			String imageName = contentType
					.substring(contentType.indexOf("/") + 1);
			name = fileName + "." + imageName;
			mainFile.transferTo(new File(pathRoot + name));
			String photoUrl = filePath + "/" + name;
			goodService.updateGoodPhotoUrl(photoUrl, goodId);
		}
 
		for (MultipartFile mf : file) {
			if (!mf.isEmpty()) {
				String contentType = mf.getContentType();
				String fileName = goodId + randomString.getRandomString(10);
				String imageName = contentType.substring(contentType
						.indexOf("/") + 1);
				name = fileName + "." + imageName;
				System.out.println("name:" + name);
				mf.transferTo(new File(pathRoot + name));
				Image image = new Image();
				image.setGoodId(goodId);
				image.setName(name);
				image.setUrl(filePath + "/" + name);
				imageService.insertImage(image);
			}
		}
		return "redirect:/goods/goodInfo?goodId=" + goodId;
	}
 
	@RequestMapping(value = "/goods/userGoodEdit/delete/{goodId}", method = RequestMethod.GET)
	public ResponseEntity deleteGood(@PathVariable Integer goodId) {
		Boolean success;
		success = goodService.deleteGood(goodId) > 0;
		return ResponseEntity.ok(success);
	}
 
	@RequestMapping(value = "/goods/userGoodEdit/updateGoodStatus/{goodId}", method = RequestMethod.GET)
	public ResponseEntity updateGoodStatus(@PathVariable Integer goodId) {
		Boolean success;
		success = goodService.updateGoodStatusId(0, goodId) > 0;
		return ResponseEntity.ok(success);
	}
 
	@RequestMapping(value = "/admin/goods/allGoods", method = RequestMethod.GET)
	public ResponseEntity adminGetAllGoods() {
		List<Good> goodList = goodService.getAllGoodList();
		for (Good good : goodList) {
			good.setGoodUser(userService.getUserById(good.getUserId()));
			good.setGoodSecondType(typeService.getSecondTypeById(good
					.getSecondTypeId()));
		}
		return ResponseEntity.ok(goodList);
	}
}

訂單管理控制層:

@Controller
public class OrderController {
	private final GoodService goodService;
	private final OrderService orderService;
 
	@Autowired
	public OrderController(GoodService goodService, OrderService orderService) {
		this.goodService = goodService;
		this.orderService = orderService;
	}
 
	@RequestMapping(value = "/user/orderInfo", method = RequestMethod.GET)
	public String getOrderInfo(ModelMap model,
			@RequestParam(value = "orderId", required = false) Integer orderId,
			HttpSession session) {
		User sessionUser = (User) session.getAttribute("user");
		if (sessionUser == null) {
			return "redirect:/";
		}
		Order orderInfo = orderService.getOrderById(orderId);
		List<Order> orders = orderService.getOtherOrderByCustomerId(
				sessionUser.getId(), orderId);
		model.addAttribute("orderInfo", orderInfo);
		model.addAttribute("orders", orders);
		return "user/orderInfo";
	}
 
	@RequestMapping(value = "/user/sellerInfo", method = RequestMethod.GET)
	public String getSellerInfo(ModelMap model,
			@RequestParam(value = "orderId", required = false) Integer orderId,
			HttpSession session) {
		User sessionUser = (User) session.getAttribute("user");
		if (sessionUser == null) {
			return "redirect:/";
		}
		Order orderInfo = orderService.getOrderById(orderId);
		List<Order> orders = orderService.getOtherOrderBySellerId(
				sessionUser.getId(), orderId);
		model.addAttribute("orderInfo", orderInfo);
		model.addAttribute("orders", orders);
		System.out.println("sellerInfo.size:" + orders.size());
		return "user/sellerInfo";
	}
 
	@RequestMapping(value = "/user/order/delete/{orderId}", method = RequestMethod.GET)
	public ResponseEntity deleteOrderById(@PathVariable Integer orderId) {
		Boolean success;
		success = orderService.deleteOrderById(orderId) > 0;
		return ResponseEntity.ok(success);
	}
 
	@RequestMapping(value = "/user/sellerOrder/delete/{orderId}&{goodId}", method = RequestMethod.GET)
	public ResponseEntity deleteSellerOrderById(@PathVariable Integer orderId,
			@PathVariable Integer goodId) {
		Boolean success;
		success = goodService.updateGoodStatusId(1, goodId) > 0;
		if (success) {
			success = orderService.deleteOrderById(orderId) > 0;
		}
		return ResponseEntity.ok(success);
	}
 
	@RequestMapping(value = "/user/order/update/status/{orderId}&{statusId}", method = RequestMethod.GET)
	public ResponseEntity updateOrderStatus(@PathVariable Integer orderId,
			@PathVariable Integer statusId) {
		Boolean success = orderService.updateStatus(statusId, orderId) > 0;
		if (success) {
			Order order = orderService.getOrderById(orderId);
			return ResponseEntity.ok(order);
		}
		return ResponseEntity.ok(success);
	}
 
	@RequestMapping(value = "/user/order/create", method = RequestMethod.POST)
	public ResponseEntity createOrder(@RequestBody Order order) {
		Boolean success = orderService.insertOrder(order) > 0;
		if (success) {
			success = goodService.updateGoodStatusId(0, order.getGoodId()) > 0;
			if (success) {
				return ResponseEntity.ok(order.getId());
			} else {
				orderService.deleteOrderById(order.getId());
				return ResponseEntity.ok(success);
			}
		}
		return ResponseEntity.ok(success);
	}
 
	@RequestMapping(value = "/user/order/allOrder", method = RequestMethod.GET)
	public ResponseEntity getAllOrders() {
		List<Order> orderList = orderService.getOrderList();
		return ResponseEntity.ok(orderList);
	}
}

用戶管理控制層:

@Controller
@RequestMapping(value = "user")
public class UserController {
	private final GoodService goodService;
	private final OrderService orderService;
	private final ReviewService reviewService;
	private final UserService userService;
	private final CollectService collectService;
 
	@Autowired
	public UserController(GoodService goodService, OrderService orderService,
			ReviewService reviewService, UserService userService,
			CollectService collectService) {
		this.goodService = goodService;
		this.orderService = orderService;
		this.reviewService = reviewService;
		this.userService = userService;
		this.collectService = collectService;
	}
 
	@RequestMapping(value = "userProfile", method = RequestMethod.GET)
	public String getMyProfile(ModelMap model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:/";
		}
		List<Collect> collects = collectService
				.getCollectByUserId(user.getId());
		for (Collect collect : collects) {
			collect.setGood(goodService.getGoodById(collect.getGoodId()));
		}
		List<Good> goods = goodService.getGoodByUserId(user.getId());
		List<Order> orders = orderService.getOrderByCustomerId(user.getId());
		List<Review> reviews = reviewService.gerReviewByToUserId(user.getId());
		List<Reply> replies = reviewService.gerReplyByToUserId(user.getId());
		List<Order> sellGoods = orderService.getOrderBySellerId(user.getId());
		model.addAttribute("collects", collects);
		model.addAttribute("goods", goods);
		model.addAttribute("orders", orders);
		model.addAttribute("reviews", reviews);
		model.addAttribute("replies", replies);
		model.addAttribute("sellGoods", sellGoods);
		return "user/userProfile";
	}
 
	@RequestMapping(value = "/review", method = RequestMethod.GET)
	public String getReviewInfo(@RequestParam(required = false) Integer goodId,
			@RequestParam(required = false) Integer reviewId) {
		System.out.println("reviewId" + reviewId);
		if (reviewId != null) {
			System.out.println("reviewId" + reviewId);
			if (reviewService.updateReviewStatus(1, reviewId) == 1) {
				return "redirect:/goods/goodInfo?goodId=" + goodId;
			}
		}
		return "redirect:/user/userProfile";
	}
 
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	public String getReplyInfo(
			@RequestParam(required = false) Integer reviewId,
			@RequestParam(required = false) Integer replyId) {
		if (replyId != null) {
			if (reviewService.updateReplyStatus(1, replyId) == 1) {
				Integer goodId = reviewService.getGoodIdByReviewId(reviewId);
				return "redirect:/goods/goodInfo?goodId=" + goodId;
			}
		}
		return "redirect:/user/userProfile";
	}
 
	@RequestMapping(value = "/userEdit", method = RequestMethod.GET)
	public String getUserEdit(ModelMap model,
			@RequestParam(value = "userId", required = false) Integer userId,
			HttpSession session) {
		User sessionUser = (User) session.getAttribute("user");
		if (sessionUser == null) {
			return "redirect:/";
		}
		User user = userService.getUserById(userId);
		List<Order> sellGoods = orderService.getOrderBySellerId(user.getId());
		List<Review> reviews = reviewService.gerReviewByToUserId(user.getId());
		List<Reply> replies = reviewService.gerReplyByToUserId(user.getId());
		model.addAttribute("user", user);
		model.addAttribute("sellGoods", sellGoods);
		model.addAttribute("reviews", reviews);
		model.addAttribute("replies", replies);
		return "user/userEdit";
	}
 
	@RequestMapping(value = "/userEdit", method = RequestMethod.POST)
	public String postUserEdit(ModelMap model, @Valid User user,
			HttpSession session,
			@RequestParam(value = "photo", required = false) MultipartFile photo)
			throws IOException {
		String status;
		Boolean insertSuccess;
		User sessionUser = (User) session.getAttribute("user");
		user.setId(sessionUser.getId());
		InfoCheck infoCheck = new InfoCheck();
		if (!infoCheck.isMobile(user.getMobile())) {
			status = "請輸入正確的手機號!";
		} else if (!infoCheck.isEmail(user.getEmail())) {
			status = "請輸入正確的郵箱!";
		} else if (userService.getUserByMobile(user.getMobile()).getId() != user
				.getId()) {
			System.out.println(userService.getUserByMobile(user.getMobile())
					.getId() + " " + user.getId());
			status = "此手機號碼已使用!";
		} else if (userService.getUserByEmail(user.getEmail()).getId() != user
				.getId()) {
			status = "此郵箱已使用!";
		} else {
			if (!photo.isEmpty()) {
				RandomString randomString = new RandomString();
				FileCheck fileCheck = new FileCheck();
				String filePath = "/statics/image/photos/" + user.getId();
				String pathRoot = fileCheck.checkGoodFolderExist(filePath);
				String fileName = user.getId()
						+ randomString.getRandomString(10);
				String contentType = photo.getContentType();
				String imageName = contentType.substring(contentType
						.indexOf("/") + 1);
				String name = fileName + "." + imageName;
				photo.transferTo(new File(pathRoot + name));
				String photoUrl = filePath + "/" + name;
				user.setPhotoUrl(photoUrl);
			} else {
				String photoUrl = userService.getUserById(user.getId())
						.getPhotoUrl();
				user.setPhotoUrl(photoUrl);
			}
			insertSuccess = userService.updateUser(user);
			if (insertSuccess) {
				session.removeAttribute("user");
				session.setAttribute("user", user);
				return "redirect:/user/userProfile";
			} else {
				status = "修改失敗!";
				model.addAttribute("user", user);
				model.addAttribute("status", status);
				return "user/userEdit";
			}
		}
		System.out.println(user.getMobile());
		System.out.println(status);
		model.addAttribute("user", user);
		model.addAttribute("status", status);
		return "user/userEdit";
	}
 
	@RequestMapping(value = "/password/edit", method = RequestMethod.POST)
	public ResponseEntity editPassword(@RequestBody Password password) {
		User user = userService.getUserById(password.getUserId());
		String oldPass = DigestUtils
				.md5DigestAsHex((password.getOldPassword() + user.getCode())
						.getBytes());
		if (oldPass.equals(user.getPassword())) {
			RandomString randomString = new RandomString();
			String code = (randomString.getRandomString(5));
			String md5Pass = DigestUtils.md5DigestAsHex((password
					.getNewPassword() + code).getBytes());
			Boolean success = userService.updatePassword(md5Pass, code,
					password.getUserId());
			if (success) {
				return ResponseEntity.ok(true);
			} else {
				return ResponseEntity.ok("密碼修改失敗!");
			}
		} else {
			return ResponseEntity.ok("原密碼輸入不正確!");
		}
	}
 
}

到此這篇關於Java畢業設計實戰之二手書商城系統的實現的文章就介紹到這瞭,更多相關Java 二手書商城系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: