一个基于Java的网上书店的设计与实现,归纳出了几个模块,首先是登录注册模块,购物车模块,订单模块,个人中心模块,用户管理模块,图书管理模块等。
该项目是java技术的实战 *** 作,采用了MVC设计模式,查询分页,持久化层方法的封装等等,对java技术的巩固很有帮助,为J2EE的学习打下基础,适用于课程设计,毕业设计。
@Controller @RequestMapping("/book") public class BookInfoController { @Autowired private IBookInfoService bookInfoService; @Autowired private BookDescMapper bookDescMapper; @RequestMapping("/info/{bookId}") public String bookInfo(@PathVariable("bookId") Integer bookId, Model model) throws BSException { //查询书籍 BookInfo bookInfo = bookInfoService.findById(bookId); //查询书籍推荐列表 List购物车控制层:recommendBookList = bookInfoService.findBookListByCateId(bookInfo.getBookCategoryId(), 1, 5); //查询书籍详情 BookDesc bookDesc = bookDescMapper.selectByPrimaryKey(bookId); //增加访问量 bookInfoService.addLookMount(bookInfo); Collections.shuffle(recommendBookList); model.addAttribute("bookInfo", bookInfo); model.addAttribute("bookDesc", bookDesc); model.addAttribute("recommendBookList", recommendBookList); return "book_info"; } @RequestMapping("/list") public String bookSearchList(@RequestParam(defaultValue = "", required = false) String keywords, @RequestParam(defaultValue = "0", required = false) int cateId,//分类Id,默认为0,即不按照分类Id查 @RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "6", required = false) int pageSize, Model model) { keywords = keywords.trim(); PageInfo bookPageInfo = bookInfoService.findBookListByCondition(keywords, cateId, page, pageSize,0);//storeId为0,不按照商店Id查询 model.addAttribute("bookPageInfo", bookPageInfo); model.addAttribute("keywords", keywords); model.addAttribute("cateId", cateId); return "book_list"; } }
@Controller @RequestMapping("/cart") public class CartController { @Autowired private IBookInfoService bookInfoService; @Autowired private ICartService cartService; //返回购物差页面 @GetMapping("/items") public String showCart() { return "cart"; } @RequestMapping("/addition") public String addToCart(@RequestParam(value = "bookId",defaultValue = "0") int bookId, @RequestParam(required = false,defaultValue = "0") int buyNum, HttpServletRequest request) { Cart cart = (Cart) request.getSession().getAttribute("cart"); //根据要加入购物车的bookId查询bookInfo BookInfo bookInfo = bookInfoService.queryBookAvailable(bookId); if (bookInfo != null) { //这本书在数据库里 BSResult bsResult = cartService.addToCart(bookInfo, cart, buyNum); request.getSession().setAttribute("cart", bsResult.getData()); request.setAttribute("bookInfo", bookInfo); } else { //数据库里没有这本书,或库存不足 request.setAttribute("bookInfo", null); } return "addcart"; } @GetMapping("/clear") public String clearCart(HttpServletRequest request) { cartService.clearCart(request,"cart"); return "cart"; } @GetMapping("/deletion/{bookId}") public String deleteCartItem(@PathVariable("bookId") int bookId,HttpServletRequest request){ cartService.deleteCartItem(bookId, request); return "redirect:/cart/items"; } @PostMapping("/buy/num/update") @ResponseBody public BSResult updateBuyNum(int bookId, int newNum, HttpServletRequest request){ return cartService.updateBuyNum(bookId, newNum, request); } @PostMapping("/checkOne") @ResponseBody public BSResult checkACartItem(int bookId,HttpServletRequest request){ Cart cart = (Cart)request.getSession().getAttribute("cart"); return cartService.checkedOrNot(cart, bookId); } }订单信息控制层:
@Controller @RequestMapping("/order") public class OrderController { @Autowired private IOrderService orderService; @Autowired private ICartService cartService; @Autowired private IBookInfoService bookInfoService; @GetMapping("/info") public String orderInfo(@RequestParam(required = false, defaultValue = "0") int bookId, @RequestParam(required = false, defaultValue = "0") int buyNum, HttpServletRequest request) throws BSException { if (bookId != 0) { //点了立即购买,放到request域中,也session的立即购买域中以区分购物车中的书籍 BookInfo bookInfo = bookInfoService.findById(bookId); if (bookInfo != null) { BSResult bsResult = cartService.addToCart(bookInfo, null, buyNum); request.getSession().setAttribute("buyNowCart", bsResult.getData()); request.setAttribute("cart", bsResult.getData()); return "order_info"; } else { request.setAttribute("exception", "不好意思,书籍库存不足或不存在了!"); return "exception"; } } //没有点立即购买,购物车中的总金额大于0才让填写订单信息 Cart cart = (Cart) request.getSession().getAttribute("cart"); if (cart != null && cart.getTotal() > 0) { return "order_info"; } else { return "cart"; } } @GetMapping("/payPage/{orderId}") public String toPay(@PathVariable("orderId") String orderId, Model model) { BSResult bsResult = orderService.findOrderById(orderId); if (bsResult.getCode() == 200) { model.addAttribute("order", bsResult.getData()); return "payment"; } return "exception"; } @RequestMapping("/deletion/{orderId}") public String deletion(@PathVariable("orderId") String orderId) { BSResult bsResult = orderService.deleteOrder(orderId); if (bsResult.getCode() == 200) { return "redirect:/order/list"; } return "exception"; } @GetMapping("/list") public String orderList(HttpServletRequest request) { User loginUser = (User) request.getSession().getAttribute("loginUser"); ListorderCustoms = orderService.findOrdersByUserId(loginUser.getUserId()); request.setAttribute("orderCustoms", orderCustoms); return "order_list"; } @PostMapping("/creation") public String createOrder(User userDTO, String express, int payMethod, HttpServletRequest request) { //立即购买,优先创建订单 Cart buyNowCart = (Cart) request.getSession().getAttribute("buyNowCart"); User loginUser = (User) request.getSession().getAttribute("loginUser"); userDTO.setUserId(loginUser.getUserId()); userDTO.setZipCode(loginUser.getZipCode()); if (buyNowCart != null) { BSResult bsResult = orderService.createOrder(buyNowCart, userDTO, express, payMethod); if (bsResult.getCode() == 200) { request.setAttribute("order", bsResult.getData()); cartService.clearCart(request, "buyNowCart"); return "payment"; } else { request.setAttribute("exception", bsResult.getMessage()); return "exception"; } } //普通购物车 Cart cart = (Cart) request.getSession().getAttribute("cart"); if (cart != null) { BSResult bsResult = orderService.createOrder(cart, userDTO, express, payMethod); if (bsResult.getCode() == 200) { request.setAttribute("order", bsResult.getData()); cartService.clearCart(request, "cart"); return "payment"; } else { request.setAttribute("exception", bsResult.getMessage()); return "exception"; } } else { request.setAttribute("exception", "购物车为空!"); return "exception"; } } @RequestMapping("//confirm/i/{orderId}") public String confirmReceiving(@PathVariable("orderId") String orderId, Model model) { BSResult bsResult = orderService.confirmReceiving(orderId); if (bsResult.getCode() == 200) { return "redirect:/order/list"; } else { model.addAttribute("exception", bsResult.getMessage()); return "exception"; } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)