Java项目:在线书城书店系统(java+jdbc+Servlet+mysql)
Posted qq_1334611189
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java项目:在线书城书店系统(java+jdbc+Servlet+mysql)相关的知识,希望对你有一定的参考价值。
一个基于Java的网上书店的设计与实现,归纳出了几个模块,首先是登录注册模块,购物车模块,订单模块,个人中心模块,用户管理模块,图书管理模块等。
该项目是java技术的实战操作,采用了MVC设计模式,查询分页,持久化层方法的封装等等,对java技术的巩固很有帮助,为J2EE的学习打下基础,适用于课程设计,毕业设计。
书信息控制层:
@Controller
@RequestMapping("/book")
public class BookInfoController
@Autowired
private IBookInfoService bookInfoService;
@Autowired
private BookDescMapper bookDescMapper;
/**
* 查询某一本书籍详情
*
* @param bookId
* @param model
* @return
*/
@RequestMapping("/info/bookId")
public String bookInfo(@PathVariable("bookId") Integer bookId, Model model) throws BSException
//查询书籍
BookInfo bookInfo = bookInfoService.findById(bookId);
//查询书籍推荐列表
List<BookInfo> 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";
/**
* 通过关键字和书籍分类搜索书籍列表
*
* @param keywords
* @return
*/
@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<BookInfo> 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";
/**
* 加入购物车
*
* @param bookId
* @param request
* @return
*/
@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";
/**
* 更新某个购物车项的购买数量
* @param bookId
* @param newNum
* @param request
* @return
*/
@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;
/**
* 填写订单信息页面
*
* @param bookId
* @param buyNum
* @param request
* @return
*/
@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";
/**
* 订单列表
*
* @return
*/
@GetMapping("/list")
public String orderList(HttpServletRequest request)
User loginUser = (User) request.getSession().getAttribute("loginUser");
List<OrderCustom> orderCustoms = orderService.findOrdersByUserId(loginUser.getUserId());
request.setAttribute("orderCustoms", orderCustoms);
return "order_list";
/**
* 创建订单
*
* @return
*/
@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";
/**
* 确认收货
*
* @param orderId
* @return
*/
@RequestMapping("/confirm/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";
以上是关于Java项目:在线书城书店系统(java+jdbc+Servlet+mysql)的主要内容,如果未能解决你的问题,请参考以下文章
Java精品项目源码第33期前后端分离系统在线书城系统推荐版