基于javaweb+mysql的ssm水果蔬菜商城系统(java+ssm+jsp+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM水果蔬菜商城系统(java+ssm+jsp+mysql)
项目描述:这是一个基于SSM框架开发的水果蔬菜商城系统。首先,这个项目页面简洁清爽,易于理解和学习。其次,这个项目功能丰富,具有一个在线水果蔬菜商城该有的所有功能,并且还涉及到沙箱支付宝支付等等技术亮点。
项目功能:此项目分为三个角色:客户、卖家和管理员。客户有登录注册、浏览商品信息、添加购物车、支付订单、收藏商品、评论商品等等功能。卖家有管理自己的商品、管理自己的订单、管理自己个人信息等等功能。管理员具有管理所有用户信息、管理所有商品信息、管理所有评论信息、管理商品种类信息、管理所有订单信息等等功能。
应用技术:Jsp + SSM + MySQL + 沙箱支付宝
运行环境:Eclipse/IntelliJ IDEA + MySQL5.7(项目压缩包中自带) + Tomcat7.0(项目压缩包中自带) + JDK1.8
}
/**
* 根据列表生成二级树形关系分类
* @param productCategorieList
* @return
*/
private List<Map<String, Object>> getTreeCategory(List<ProductCategory> productCategorieList){
List<Map<String, Object>> ret = new ArrayList<Map<String,Object>>();
//所有的父分类整理
for(ProductCategory productCategory : productCategorieList){
if(productCategory.getParentId() == null){
Map<String, Object> top = new HashMap<String, Object>();
top.put("id", productCategory.getId());
top.put("text", productCategory.getName());
top.put("children", new ArrayList<Map<String,Object>>());
ret.add(top);
}
}
for(ProductCategory productCategory : productCategorieList){
if(productCategory.getParentId() != null){
for(Map<String, Object> map : ret){
if(productCategory.getParentId().longValue() == Long.valueOf(map.get("id")+"")){
List children = (List)map.get("children");
Map<String, Object> child = new HashMap<String, Object>();
child.put("id", productCategory.getId());
child.put("text", productCategory.getName());
children.add(child);
}
}
}
}
return ret;
}
}
@Autowired
private ProductService productService;
@Autowired
private FavoriteService favoriteService;
/**
* 收藏列表页面
* @param model
* @return
*/
@RequestMapping(value = "/list",method = RequestMethod.GET)
public ModelAndView index(ModelAndView model,Integer page,HttpServletRequest request){
model.addObject("productCategoryList", MenuUtil.getTreeCategory(productCategoryService.findList(new HashMap<String, Object>())));
model.addObject("allCategoryId","shop_hd_menu_all_category");
Account onlineAccount = (Account)request.getSession().getAttribute("account");
Map<String, Object> queryMap = new HashMap<String, Object>();
if(page == null || page.intValue() <= 0){
page = 1;
}
queryMap.put("offset", (page -1) * 16);
queryMap.put("pageSize", 16);
queryMap.put("userId", onlineAccount.getId());
queryMap.put("orderBy", "createTime");
queryMap.put("sort", "desc");
model.addObject("favoriteList", favoriteService.findList(queryMap));
model.addObject("currentUser", "current_");
model.addObject("page", page);
model.setViewName("home/favorite/list");
return model;
}
/**
* 添加收藏
* @param account
* @return
*/
@RequestMapping(value = "/add",method = RequestMethod.POST)
@ResponseBody
public Map<String, String> add(Favorite favorite,HttpServletRequest request){
Map<String, String> ret = new HashMap<String, String>();
Account onlineAccount = (Account)request.getSession().getAttribute("account");
ret.put("type", "error");
/**
* 角色role控制器
*
*/
@RequestMapping("/admin/role")
@Controller
public class RoleController {
@Autowired
private RoleService roleService;
@Autowired
private AuthorityService authorityService;
@Autowired
private MenuService menuService;
/**
* 角色列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
// 商户订单号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
// 支付宝交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
// 付款金额
String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
System.out.println("商户订单号=" + out_trade_no);
System.out.println("支付宝交易号=" + trade_no);
System.out.println("付款金额=" + total_amount);
// 更改订单状态
Map<Integer, Order> allOrderMap = (Map<Integer, Order>) request.getSession().getAttribute("allOrderMap");
for(Map.Entry<Integer, Order> mapEntry : allOrderMap.entrySet()) {
mapEntry.getValue().setStatus(Order.ORDER_STATUS_WAITING);
orderService.editStateAfterPay(mapEntry.getValue());
}
response.sendRedirect("../order/order_success");//跳转付款成功页面
} else {
//付款失败
response.sendRedirect("../system/operation_error");//跳转付款失败页面
}
}
}
/**
* 支付页面
* @param req
* @param httpResponse
* @throws IOException
*/
@GetMapping("/alipay/pay")
public void alipay(HttpServletRequest req, HttpServletResponse httpResponse) throws IOException {
double totalMoney = (double) req.getSession().getAttribute("totalMoney");
Account onlineAccount = (Account)req.getSession().getAttribute("account");
Random r = new Random();
//实例化客户端,填入所需参数
AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//在公共参数中设置回跳和通知地址
request.setReturnUrl(RETURN_URL);
//request.setNotifyUrl(NOTIFY_URL);
//商户订单号,商户网站订单系统中唯一订单号,必填
//生成随机Id
String out_trade_no = String.valueOf(UUID.randomUUID());
//付款金额,必填
String total_amount = String.valueOf(totalMoney);
//订单名称,必填
String subject ="水果蔬菜商城订单支付";
//商品描述,可空
String body = "尊敬的用户:"+onlineAccount.getName()+",欢迎您在水果蔬菜商城下单!";
request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"timeout_express\":\""+"5m"+"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
String form = "";
try {
form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
httpResponse.setContentType("text/html;charset=" + CHARSET);
httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
}
private AddressService addressService;
/**
* 用户中心页面
* @param model
* @return
*/
@RequestMapping(value = "/info",method = RequestMethod.GET)
public ModelAndView info(ModelAndView model,HttpServletRequest request){
model.addObject("productCategoryList", MenuUtil.getTreeCategory(productCategoryService.findList(new HashMap<String, Object>())));
model.addObject("allCategoryId","shop_hd_menu_all_category");
Account onlineAccount = (Account)request.getSession().getAttribute("account");
model.addObject("user", onlineAccount);
model.addObject("currentUser", "current_");
model.setViewName("home/user/info");
return model;
}
/**
* 修改密码页面
* @param model
* @return
*/
@RequestMapping(value = "/update_pwd",method = RequestMethod.GET)
public ModelAndView updatePwd(ModelAndView model){
model.addObject("productCategoryList", MenuUtil.getTreeCategory(productCategoryService.findList(new HashMap<String, Object>())));
model.addObject("allCategoryId","shop_hd_menu_all_category");
model.addObject("currentUser", "current_");
model.setViewName("home/user/update_pwd");
return model;
}
/**
* 修改密码提交
* @param password
* @param newPassword
* @param request
/**
* 菜单管理控制器
*
*/
@RequestMapping("/admin/menu")
@Controller
public class MenuController {
@Autowired
private MenuService menuService;
/**
* 菜单管理列表页
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.addObject("topList", menuService.findTopList());
model.setViewName("menu/list");
return model;
}
/**
* 获取菜单列表
* @param page
* @param name
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getMenuList(Page page,
@RequestParam(name="name",required=false,defaultValue="") String name
){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 500);
queryMap.put("name", name);
List<Menu> findList = menuService.findList(queryMap);
ret.put("rows", findList);
public Map<String, Object> getIconList(HttpServletRequest request){
Map<String, Object> ret = new HashMap<String, Object>();
String realPath = request.getServletContext().getRealPath("/");
File file = new File(realPath + "\\resources\\admin\\easyui\\css\\icons");
List<String> icons = new ArrayList<String>();
if(!file.exists()){
ret.put("type", "error");
ret.put("msg", "文件目录不存在!");
return ret;
}
File[] listFiles = file.listFiles();
for(File f:listFiles){
if(f!= null && f.getName().contains("png")){
icons.add("icon-" + f.getName().substring(0, f.getName().indexOf(".")).replace("_", "-"));
}
}
ret.put("type", "success");
ret.put("content", icons);
return ret;
}
/**
* 菜单添加
* @param menu
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(Menu menu){
Map<String, String> ret = new HashMap<String, String>();
if(menu == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的菜单信息!");
return ret;
}
if(StringUtils.isEmpty(menu.getName())){
ret.put("type", "error");
ret.put("msg", "请填写菜单名称!");
return ret;
}
if(StringUtils.isEmpty(menu.getIcon())){
ret.put("type", "error");
ret.put("msg", "请填写菜单图标类!");
return ret;
}
if(menu.getParentId() == null){
menu.setParentId(0l);
}
if(menuService.add(menu) <= 0){
ret.put("type", "error");
ret.put("msg", "添加失败,请联系管理员!");
@Autowired
private AuthorityService authorityService;
@Autowired
private MenuService menuService;
/**
* 角色列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.setViewName("/role/list");
return model;
}
/**
* 获取角色列表
* @param page
* @param name
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getList(Page page,
@RequestParam(name="name",required=false,defaultValue="") String name
){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", name);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", roleService.findList(queryMap));
ret.put("total", roleService.getTotal(queryMap));
return ret;
}
/**
* 角色添加
* @param role
* @return
@RequestMapping(value = "/list",method = RequestMethod.GET)
public ModelAndView purse(ModelAndView model,HttpServletRequest request){
Account onlineAccount = (Account)request.getSession().getAttribute("account");
model.addObject("user", onlineAccount);
/*List<Purse> purseList = purseService.findById(onlineAccount.getId());
if(purseList.size()>0) {
Purse purse = purseList.get(0);
model.addObject("purse", purse);
}*/
model.addObject("productCategoryList", MenuUtil.getTreeCategory(productCategoryService.findList(new HashMap<String, Object>())));
model.addObject("allCategoryId","shop_hd_menu_all_category");
model.addObject("currentUser", "current_");
model.setViewName("home/purse/purse");
return model;
}
/**
* 确认充值金额
* @return
*/
@ResponseBody
@RequestMapping(value="/recharge",method=RequestMethod.POST)
public Map<String,String> recharge(@RequestParam("balance")double balance,HttpServletRequest request){
HashMap<String, String> ret = new HashMap<String,String>();
ret.put("type", "error");
Purse purse = new Purse();
if(balance<=0) {
ret.put("msg", "金额为正整数");
return ret;
}
Account onlineAccount = (Account)request.getSession().getAttribute("account");
purse.setUserId(onlineAccount.getId().intValue());
purse.setRecharge(balance);
purse.setState(0);
if(purseService.add(purse)<=0) {
ret.put("msg", "充值失败请联系管理员");
return ret;
}
ret.put("type", "success");
ret.put("msg", "充值成功,等待管理员审核");
return ret;
}
/**
* 充值记录页面
* @param model
* @param request
* @return
*/
@Autowired
private RoleService roleService;
/**
* 用户列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
Map<String, Object> queryMap = new HashMap<String, Object>();
model.addObject("roleList", roleService.findList(queryMap));
model.setViewName("user/list");
return model;
}
/**
* 获取用户列表
* @param page
* @param username
* @param roleId
* @param sex
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getList(Page page,
@RequestParam(name="username",required=false,defaultValue="") String username,
@RequestParam(name="roleId",required=false) Long roleId,
@RequestParam(name="sex",required=false) Integer sex,
HttpServletRequest request
){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
Role role = (Role) request.getSession().getAttribute("role");
User user = (User) request.getSession().getAttribute("admin");
if(role.getId().equals(Long.valueOf("2"))) {
//如果是卖家角色, 只能看到自己的个人信息
queryMap.put("Id", user.getId());
}
queryMap.put("username", username);
queryMap.put("roleId", roleId);
queryMap.put("sex", sex);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", userService.findList(queryMap));
ret.put("total", userService.getTotal(queryMap));
return ret;
}
/**
* 添加用户
if(existAddress == null){
ret.put("msg", "不存在该地址!");
return ret;
}
if(StringUtils.isEmpty(address.getName())){
ret.put("msg", "请填写收货人!");
return ret;
}
if(StringUtils.isEmpty(address.getAddress())){
ret.put("msg", "请填写收货地址!");
return ret;
}
if(StringUtils.isEmpty(address.getPhone())){
ret.put("msg", "请填写手机号!");
return ret;
}
address.setUserId(onlineAccount.getId());
if(addressService.edit(address) <= 0){
ret.put("msg", "编辑失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
return ret;
}
/**
* 删除收货地址
* @param favoriteId
* @return
*/
@RequestMapping(value = "/delete",method = RequestMethod.POST)
@ResponseBody
public Map<String, String> delete(Long id){
Map<String, String> ret = new HashMap<String, String>();
ret.put("type", "error");
if(id == null){
ret.put("msg", "请选择要删除的地址");
return ret;
}
if(addressService.delete(id) <= 0){
ret.put("msg", "删除出错,请联系管理员!");
return ret;
}
ret.put("type", "success");
return ret;
}
* @return
*/
private List<Map<String, Object>> getTreeCategory(List<ProductCategory> productCategorieList){
List<Map<String, Object>> ret = new ArrayList<Map<String,Object>>();
//所有的父分类整理
for(ProductCategory productCategory : productCategorieList){
if(productCategory.getParentId() == null){
Map<String, Object> top = new HashMap<String, Object>();
top.put("id", productCategory.getId());
top.put("text", productCategory.getName());
top.put("children", new ArrayList<Map<String,Object>>());
ret.add(top);
}
}
for(ProductCategory productCategory : productCategorieList){
if(productCategory.getParentId() != null){
for(Map<String, Object> map : ret){
if(productCategory.getParentId().longValue() == Long.valueOf(map.get("id")+"")){
List children = (List)map.get("children");
Map<String, Object> child = new HashMap<String, Object>();
child.put("id", productCategory.getId());
child.put("text", productCategory.getName());
children.add(child);
}
}
}
}
return ret;
}
}
queryMap.put("roleId", roleId);
queryMap.put("sex", sex);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", userService.findList(queryMap));
ret.put("total", userService.getTotal(queryMap));
return ret;
}
/**
* 添加用户
* @param user
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(User user){
Map<String, String> ret = new HashMap<String, String>();
if(user == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的用户信息!");
return ret;
}
if(StringUtils.isEmpty(user.getUsername())){
ret.put("type", "error");
ret.put("msg", "请填写用户名!");
return ret;
}
if(StringUtils.isEmpty(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "请填写密码!");
return ret;
}
if(user.getRoleId() == null){
ret.put("type", "error");
ret.put("msg", "请选择所属角色!");
return ret;
}
if(isExist(user.getUsername(), 0l)){
ret.put("type", "error");
ret.put("msg", "该用户名已经存在,请重新输入!");
return ret;
}
if(userService.add(user) <= 0){
ret.put("type", "error");
ret.put("msg", "用户添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "角色添加成功!");
/**
* 角色role控制器
*
*/
@RequestMapping("/admin/role")
@Controller
public class RoleController {
@Autowired
private RoleService roleService;
@Autowired
private AuthorityService authorityService;
@Autowired
private MenuService menuService;
/**
* 角色列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.setViewName("/role/list");
return model;
}
/**
}
}
ret.put("type", "success");
ret.put("content", icons);
return ret;
}
/**
* 菜单添加
* @param menu
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(Menu menu){
Map<String, String> ret = new HashMap<String, String>();
if(menu == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的菜单信息!");
return ret;
}
if(StringUtils.isEmpty(menu.getName())){
ret.put("type", "error");
ret.put("msg", "请填写菜单名称!");
return ret;
}
if(StringUtils.isEmpty(menu.getIcon())){
ret.put("type", "error");
ret.put("msg", "请填写菜单图标类!");
return ret;
}
if(menu.getParentId() == null){
menu.setParentId(0l);
}
if(menuService.add(menu) <= 0){
ret.put("type", "error");
ret.put("msg", "添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "添加成功!");
return ret;
}
ProductCategory productCategory = productCategoryService.findById(product.getProductCategoryId());
product.setTags(productCategory.getTags() + "," + productCategory.getId());
product.setCreateTime(new Date());
if(productService.add(product) <= 0){
ret.put("type", "error");
ret.put("msg", "添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "添加成功!");
return ret;
}
/**
* 编辑商品
* @param product
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> edit(Product product, HttpServletRequest request){
Map<String, Object> ret = new HashMap<String, Object>();
if(product == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的商品信息");
return ret;
}
if(StringUtils.isEmpty(product.getName())){
ret.put("type", "error");
ret.put("msg", "请填写商品名称");
return ret;
}
if(product.getProductCategoryId() == null){
ret.put("type", "error");
ret.put("msg", "请选择所属分类!");
return ret;
}
if(product.getPrice() == null){
ret.put("type", "error");
ret.put("msg", "请填写商品价格!");
return ret;
}
if(StringUtils.isEmpty(product.getImageUrl())){
ret.put("type", "error");
ret.put("msg", "请上传商品主图");
return ret;
}
Role role = (Role) request.getSession().getAttribute("role");
User user = (User) request.getSession().getAttribute("admin");
if(role.getId().equals(Long.valueOf("2"))) {
/**
* 前台首页控制器
*
*/
@RequestMapping("/home")
@Controller
public class IndexController {
@Autowired
private AccountService accountService;
@Autowired
private ProductCategoryService productCategoryService;
@Autowired
private ProductService productService;
/**
* 前台首页页面
* @param model
* @return
*/
@RequestMapping(value = "/index",method = RequestMethod.GET)
public ModelAndView index(ModelAndView model){
model.addObject("productCategoryList", MenuUtil.getTreeCategory(productCategoryService.findList(new HashMap<String, Object>())));
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 5);
queryMap.put("orderBy", "createTime");
queryMap.put("sort", "desc");
queryMap.put("state", 2);
model.addObject("lastProductList", productService.findList(queryMap));
queryMap.put("orderBy", "sellNum");
model.addObject("sellProductList", productService.findList(queryMap));
model.addObject("allCategoryClass","shop_hd_menu_hover");
model.addObject("currentHome", "current_");
model.setViewName("home/index/index");
return model;
}
/**
* 用户登录页面
* @param model
* @return
*/
@RequestMapping(value = "/login",method = RequestMethod.GET)
public ModelAndView login(ModelAndView model){
model.addObject("title", "用户登录");