作者主页:夜未央5788
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
本系统分为前后台,主要实现的功能有:
前台:(1)二手物品信息查看、搜索。
(2)学生注册登录、个人信息修改。
(3)二手物品信息发布、编辑。
(4)二手物品评论、回复、举报。
(5)求购信息发布。
(6)求购信息查看。
后台:(1)管理员登录。
(2)系统管理:菜单管理、角色用户管理、权限管理、日志管理、数据库备份。
(3)业务管理:二手物品管理、求购物品管理、学生信息管理、评论管理、举报管理。
(4)站点管理:友情链接管理、站点基本信息(站点名称、logo、版权等)设置
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7版本;
1. 后端:Spring SpringMVC MyBatis
2. 前端:JSP+bootstrap+jQuery
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
3. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行;
4. 运行项目,输入localhost:8080/ 登录
后台访问:登录普通用户后,通过右上角更多按钮下的“登录后台”按钮进入;
package com.ldu.controller;
import com.ldu.pojo.Admin;
import com.ldu.pojo.Goods;
import com.ldu.pojo.Orders;
import com.ldu.pojo.Purse;
import com.ldu.pojo.User;
import com.ldu.service.AdminService;
import com.ldu.service.GoodsService;
import com.ldu.service.OrdersService;
import com.ldu.service.PurseService;
import com.ldu.service.UserService;
import com.ldu.util.GoodsGrid;
import com.ldu.util.OrdersGrid;
import com.ldu.util.PurseGrid;
import com.ldu.util.UserGrid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* 2021-3-7 10:40:38
*/
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
@Resource
private UserService userService;
@Resource
private GoodsService goodsService;
@Resource
private OrdersService ordersService;
@Resource
private PurseService purseService;
@Resource
private AdminService adminService;
@RequestMapping(value = "", method = RequestMethod.GET)
public String login(HttpSession session) {
// 清除session
//session.invalidate();
return "/admin/login";
}
@RequestMapping(value = "/index", method = RequestMethod.POST)
public String index(HttpServletRequest request, Admin admins) {
Admin myadmin = adminService.findAdmin(admins.getPhone(), admins.getPassword());
if (myadmin != null) {
request.getSession().setAttribute("admin", myadmin);
return "/admin/index";
}
return "/admin/login";
}
@RequestMapping(value = "/indexs")
public String indexs(HttpServletRequest request) {
Admin admin = (Admin) request.getSession().getAttribute("admin");
if (admin != null) {
Integer id = admin.getId();
Admin myadmin = adminService.findAdminById(id);
request.getSession().setAttribute("admin", myadmin);
return "/admin/index";
}
return "/admin/login";
}
/* 个人信息 */
@RequestMapping(value = "/info")
@ResponseBody
public ModelAndView getInfo(HttpServletRequest request) {
Admin admin = (Admin) request.getSession().getAttribute("admin");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("admin", admin);
modelAndView.setViewName("admin/info");
return modelAndView;
}
/* 个人密码 */
@RequestMapping(value = "/modify")
@ResponseBody
public ModelAndView getModify(HttpServletRequest request) {
Admin admin = (Admin) request.getSession().getAttribute("admin");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("admin", admin);
modelAndView.setViewName("admin/modify");
return modelAndView;
}
/* 修改密码 */
@RequestMapping(value = "/changePassword")
@ResponseBody
public ModelAndView changePassword(HttpServletRequest request,Admin admin) {
String pwd=request.getParameter("password1");
ModelAndView modelAndView = new ModelAndView();
Admin admins = (Admin) request.getSession().getAttribute("admin");
if(admin.getPassword().equals(admins.getPassword())) {
admins.setPassword(pwd);
adminService.updateAdmin(admins);
}else {
modelAndView.addObject("msg", "原密码有误,请重新输入!");
modelAndView.setViewName("admin/modify");
return modelAndView;
}
modelAndView.setViewName("admin/login");
return modelAndView;
}
/*********************************************************
* 用户管理 1.查找所有用户 2.查看用户 3.修改用户 4.删除用户 5.查询用户
*
**********************************************************/
/* 查找所有用户 */
@RequestMapping(value = "/userList")
@ResponseBody
public ModelAndView getUserList(@RequestParam("pageNum") int pageNum) {
ModelAndView modelAndView = new ModelAndView();
int pageSize = 10;
int total = userService.getUserNum();
List rows = userService.getPageUser(pageNum, pageSize);
UserGrid userGrid = new UserGrid();
userGrid.setCurrent(pageNum);
userGrid.setRowCount(pageSize);
userGrid.setRows(rows);
userGrid.setTotal(total);
modelAndView.addObject("userGrid", userGrid);
modelAndView.setViewName("admin/user/user_list");
return modelAndView;
}
/* 查看用户 */
@RequestMapping(value = "/getUser")
@ResponseBody
public User getUser(HttpServletRequest request) {
String id = request.getParameter("id");
User user = userService.getUserById(Integer.parseInt(id));
return user;
}
/* 修改用户 */
@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
@ResponseBody
public String updateUser(HttpServletRequest request, User user) {
User users = userService.selectByPrimaryKey(user.getId());
user.setPassword(users.getPassword());
try {
userService.updateUserName(user);
} catch (Exception e) {
return "{\"success\":false,\"msg\":\"保存失败!\"}";
}
return "{\"success\":true,\"msg\":\"保存成功!\"}";
}
/* 删除用户 */
@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
@ResponseBody
public String deleteUser(HttpServletRequest request, @RequestParam(value = "ids[]") String[] ids) {
try {
for (int i = 0; i < ids.length; i++) {
userService.deleteUserById(ids[i]);
}
} catch (Exception e) {
return "{\"success\":false,\"msg\":\"删除失败!\"}";
}
return "{\"success\":true,\"msg\":\"删除成功!\"}";
}
/* 查询用户 */
@RequestMapping(value = "/searchUser", method = RequestMethod.POST)
@ResponseBody
public ModelAndView searchUser(HttpServletRequest request, User user) {
ModelAndView mv = new ModelAndView();
int pageNum = 1;
int pageSize = 10;
int total = userService.getUserNum();
String phone = user.getPhone();
String username = user.getUsername();
String qq = user.getQq();
List rows = userService.getPageUserByUser(phone, username, qq, pageNum, pageSize);
UserGrid userGrid = new UserGrid();
User searchuser = new User();
searchuser.setPhone(phone);
searchuser.setUsername(username);
searchuser.setQq(qq);
userGrid.setCurrent(pageNum);
userGrid.setRowCount(pageSize);
userGrid.setRows(rows);
userGrid.setTotal(total);
mv.addObject("userGrid", userGrid);
mv.addObject("searchuser", searchuser);
mv.setViewName("admin/user/user_list");
return mv;
}
/*********************************************************
* 商品管理 1.查找所有商品 2.查看商品 3.修改商品 4.删除商品 5.查询商品 lyq
*
**********************************************************/
/* 查找所有商品 */
@RequestMapping(value = "/goodsList")
@ResponseBody
public ModelAndView getGoodsList(@RequestParam("pageNum") int pageNum) {
ModelAndView modelAndView = new ModelAndView();
int pageSize = 10;
int total = goodsService.getGoodsNum();
List rows = goodsService.getPageGoods(pageNum, pageSize);
GoodsGrid goodsGrid = new GoodsGrid();
goodsGrid.setCurrent(pageNum);
goodsGrid.setRowCount(pageSize);
goodsGrid.setRows(rows);
goodsGrid.setTotal(total);
modelAndView.addObject("goodsGrid", goodsGrid);
modelAndView.setViewName("admin/goods/goods_list");
return modelAndView;
}
/* 查看商品 */
@RequestMapping(value = "/getGoods")
@ResponseBody
public Goods getGoods(HttpServletRequest request) {
String id = request.getParameter("id");
Goods goods = goodsService.getGoodsById(Integer.parseInt(id));
return goods;
}
/* 修改商品 */
@RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
@ResponseBody
public String updateGoods(HttpServletRequest request, Goods goods) {
int id = goods.getId();
Goods oldGoods = goodsService.getGoodsById(id);
goods.setUserId(oldGoods.getUserId());
goods.setPolishTime(oldGoods.getPolishTime());
goods.setEndTime(oldGoods.getEndTime());
try {
goodsService.updateGoodsByPrimaryKeyWithBLOBs(goods.getId(), goods);
} catch (Exception e) {
return "{\"success\":false,\"msg\":\"保存失败!\"}";
}
return "{\"success\":true,\"msg\":\"保存成功!\"}";
}
/* 删除商品 */
@RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
@ResponseBody
public String deleteGoods(HttpServletRequest request, @RequestParam(value = "ids[]") String[] ids) {
try {
for (int i = 0; i < ids.length; i++) {
goodsService.deleteGoodsByPrimaryKeys(Integer.parseInt(ids[i]));
}
} catch (Exception e) {
return "{\"success\":false,\"msg\":\"删除失败!\"}";
}
return "{\"success\":true,\"msg\":\"删除成功!\"}";
}
/* 查询商品 */
@RequestMapping(value = "/searchGoods", method = RequestMethod.POST)
public ModelAndView searchGoods(HttpServletRequest request, Goods goods) {
ModelAndView mv = new ModelAndView();
int pageNum = 1;
int pageSize = 10;
int total = goodsService.getGoodsNum();
Integer id = goods.getId();
String name = goods.getName();
Integer status = goods.getStatus();
List rows = goodsService.getPageGoodsByGoods(id, name, status, pageNum, pageSize);
GoodsGrid goodsGrid = new GoodsGrid();
Goods searchgoods = new Goods();
searchgoods.setId(id);
searchgoods.setName(name);
searchgoods.setStatus(status);
goodsGrid.setCurrent(pageNum);
goodsGrid.setRowCount(pageSize);
goodsGrid.setRows(rows);
goodsGrid.setTotal(total);
mv.addObject("goodsGrid", goodsGrid);
mv.addObject("searchgoods", searchgoods);
mv.setViewName("admin/goods/goods_list");
return mv;
}
/*********************************************************
* 订单管理 1.查找所有订单 2.查看订单 3.修改订单 4.删除订单 5.查询订单 lyq
*
**********************************************************/
/* 查找所有订单 */
@RequestMapping(value = "/ordersList")
@ResponseBody
public ModelAndView getOrdersList(@RequestParam("pageNum") int pageNum) {
ModelAndView modelAndView = new ModelAndView();
int pageSize = 10;
int total = ordersService.getOrdersNum();
List rows = ordersService.getPageOrders(pageNum, pageSize);
OrdersGrid ordersGrid = new OrdersGrid();
ordersGrid.setCurrent(pageNum);
ordersGrid.setRowCount(pageSize);
ordersGrid.setRows(rows);
ordersGrid.setTotal(total);
modelAndView.addObject("ordersGrid", ordersGrid);
modelAndView.setViewName("admin/orders/orders_list");
return modelAndView;
}
/* 查看订单 */
@RequestMapping(value = "/getOrders")
@ResponseBody
public Orders getOrders(HttpServletRequest request) {
String id = request.getParameter("id");
Orders orders = ordersService.getOrdersById(Integer.parseInt(id));
return orders;
}
/* 修改订单 */
@RequestMapping(value = "/updateOrders", method = RequestMethod.POST)
@ResponseBody
public String updateOrders(HttpServletRequest request, Orders orders) {
int id = orders.getId();
Orders oldorders = ordersService.getOrdersById(id);
orders.setGoodsId(oldorders.getGoodsId());
orders.setUserId(oldorders.getUserId());
Goods goods=goodsService.getGoodsById(oldorders.getGoods().getId());
if (oldorders.getOrderState() != orders.getOrderState()) {
Float balance = orders.getOrderPrice();
if (orders.getOrderState() == 3) {
purseService.updatePurseByuserId(goods.getUserId(), balance);
} else {
purseService.updatePurseOfdel(goods.getUserId(), balance);
}
}
try {
ordersService.updateByPrimaryKey(id, orders);
} catch (Exception e) {
return "{\"success\":false,\"msg\":\"保存失败!\"}";
}
return "{\"success\":true,\"msg\":\"保存成功!\"}";
}
/* 删除订单 */
@RequestMapping(value = "/deleteOrders", method = RequestMethod.POST)
@ResponseBody
public String deleteOrders(HttpServletRequest request, @RequestParam(value = "ids[]") String[] ids) {
try {
for (int i = 0; i < ids.length; i++) {
ordersService.deleteOrdersByPrimaryKeys(Integer.parseInt(ids[i]));
}
} catch (Exception e) {
return "{\"success\":false,\"msg\":\"删除失败!\"}";
}
return "{\"success\":true,\"msg\":\"删除成功!\"}";
}
/* 查询订单 */
@RequestMapping(value = "/searchOrders", method = RequestMethod.POST)
@ResponseBody
public ModelAndView searchOrders(HttpServletRequest request, Orders orders) {
ModelAndView mv = new ModelAndView();
int pageNum = 1;
int pageSize = 10;
int total = ordersService.getOrdersNum();
Long orderNum = orders.getOrderNum();
String orderInformation = orders.getOrderInformation();
Integer orderState = orders.getOrderState();
List rows = ordersService.getPageOrdersByOrders(orderNum, orderInformation, orderState, pageNum, pageSize);
OrdersGrid ordersGrid = new OrdersGrid();
Orders searchorders = new Orders();
searchorders.setOrderNum(orderNum);
searchorders.setOrderInformation(orderInformation);
searchorders.setOrderState(orderState);
ordersGrid.setCurrent(pageNum);
ordersGrid.setRowCount(pageSize);
ordersGrid.setRows(rows);
ordersGrid.setTotal(total);
mv.addObject("ordersGrid", ordersGrid);
mv.addObject("searchorders", searchorders);
mv.setViewName("admin/orders/orders_list");
return mv;
}
/*********************************************************
* 钱包管理 1.查找所有钱包 2.查看钱包 3.修改钱包 4.删除钱包 5.查询钱包 lyq
*
**********************************************************/
/* 查找所有钱包 */
@RequestMapping(value = "/purseList")
@ResponseBody
public ModelAndView getPurseList(HttpServletRequest request) {
int pageNum=Integer.parseInt(request.getParameter("pageNum"));
ModelAndView modelAndView = new ModelAndView();
int pageSize = 10;
int total = purseService.getPurseNum();
List rows = purseService.getPagePurse(pageNum, pageSize);
PurseGrid purseGrid = new PurseGrid();
purseGrid.setCurrent(pageNum);
purseGrid.setRowCount(pageSize);
purseGrid.setRows(rows);
purseGrid.setTotal(total);
modelAndView.addObject("purseGrid", purseGrid);
modelAndView.setViewName("admin/purse/purse_list");
return modelAndView;
}
/* 查询钱包 */
@RequestMapping(value = "/searchPurse", method = RequestMethod.POST)
@ResponseBody
public ModelAndView searchPurse(HttpServletRequest request, Purse purse) {
ModelAndView mv = new ModelAndView();
int pageNum = 1;
int pageSize = 10;
int total = purseService.getPurseNum();
Integer userId = purse.getUserId();
Integer state=purse.getState();
List rows = purseService.getPagePurseByPurse(userId,state, pageNum, pageSize);
PurseGrid purseGrid = new PurseGrid();
Purse searchpurse = new Purse();
searchpurse.setUserId(userId);
searchpurse.setState(state);
purseGrid.setCurrent(pageNum);
purseGrid.setRowCount(pageSize);
purseGrid.setRows(rows);
purseGrid.setTotal(total);
mv.addObject("purseGrid", purseGrid);
mv.addObject("searchpurse", searchpurse);
mv.setViewName("admin/purse/purse_list");
return mv;
}
/* 查看订单 */
@RequestMapping(value = "/getPurse")
@ResponseBody
public Purse getPurse(HttpServletRequest request) {
String id = request.getParameter("id");
Purse purse = purseService.getPurseById(Integer.parseInt(id));
return purse;
}
/* 修改钱包 */
// @RequestMapping(value = "/updatePurse", method = RequestMethod.POST)
// @ResponseBody
// public String updatePurse(HttpServletRequest request, Purse purse) {
// int id = purse.getId();
// try {
// purseService.updateByPrimaryKey(id, purse);
// } catch (Exception e) {
// return "{\"success\":false,\"msg\":\"保存失败!\"}";
// }
// return "{\"success\":true,\"msg\":\"保存成功!\"}";
// }
/* 通过 */
@RequestMapping(value = "/updatePursePass", method = RequestMethod.POST)
@ResponseBody
public String updatePursePass(HttpServletRequest request, Purse purse) {
Float balance=purse.getBalance();
purse.setState(2);
try {
if(purse.getRecharge()!=null){//充值 充值金额=null 当前金额=当前金额+充值金额
Float recharge=purse.getRecharge();
Float balanceRecharge=balance+recharge;
purse.setBalance(balanceRecharge);
purseService.updatePursePassById(purse.getId(),purse);
}if(purse.getWithdrawals()!=null) {//提现
Float withdrawals=purse.getWithdrawals();
Float balanceWithdrawals=balance-withdrawals;
purse.setBalance(balanceWithdrawals);
purseService.updatePurseRefuseById(purse.getId(),purse);
}
}
catch (Exception e) {
return "{\"success\":true,\"msg\":\"审核失败,请核对金额!\"}";
}
return "{\"success\":true,\"msg\":\"审核成功!\"}";
}
/* 不通过 */
@RequestMapping(value = "/updatePurseRefuse", method = RequestMethod.POST)
@ResponseBody
public String updatePurseRefuse(HttpServletRequest request, Purse purse) {
purse.setState(1);
try {
purseService.updatePurseRefuseById(purse.getId(),purse);
}
catch (Exception e) {
return "{\"success\":true,\"msg\":\"审核失败!\"}";
}
return "{\"success\":true,\"msg\":\"审核成功!\"}";
}
/* 用户查看审核结果 */
@RequestMapping(value = "/updatePurseState", method = RequestMethod.GET)
public void updatePurseState(HttpServletRequest request) {
Integer id=Integer.parseInt(request.getParameter("id"));
Purse purse= purseService.getPurseById(id);
purse.setState(null);
this.purseService.updateByPrimaryKey(id,purse);//修改state为null
}
}
package com.ldu.controller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.ldu.pojo.Catelog;
import com.ldu.pojo.CommentExtend;
import com.ldu.pojo.Comments;
import com.ldu.pojo.Goods;
import com.ldu.pojo.GoodsExtend;
import com.ldu.pojo.Image;
import com.ldu.pojo.Purse;
import com.ldu.pojo.User;
import com.ldu.service.CatelogService;
import com.ldu.service.GoodsService;
import com.ldu.service.ImageService;
import com.ldu.service.PurseService;
import com.ldu.service.UserService;
import com.ldu.util.DateUtil;
@Controller
@RequestMapping(value = "/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Autowired
private ImageService imageService;
@Autowired
private CatelogService catelogService;
@Autowired
private UserService userService;
@Resource
private PurseService purseService;
/**
* 首页显示商品,每一类商品查询6件,根据最新上架排序 key的命名为catelogGoods1、catelogGoods2....
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/homeGoods")
public ModelAndView homeGoods() throws Exception {
ModelAndView modelAndView = new ModelAndView();
// 商品种类数量
int catelogSize = 7;
// 每个种类显示商品数量
int goodsSize = 6;
List goodsList = null;
List goodsAndImage = null;
/* 获取最新发布列表 */
goodsList = goodsService.getGoodsOrderByDate(goodsSize);
goodsAndImage = new ArrayList();
for (int j = 0; j < goodsList.size(); j++) {
// 将用户信息和image信息封装到GoodsExtend类中,传给前台
GoodsExtend goodsExtend = new GoodsExtend();
Goods goods = goodsList.get(j);
List images = imageService.getImagesByGoodsPrimaryKey(goods.getId());
goodsExtend.setGoods(goods);
goodsExtend.setImages(images);
goodsAndImage.add(j, goodsExtend);
}
String key0 = "catelog" + "Goods";
modelAndView.addObject(key0, goodsAndImage);
/* 获取其他列表物品信息 */
for (int i = 1; i <= catelogSize; i++) {
goodsList = goodsService.getGoodsByCatelogOrderByDate(i, goodsSize);
goodsAndImage = new ArrayList();
for (int j = 0; j < goodsList.size(); j++) {
// 将用户信息和image信息封装到GoodsExtend类中,传给前台
GoodsExtend goodsExtend = new GoodsExtend();
Goods goods = goodsList.get(j);
List images = imageService.getImagesByGoodsPrimaryKey(goods.getId());
goodsExtend.setGoods(goods);
goodsExtend.setImages(images);
goodsAndImage.add(j, goodsExtend);
}
String key = "catelog" + "Goods" + i;
modelAndView.addObject(key, goodsAndImage);
}
modelAndView.setViewName("goods/homeGoods");
return modelAndView;
}
/**
* 搜索商品
*
* @param str //ajax传值
* @return
* @throws Exception
*/
@RequestMapping(value = "/search")
public ModelAndView searchGoods(@RequestParam(value = "str", required = false) String str) throws Exception {
List goodsList = goodsService.searchGoods(str, str);
List goodsExtendList = new ArrayList();
for (int i = 0; i < goodsList.size(); i++) {
GoodsExtend goodsExtend = new GoodsExtend();
Goods goods = goodsList.get(i);
List imageList = imageService.getImagesByGoodsPrimaryKey(goods.getId());
goodsExtend.setGoods(goods);
goodsExtend.setImages(imageList);
goodsExtendList.add(i, goodsExtend);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("goodsExtendList", goodsExtendList);
modelAndView.addObject("search", str);
modelAndView.setViewName("/goods/searchGoods");
return modelAndView;
}
/**
* 查询该类商品
*
* @param id
* 要求该参数不为空
* @return
* @throws Exception
*/
@RequestMapping(value = "/catelog")
public ModelAndView homeGoods(HttpServletRequest request, @RequestParam(value = "str", required = false) String str)
throws Exception {
ModelAndView modelAndView = new ModelAndView();
// 每个种类显示商品数量
int goodsSize = 12;
List goodsList = null;
List goodsAndImage = null;
/* 获取最新发布列表 */
goodsList = goodsService.getGoodsByStr(goodsSize, str, str);
goodsAndImage = new ArrayList();
for (int j = 0; j < goodsList.size(); j++) {
// 将用户信息和image信息封装到GoodsExtend类中,传给前台
GoodsExtend goodsExtend = new GoodsExtend();
Goods goods = goodsList.get(j);
List images = imageService.getImagesByGoodsPrimaryKey(goods.getId());
goodsExtend.setGoods(goods);
goodsExtend.setImages(images);
goodsAndImage.add(j, goodsExtend);
}
modelAndView.addObject("goodsExtendList", goodsAndImage);
modelAndView.addObject("search", str);
modelAndView.setViewName("/goods/catelogGoods");
return modelAndView;
}
/**
* 查询该类商品
*
* @param id
* 要求该参数不为空
* @return
* @throws Exception
*/
@RequestMapping(value = "/catelog/{id}")
public ModelAndView catelogGoods(HttpServletRequest request, @PathVariable("id") Integer id,
@RequestParam(value = "str", required = false) String str) throws Exception {
List goodsList = goodsService.getGoodsByCatelog(id, str, str);
Catelog catelog = catelogService.selectByPrimaryKey(id);
List goodsExtendList = new ArrayList();
for (int i = 0; i < goodsList.size(); i++) {
GoodsExtend goodsExtend = new GoodsExtend();
Goods goods = goodsList.get(i);
List imageList = imageService.getImagesByGoodsPrimaryKey(goods.getId());
goodsExtend.setGoods(goods);
goodsExtend.setImages(imageList);
goodsExtendList.add(i, goodsExtend);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("goodsExtendList", goodsExtendList);
modelAndView.addObject("catelog", catelog);
modelAndView.addObject("search", str);
modelAndView.setViewName("/goods/catelogGoods");
return modelAndView;
}
/**
* 根据商品id查询该商品详细信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value = "/goodsId/{id}")
public ModelAndView getGoodsById(HttpServletRequest request, @PathVariable("id") Integer id,
@RequestParam(value = "str", required = false) String str) throws Exception {
Goods goods = goodsService.getGoodsByPrimaryKey(id);
User seller = userService.selectByPrimaryKey(goods.getUserId());
Catelog catelog = catelogService.selectByPrimaryKey(goods.getCatelogId());
GoodsExtend goodsExtend = new GoodsExtend();
List imageList = imageService.getImagesByGoodsPrimaryKey(id);
CommentExtend CommentExtend=goodsService.selectCommentsByGoodsId(id);
goodsExtend.setGoods(goods);
goodsExtend.setImages(imageList);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("CommentExtend",CommentExtend);
modelAndView.addObject("goodsExtend", goodsExtend);
modelAndView.addObject("seller", seller);
modelAndView.addObject("search", str);
modelAndView.addObject("catelog", catelog);
modelAndView.setViewName("/goods/detailGoods");
return modelAndView;
}
/**
* 发布评论
* @return
*/
@RequestMapping(value = "/addComments",method=RequestMethod.POST)
public void deleteFocus(HttpServletRequest request,Comments comments) {
User cur_user = (User)request.getSession().getAttribute("cur_user");
comments.setUser(cur_user);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date createAt =new Date();
comments.setCreateAt(sdf.format(createAt));
goodsService.addComments(comments);
}
/**
* 修改商品信息
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/editGoods/{id}")
public ModelAndView editGoods(HttpServletRequest request,@PathVariable("id") Integer id) throws Exception {
User cur_user = (User) request.getSession().getAttribute("cur_user");
Goods goods = goodsService.getGoodsByPrimaryKey(id);
List imageList = imageService.getImagesByGoodsPrimaryKey(id);
GoodsExtend goodsExtend = new GoodsExtend();
goodsExtend.setGoods(goods);
goodsExtend.setImages(imageList);
ModelAndView modelAndView = new ModelAndView();
Integer userId = cur_user.getId();
Purse myPurse = purseService.getPurseByUserId(userId);
modelAndView.addObject("myPurse", myPurse);
// 将商品信息添加到model
modelAndView.addObject("goodsExtend", goodsExtend);
modelAndView.setViewName("/goods/editGoods");
return modelAndView;
}
/**
* 提交商品更改信息
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/editGoodsSubmit")
public String editGoodsSubmit(HttpServletRequest request, Goods goods) throws Exception {
User cur_user = (User) request.getSession().getAttribute("cur_user");
goods.setUserId(cur_user.getId());
String polish_time = DateUtil.getNowDay();
goods.setPolishTime(polish_time);
goods.setStatus(1);
goodsService.updateGoodsByPrimaryKeyWithBLOBs(goods.getId(), goods);
return "redirect:/user/allGoods";
}
/**
* 商品下架
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/offGoods")
public ModelAndView offGoods() throws Exception {
return null;
}
/**
* 用户删除商品
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/deleteGoods/{id}")
public String deleteGoods(HttpServletRequest request, @PathVariable("id") Integer id) throws Exception {
Goods goods = goodsService.getGoodsByPrimaryKey(id);
// 删除商品后,catlog的number-1,user表的goods_num-1,image删除,更新session的值
User cur_user = (User) request.getSession().getAttribute("cur_user");
goods.setUserId(cur_user.getId());
int number = cur_user.getGoodsNum();
Integer calelog_id = goods.getCatelogId();
Catelog catelog = catelogService.selectByPrimaryKey(calelog_id);
catelogService.updateCatelogNum(calelog_id, catelog.getNumber() - 1);
userService.updateGoodsNum(cur_user.getId(), number - 1);
cur_user.setGoodsNum(number - 1);
request.getSession().setAttribute("cur_user", cur_user);// 修改session值
//imageService.deleteImagesByGoodsPrimaryKey(id);
goodsService.deleteGoodsByPrimaryKey(id);
return "redirect:/user/allGoods";
}
/**
* 发布商品
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/publishGoods")
public ModelAndView publishGoods(HttpServletRequest request) {
// 可以校验用户是否登录
User cur_user = (User) request.getSession().getAttribute("cur_user");
// if (cur_user == null) {
// return "/goods/homeGoods";
// } else {
Integer userId = cur_user.getId();
Purse myPurse = purseService.getPurseByUserId(userId);
ModelAndView mv = new ModelAndView();
mv.addObject("myPurse", myPurse);
mv.setViewName("/goods/pubGoods");
return mv;
}
/**
* 提交发布的商品信息
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/publishGoodsSubmit")
public String publishGoodsSubmit(HttpServletRequest request, Image ima, Goods goods, MultipartFile image)
throws Exception {
// 查询出当前用户cur_user对象,便于使用id
User cur_user = (User) request.getSession().getAttribute("cur_user");
goods.setUserId(cur_user.getId());
goodsService.addGood(goods, 10);// 在goods表中插入物品
// 返回插入的该物品的id
int goodsId = goods.getId();
ima.setGoodsId(goodsId);
imageService.insert(ima);// 在image表中插入商品图片
// 发布商品后,catlog的number+1,user表的goods_num+1,更新session的值
int number = cur_user.getGoodsNum();
Integer calelog_id = goods.getCatelogId();
Catelog catelog = catelogService.selectByPrimaryKey(calelog_id);
catelogService.updateCatelogNum(calelog_id, catelog.getNumber() + 1);
userService.updateGoodsNum(cur_user.getId(), number + 1);
cur_user.setGoodsNum(number + 1);
request.getSession().setAttribute("cur_user", cur_user);// 修改session值
return "redirect:/user/allGoods";
}
/**
* 上传物品
*
* @param session
* @param myfile
* @return
* @throws IllegalStateException
* @throws IOException
*/
@ResponseBody
@RequestMapping(value = "/uploadFile")
public Map uploadFile(HttpSession session, MultipartFile myfile)
throws IllegalStateException, IOException {
// 原始名称
String oldFileName = myfile.getOriginalFilename(); // 获取上传文件的原名
// 存储图片的物理路径
//String file_path = session.getServletContext().getRealPath("upload");
//windows版系统参考此路径修改
String file_path = "E:\\squirrel\\WebContent\\upload";
// 苹果版系统参考此路径修改
//String file_path = "/Users/hanmeng/Desktop/java源码/SSM/008ssm+mysql+jsp实现的校园二手市场交易平台源码/源码/squirrel/WebContent/upload";
//session.getServletContext().getRealPath("upload");
System.out.println("file_path:"+file_path);
// 上传图片
if (myfile != null && oldFileName != null && oldFileName.length() > 0) {
// 新的图片名称
String newFileName = UUID.randomUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
// 新图片
File newFile = new File(file_path + "/" + newFileName);
// 将内存中的数据写入磁盘
myfile.transferTo(newFile);
// 将新图片名称返回到前端
Map map = new HashMap();
map.put("success", "成功啦");
map.put("imgUrl", newFileName);
return map;
} else {
Map map = new HashMap();
map.put("error", "图片不合法");
return map;
}
}
/**
* 根据商品id查询该商品详细信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value = "/buyId/{id}")
public ModelAndView getGoodsdetailById(HttpServletRequest request, @PathVariable("id") Integer id)
throws Exception {
Goods goods = goodsService.getGoodsByPrimaryKey(id);
GoodsExtend goodsExtend = new GoodsExtend();
List imageList = imageService.getImagesByGoodsPrimaryKey(id);
goodsExtend.setGoods(goods);
goodsExtend.setImages(imageList);
User cur_user = (User)request.getSession().getAttribute("cur_user");
Integer userId = cur_user.getId();
Purse myPurse=purseService.getPurseByUserId(userId);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("goodsExtend", goodsExtend);
modelAndView.addObject("myPurse",myPurse);
modelAndView.setViewName("/user/pay");
return modelAndView;
}
}
如果也想学习本系统,下面领取。关注并回复:008ssm