基于SSM实现水果蔬菜商城管理系统(一)https://developer.aliyun.com/article/1423109
package com.javapandeng.controller; import com.javapandeng.base.BaseController; import com.javapandeng.po.Comment; import com.javapandeng.service.CommentService; import com.javapandeng.utils.Consts; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * 评论 */ @Controller @RequestMapping("/comment") public class CommentController extends BaseController { @Autowired private CommentService commentService; /** * 添加执行 */ @RequestMapping("/exAdd") public String exAdd(Comment comment, HttpServletRequest request){ Object attribute = request.getSession().getAttribute(Consts.ID); if(attribute==null){ return "redirect:/login/toLogin"; } Integer userId = Integer.valueOf(attribute.toString()); comment.setAddTime(new Date()); comment.setUserId(userId); commentService.insert(comment); return "redirect:/itemOrder/myOrder.action"; } }
package com.javapandeng.controller; import com.javapandeng.base.BaseController; import com.javapandeng.po.User; import com.javapandeng.service.UserService; import com.javapandeng.utils.Consts; import com.javapandeng.utils.Pager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.persistence.criteria.CriteriaBuilder; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/user") public class UserController extends BaseController { @Autowired private UserService userService; /** * 分页查询用户 * @param model * @param user * @return */ @RequestMapping("findBySql") public String findBySql(Model model, User user){ String sql="select * from user where 1=1 "; if(!isEmpty(user.getUserName())){ sql+=" and userName like '%"+user.getUserName()+"%' "; } sql+=" order by id"; Pager<User> pagers= userService.findBySqlRerturnEntity(sql); model.addAttribute("pagers",pagers); model.addAttribute("obj", user); return "user/user"; } /** * 删除用户 * @return */ @RequestMapping("delete") public String delete(Integer id){ userService.deleteById(id); return "redirect:/user/findBySql.action"; } /** * 跳到个人中心页面,产看用户信息 * @return */ @RequestMapping("view") public String view(Model model, HttpServletRequest request){ Object session =request.getSession().getAttribute(Consts.ID); if(session==null){ return "redirect:/login/uLogin.action"; } Integer userId=Integer.valueOf(session.toString()); User byId = userService.getById(userId); model.addAttribute("obj",byId); return "user/view"; } /** * 跳到个人中心页面,产看用户信息 * @return */ @RequestMapping("exUpdate") public String exUpdate(User user,HttpServletRequest request){ Object session =request.getSession().getAttribute(Consts.ID); if(session==null){ return "redirect:/login/uLogin.action"; } user.setId(Integer.valueOf(session.toString())); userService.updateById(user); return "redirect:/user/view.action"; } }
package com.javapandeng.controller; import com.alibaba.fastjson.JSONObject; import com.javapandeng.base.BaseController; import com.javapandeng.po.*; import com.javapandeng.service.*; import com.javapandeng.utils.Consts; import com.javapandeng.utils.Pager; import jdk.nashorn.internal.ir.RuntimeNode; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @Controller @RequestMapping("/itemOrder") public class ItemOrderController extends BaseController { @Autowired private ItemOrderService itemOrderService; @Autowired private UserService userService; @Autowired private CarService carService; @Autowired private OrderDetailService orderDetailService; @Autowired private ItemService itemService; /* 管理员界面查看所有订单 */ @RequestMapping("findBySql") public String findBySql (ItemOrder itemOrder, Model model){ String sql="select * from item_order where 1=1"; if(!isEmpty(itemOrder.getCode())){ sql+=" and code like '%"+itemOrder.getCode()+"%'" ; } sql+=" order by id desc"; Pager<ItemOrder> pagers= itemOrderService.findBySqlRerturnEntity(sql); model.addAttribute("pagers",pagers); model.addAttribute("obj", itemOrder); return "itemOrder/itemOrder"; } /* 用户个人界面查看所有订单 */ @RequestMapping("myOrder") public String myOrder (HttpServletRequest request, Model model){ Object attribute = request.getSession().getAttribute(Consts.ID); if(attribute==null){ return "redirect:/login/uLogin"; } Integer userId= Integer.valueOf(attribute.toString()); //全部订单 String sql = "select * from item_order where user_id="+userId+" order by id desc"; List<ItemOrder> all = itemOrderService.listBySqlReturnEntity(sql); //待发货 String sql2 = "select * from item_order where user_id="+userId+" and status=0 order by id desc"; List<ItemOrder> dfh = itemOrderService.listBySqlReturnEntity(sql2); //已取消 String sql3 = "select * from item_order where user_id="+userId+" and status=1 order by id desc"; List<ItemOrder> yqx = itemOrderService.listBySqlReturnEntity(sql3); //已发货 String sql4 = "select * from item_order where user_id="+userId+" and status=2 order by id desc"; List<ItemOrder> dsh = itemOrderService.listBySqlReturnEntity(sql4); //已收货 String sql5 = "select * from item_order where user_id="+userId+" and status=3 order by id desc"; List<ItemOrder> ysh = itemOrderService.listBySqlReturnEntity(sql5); model.addAttribute("all",all); model.addAttribute("dfh",dfh); model.addAttribute("yqx",yqx); model.addAttribute("dsh",dsh); model.addAttribute("ysh",ysh); return "itemOrder/myOrder"; } /** * 购物车结算提交 */ @RequestMapping("/exAdd") @ResponseBody public String exAdd(@RequestBody List<CarDto> list, HttpServletRequest request){ Object attribute = request.getSession().getAttribute(Consts.ID); JSONObject js = new JSONObject(); if(attribute==null){ js.put(Consts.RES,0);//res=0,在前端页面ajax里提示需要登录 return js.toJSONString(); } Integer userId = Integer.valueOf(attribute.toString()); User byId = userService.getById(userId); if(StringUtils.isEmpty(byId.getAddress())){ js.put(Consts.RES,2);//res=2,在前端页面ajax里提示需要地址 return js.toJSONString(); } List<Integer> ids = new ArrayList<>(); BigDecimal to = new BigDecimal(0); for(CarDto c:list){ ids.add(c.getId()); Car load = carService.load(c.getId()); to = to.add(new BigDecimal(load.getPrice()).multiply(new BigDecimal(c.getNum()))); } ItemOrder order = new ItemOrder(); order.setStatus(0); order.setCode(getOrderNo()); order.setIsDelete(0); order.setTotal(to.setScale(2,BigDecimal.ROUND_HALF_UP).toString()); order.setUserId(userId); order.setAddTime(new Date()); itemOrderService.insert(order); //订单详情放入orderDetail,删除购物车 if(!CollectionUtils.isEmpty(ids)){ for(CarDto c:list){ Car load = carService.load(c.getId()); OrderDetail de = new OrderDetail(); de.setItemId(load.getItemId()); de.setOrderId(order.getId()); de.setStatus(0); de.setNum(c.getNum()); de.setTotal(String.valueOf(c.getNum()*load.getPrice())); orderDetailService.insert(de); //修改成交数 Item load2 = itemService.load(load.getItemId()); load2.setGmNum(load2.getGmNum()+c.getNum()); itemService.updateById(load2); //删除购物车 carService.deleteById(c.getId()); } } js.put(Consts.RES,1); return js.toJSONString(); } private static String date; private static long orderNum = 0L; public static synchronized String getOrderNo(){ String str = new SimpleDateFormat("yyyyMMddHHmm").format(new Date()); if(date==null||!date.equals(str)){ date = str; orderNum = 0L; } orderNum++; long orderNO = Long.parseLong(date)*10000; orderNO += orderNum; return orderNO+""; } /** * 取消订单 */ @RequestMapping("/qx") public String qx(Integer id,Model model){ ItemOrder obj =itemOrderService.load(id); obj.setStatus(1); itemOrderService.updateById(obj); model.addAttribute("obj",obj); return "redirect:/itemOrder/myOrder"; } /** * 后台发货 */ @RequestMapping("/fh") public String fh(Integer id,Model model){ ItemOrder obj =itemOrderService.load(id); obj.setStatus(2); itemOrderService.updateById(obj); model.addAttribute("obj",obj); return "redirect:/itemOrder/findBySql"; } /** * 用户收货 */ @RequestMapping("/sh") public String sh(Integer id,Model model){ ItemOrder obj =itemOrderService.load(id); obj.setStatus(3); itemOrderService.updateById(obj); model.addAttribute("obj",obj); return "redirect:/itemOrder/myOrder"; } /** * 用户评价入口 */ @RequestMapping("/pj") public String pj(Integer id,Model model){ model.addAttribute("id",id); return "itemOrder/pj"; } }
package com.javapandeng.controller; import com.javapandeng.base.BaseController; import com.javapandeng.po.News; import com.javapandeng.service.NewsService; import com.javapandeng.utils.Pager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date; @Controller @RequestMapping("/news") public class NewsController extends BaseController { @Autowired NewsService newsService; @RequestMapping("findBySql") public String findBySql(News news, Model model){ String sql="select * from news where 1=1 "; if(!isEmpty(news.getName())){ sql+=" and name like '%"+news.getName()+"%'"; } sql+="order by id desc"; Pager<News> pagers= newsService.findBySqlRerturnEntity(sql); model.addAttribute("pagers",pagers); model.addAttribute("obj", news); return "news/news"; } /** *跳转到添加公告页面 */ @RequestMapping("add") public String add(){ return "news/add"; } /** *添加公告 */ @RequestMapping("exAdd") public String exAdd(News news){ news.setAddTime(new Date()); newsService.insert(news); return "redirect:/news/findBySql.antion"; } /** * 跳到修改公告页面 * @return */ @RequestMapping("update") public String update(Integer id,Model model){ News news=newsService.getById(id); System.out.println(news); model.addAttribute("obj",news); return "news/update"; } /** * 修改公告并保存 * @return */ @RequestMapping("exUpdate") public String exUpdate(News news){ newsService.updateById(news); return "redirect:/news/findBySql.action"; } /** * 删除公告 * @return */ @RequestMapping("delete") public String delete(Integer id){ newsService.deleteById(id); return "redirect:/news/findBySql.action"; } /** * 前端公告列表 */ @RequestMapping("/list") public String list(Model model){ Pager<News> pagers = newsService.findByEntity(new News()); model.addAttribute("pagers",pagers); return "news/list"; } /** * 公告详情页面 */ @RequestMapping("/view") public String view(Integer id,Model model){ News obj = newsService.load(id); model.addAttribute("obj",obj); return "news/view"; } }
五,项目总结
本项目开发实现基于SSM框来进行,完成了一个主要用于销售用果和蔬菜等产品的在线商城销售管理系统。系统的前端用户可以实现注册登陆、在线浏览商品、在线添加购物车、在线购买、全文搜索、查看购物车、在线收藏、查看个人订单、修改个人信息等。后台管理用户可以管理商品分类、商品信息、订单信息、用户信息、留言信息、公告信息等相关数据。业务功能完整,页面简洁大方。