资源下载地址:https://download.csdn.net/download/sheziqiong/85838111
资源下载地址:https://download.csdn.net/download/sheziqiong/85838111
本系统采用B/S模式,前端和后台均使用框架进行开发,前端使用jsp、js和jq技术,后台使用springmvc、spring、mybatis框架整合开发。实现一个完整的网上商城从用户、商品、订单等的管理与操作。以下为详细的设计介绍:
图1 类图
图2 购物流程时序图
User表,uid为主键。
图3 买家用户实体E-R图:
Product表,pid为主键,cid为外键。cid属于category表的主键。
图4 商品实体E-R图
Order表,oid为主键,aid为外键,aid是address表的主键。
图5 订单实体E-R图
Orderitem表,itemid为主键,oid、pid为外键,oid是order表主键,pid是product表主键。
图6 order item实体E-R图
Address表,aid为主键,uid为外键,uid为user表的主键。
图7 买家地址实体E-R图
后台可以通过该功能模块实现商品上架、商品下架、修改商品信息、查看商品。通过该模块将指定的商品信息增加到数据库中,实现商品上架功能。通过该模块将商城已有商品的商品数目改为0,实现下架商品功能。通过该模块在数据库内修改指定的商品信息,实现修改商品信息功能。
用户通过该模块实现在前台页面查看和搜索商品,并浏览商品。
相关代码:
//商品分类
@RequestMapping("/admin/orderBy")
public String orderBy(String cid,String price,String amount,HttpServletRequest request) {
List<Product> productList = null;
System.out.println("cid:"+cid+" price:"+price+ " amount:"+amount);
if(cid != null) {
productList = service.getProductByDetails(cid,price,amount);
}
request.setAttribute("productList", productList);
return "admin/newProductManage";
}
//弹窗展示获得的商品数据
@RequestMapping("/admin/showProductInfo")
public void showProduct(String pid,HttpServletRequest request,HttpServletResponse response) throws IOException {
Product product = productservice.getProductInfo(pid);
Gson gson = new Gson();
String json = gson.toJson(product);
response.getWriter().write(json);
}
@RequestMapping("/admin/modifyProduct")
public void modifyProduct(Product product,HttpServletResponse response) throws IOException {
System.out.println(product.getPid());
productservice.modifyProduct(product);
response.getWriter().write("{\"isSuc\":"+true+"}");
后台可以通过该功能模块实现增加商品分类,修改商品分类以及删除商品分类等功能要求。对应在数据库中对分类信息进行增删改。
相关代码:
AdminController:
@RequestMapping("/admin/orderManage")
public String orderManage(HttpServletRequest request) {
List<Order> orderList = orderservice.getAllOrder();
Gson gson = new Gson();
String json = gson.toJson(orderList);
request.setAttribute("orderList", orderList);
request.setAttribute("json", json);
return "admin/orderManage";
}
productController:
@RequestMapping("/productByCategory")
public String getOrderByUser(String cid, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
HttpServletRequest request) {
PageBean pagebean = service.getProductListByCid(cid, currentPage);
request.setAttribute("pageBean", pagebean);
request.setAttribute("cid", cid);
return "productList";
}
用户通过该模块查看自己的订单信息、提交订单、删除订单以及更改订单收货状态。
后台通过该模块查看所有用户的订单信息以及更改订单的发货状态。
用户权限分为一般浏览者和买家。一般浏览者可以查看所有商品信息,但是不能购买商品以及没有个人中心,在选择购买时会跳到用户注册登录界面。买家则是已登录的用户,可以购买商品也拥有自己的个人中心,可查看自己的个人信息。
相关代码:
UserInterceptor.java 登录拦截器:
public class UserInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
HttpSession session = request.getSession();
Object user = session.getAttribute("user");
if(user != null) {
return true;
}else {
response.sendRedirect(request.getContextPath()+"/login.html");
}
return false;
}
用户只有在登录后才可以购买商品以及查看个人信息,没有账户的游客则需要进行注册再登录。
图25 注册新账号
相关代码:
UserConroller:
@RequestMapping("/login")
public void login(String account, String password, HttpServletRequest request, HttpServletResponse response)
throws Exception {
String email = "";
String username = "";
boolean flag = false;
try {
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(account);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
if (flag) {
email = account;
} else {
username = account;
}
System.out.println("email:" + email);
System.out.println("username:" + username);
User user = new User();
user.setUsername(username);
user.setEmail(email);
user.setPassword(password);
User logined = service.login(user);
boolean isSuccess = true;
System.out.println(logined);
HttpSession session = request.getSession();
if (logined != null) {
session.setAttribute("user", logined);
response.getWriter().write("{\"isSuccess\":" + isSuccess + "}");
}else {
isSuccess = false;
response.getWriter().write("{\"isSuccess\":" + isSuccess + "}");
}
}
所有人都可以浏览商品。
相关代码:
ProductController:
//根据pid展示商品
@RequestMapping("/product")
public String productInfo(String pid, HttpServletRequest request) {
Product product = service.getProductInfo(pid);
request.setAttribute("product", product);
return "productInfo";
}
indexController:
//展示最新和热门商品
@RequestMapping(value = {"/","/index"})
public String index(Model model) {
List<Product> hotProductList = indexservice.getHotProducts();
List<Product> newProductList = indexservice.getNewProducts();
model.addAttribute("hotProductList", hotProductList);
model.addAttribute("newProductList", newProductList);
return "index";
}
用户可以在这里查看自己的所有信息,以及地址管理和充值服务。
相关代码:
userController:
@RequestMapping("/user/userInfo")
public String toUserInfo() {
return "privilege/userInfo";
}
// 修改用户信息
@RequestMapping("/user/modify")
public void modifyUserInfo(User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println(user);
service.modifyUserInfo(user);
User newUser = service.getUser(user.getUid());
HttpSession session = request.getSession();
Boolean isSuccess = false;
if (newUser != null) {
isSuccess = true;
session.setAttribute("user", newUser);
}
response.getWriter().write("{\"isSuccess\":" + isSuccess + "}");
}
// 前往充值余额页面
@RequestMapping("/user/balance")
public String toBalancePage() {
return "privilege/balance";
}
// 充值余额
@RequestMapping("/user/recharge")
public void recharge(Double pay, HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
Double money = user.getMoney();
Double newMonney = 0.0;
if (pay != 0.0 && pay != null) {
newMonney = money + pay;
}
System.out.println("money:" + newMonney);
user.setMoney(newMonney);
service.modifyUserInfo(user);
user = service.getUser(user.getUid());
System.out.println(user);
session.setAttribute("user", user);
Boolean isSuccess = false;
if (user.getUid() != "") {
isSuccess = true;
}
response.getWriter().write("{\"isSuccess\":" + isSuccess + "}");
}
// 跳转到支付成功页面
@RequestMapping("/user/paySuccess")
public String topaySuccessPage() {
return "privilege/recharge";
}
// getAddsByUser
@RequestMapping("/user/address")
public String addressManage(HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
List<Address> addList = service.getAddress(user.getUid());
request.setAttribute("addList", addList);
return "privilege/addManage";
}
// 添加或修改用户地址
@RequestMapping("/user/addManage")
public String addManage(String aid,String tel,String address,String name,HttpServletRequest request) {
System.out.println(aid);
System.out.println(tel);
System.out.println(address);
System.out.println(name);
Address address1 = new Address();
address1.setAid(aid);
address1.setTel(tel);
address1.setAddress(address);
address1.setName(name);
System.out.println(address1);
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
address1.setUid(user.getUid());
System.out.println(address1);
if(address1.getAid() != null && address1.getAid() != "") {
service.updateAdd(address1);
}else {
service.addAddress(address1);
}
return "redirect:/user/address";
}
//删除地址
@RequestMapping("/user/delAdd")
public String delAdd(@RequestParam(value = "aid") String aid) {
Address address = new Address();
address.setAid(aid);
service.delAdd(address);
return "redirect:/user/address";
}
//设置默认地址
@RequestMapping("/user/setDefaultAdd")
public String setDefaultAdd(String aid,HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
Address address = new Address();
address.setAid(aid);
address.setUid(user.getUid());
service.setDefaultAdd(address);
return "redirect:/user/address";
}
@RequestMapping("/user/setDefaultAddFromCart")
public String setDefaultAddFromCart(String aid,HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
Address address = new Address();
address.setAid(aid);
address.setUid(user.getUid());
service.setDefaultAdd(address);
List<Address> addList = service.getAddress(user.getUid());
session.setAttribute("addListCart", addList);
return "redirect:/cart.html";
}
用户可以将自己心仪的商品先添加至购物车,然后继续浏览其他商品,最后再进行付款提交订单。
图35 购物车为空
相关代码:
Productcontroller:
// 加入购物车
@RequestMapping("/addCart")
public String addCart(String pid, int buyNum, HttpServletRequest request) {
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
}
CartItem cartitem = new CartItem();
Product product = service.getProductInfo(pid);
cartitem.setProduct(product);
cartitem.setBuyNum(buyNum);
double subtotal = product.getPrice() * buyNum;
cartitem.setSubtotal(subtotal);
double total = 0.0d;
Map<String, CartItem> cartItems = cart.getCartItems();
if(cartItems.containsKey(pid)) {
CartItem item = cartItems.get(pid);
int oldNum = item.getBuyNum();
int newNum = oldNum+buyNum;
item.setBuyNum(newNum);
item.setSubtotal(product.getPrice()*newNum);
cart.setCartItems(cartItems);
}else {
cartItems.put(pid, cartitem);
}
total = cart.getTotal()+cartitem.getSubtotal();
cart.setTotal(total);
User user = (User) session.getAttribute("user");
if (user != null) {
List<Address> address = userservice.getAddress(user.getUid());
System.out.println(address);
session.setAttribute("addListCart", address);
}
session.setAttribute("cart", cart);
return "redirect:/cart.html";
}
@RequestMapping("/delCartItem")
public String delCartItem(String pid,HttpServletRequest request) {
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if(cart!=null) {
Map<String, CartItem> cartItems = cart.getCartItems();
//修改总价
double newTotal = cart.getTotal() - cartItems.get(pid).getSubtotal();
cart.setTotal(newTotal);
// 删除
cartItems.remove(pid);
cart.setCartItems(cartItems);
}
session.setAttribute("cart", cart);
return "redirect:/cart.html";
}
@RequestMapping("/removeCart")
public String removeCart(HttpServletRequest request) {
HttpSession session = request.getSession();
session.removeAttribute("cart");
return "redirect:/cart.html";
}
ApplicationContext.xml是web应用中spring框架的配置文件,负责对web系统中beanfactory和各个bean的属性注入以及数据库连接池的配置设置。
部分配置代码如下:
Springmvc.xml是基于SSM的web系统中SpringMVC的配置文件。负责对springmvc的核心组件前端控制器、处理器等进行配置
=“${jdbc.password}” />
Springmvc.xml是基于SSM的web系统中SpringMVC的配置文件。负责对springmvc的核心组件前端控制器、处理器等进行配置
资源下载地址:https://download.csdn.net/download/sheziqiong/85838111
资源下载地址:https://download.csdn.net/download/sheziqiong/85838111