基于javaweb的校园二手市场系统(java+ssm+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的校园二手市场系统(java+SSM+mysql+maven+tomcat)
一、项目简述( +IW文档)
功能:本系统分用户前台和管理员后台。 本系统用例模型有三种,分别是游客、注册用户和系统管 理员。下面分别对这三个角色的功能进行描述: 1) 诞 游客是未注册的用户,他们可以浏览物物品,可以搜索物 品,如需购买物品,必须先注册成为网站用户。游客主要 功触吓: a.浏览物品 b.搜索物品 c.注册成为网站用户 2) 注册用户 注册用户是经过网站合法认证的用户,登录网站后可以浏 览物品、搜索物品、发布物品、关注物品、购买物品和查 看个人中心。 3) 系统管理员 系统管理员主要负责系统的后台管理工作,主要功能如 下: 用户管理,商品管理等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。
管理员控制层:
@Controller
@RequestMapping(value = “admin”)
public class AdminController {
private final UserService userService;
private final GoodService goodService;
private final TypeService typeService;
private final OrderService orderService;
@Autowired
public AdminController(UserService userService, GoodService goodService, TypeService typeService, OrderService orderService) {
this.userService = userService;
this.goodService = goodService;
this.typeService = typeService;
this.orderService = orderService;
@RequestMapping(value = “/adminLogin”, method = RequestMethod.GET)
public String getAdminLogin(){
return “admin/adminLogin”;
@RequestMapping(value = “/adminLogin”, method = RequestMethod.POST)
public String postAdminLogin(ModelMap model,
@RequestParam(value = “email”, required = false) String email,
@RequestParam(value = “password”, required = false) String password,
HttpSession session) {
User admin = userService.getUserByEmail(email);
String message;
if (admin != null){
String mdsPass = DigestUtils.md5DigestAsHex((password + admin.getCode()).getBytes());
// if (!mdsPass .equals(admin.getPassword())){
// message = “用户密码错误!”;
// }
if (!password .equals(admin.getPassword())){
message = “用户密码错误!”;
} else if (admin.getRoleId() != 101){
message = “用户没有权限访问!”;
} else {
session.setAttribute(“admin”,admin);
return “redirect:/admin/adminPage”;
} else {
message = “用户不存在!”;
model.addAttribute(“message”, message);
return “admin/adminLogin”;
@RequestMapping(value = “/adminLogout”, method = RequestMethod.GET)
public String adminLogout(@RequestParam(required = false, defaultValue = “false” )String adminLogout, HttpSession session){
if (adminLogout.equals(“true”)){
session.removeAttribute(“admin”);
// adminLogout = “false”;
return “redirect:/”;
@RequestMapping(value = “/adminPage”, method = RequestMethod.GET)
public String getAdminPage(ModelMap model,
HttpSession session){
User admin = (User) session.getAttribute(“admin”);
if (admin == null){
return “redirect:/admin/adminLogin”;
List goodList = goodService.getAllGoodList();
for (Good good : goodList) {
good.setGoodUser(userService.getUserById(good.getUserId()));
good.setGoodSecondType(typeService.getSecondTypeById(good.getSecondTypeId()));
List userList = userService.getAllUser();
List firstTypeList = typeService.getAllFirstType();
List orderList = orderService.getOrderList();
model.addAttribute(“goodList”, goodList);
model.addAttribute(“userList”, userList);
model.addAttribute(“firstTypeList”, firstTypeList);
model.addAttribute(“orderList”, orderList);
return “admin/adminPage”;
@RequestMapping(value = “/user/update/status/{statusId}&{userId}”, method = RequestMethod.GET)
public ResponseEntity updateUserStatus(@PathVariable Integer statusId,
@PathVariable Integer userId){
Boolean success = userService.updateUserStatus(statusId, userId);
if (success){
List userList = userService.getAllUser();
return ResponseEntity.ok(userList);
return ResponseEntity.ok(success);
@RequestMapping(value = “/user/delete/{userId}”, method = RequestMethod.GET)
public ResponseEntity deleteUser(@PathVariable Integer userId){
Boolean success = userService.deleteUser(userId);
if (success){
List userList = userService.getAllUser();
return ResponseEntity.ok(userList);
return ResponseEntity.ok(success);
商品控制层:
@Controller
public class GoodController {
private final GoodService goodService;
private final TypeService typeService;
private final ReviewService reviewService;
private final UserService userService;
private final ImageService imageService;
private final CollectService collectService;
private static String message = “”;
@Autowired
public GoodController(GoodService goodService, TypeService typeService,
ReviewService reviewService, UserService userService,
ImageService imageService, CollectService collectService) {
this.goodService = goodService;
this.typeService = typeService;
this.reviewService = reviewService;
this.userService = userService;
this.imageService = imageService;
this.collectService = collectService;
@RequestMapping(value = “/”, method = RequestMethod.GET)
public String getHomeGoods(
ModelMap model,
@RequestParam(required = false, defaultValue = “”) String searchText,
@RequestParam(required = false) Integer secondTypeId,
@RequestParam(required = false, defaultValue = “0”) int offset,
@RequestParam(required = false, defaultValue = “40”) int limit) {
List goods = goodService.getGoodsBySearchAndType(searchText,
secondTypeId, offset, limit);
double goodsNum = goodService.getGoodsBySearchAndTypeCount(searchText,
secondTypeId);
List firstTypes = typeService.getAllFirstType();
for (FirstType firstType : firstTypes) {
firstType.setSecondType(typeService
.getSecondTypeByFirstTypeId(firstType.getId()));
model.addAttribute(“firstTypes”, firstTypes);
model.addAttribute(“goods”, goods);
model.addAttribute(“pages”, Math.ceil(goodsNum / limit));
model.addAttribute(“goodsNum”, goodsNum);
model.addAttribute(“offset”, offset);
model.addAttribute(“limit”, limit);
return “home/homeGoods”;
@RequestMapping(value = “/goods/goodInfo”, method = RequestMethod.GET)
public String getGoodInfo(ModelMap model, HttpSession httpSession,
@RequestParam(required = false) Integer goodId) {
Good goodInfo = goodService.getGoodById(goodId);
if (goodInfo == null) {
return “goods/error”;
Integer collect = 1;
User user = (User) httpSession.getAttribute(“user”);
if (user == null) {
collect = 0;
} else {
if (collectService.getCollect(goodId, user.getId())) {
collect = 2;
List images = imageService.getImageByGoodId(goodId);
User goodUser = userService.getUserById(goodInfo.getUserId());
goodInfo.setGoodUser(userService.getUserById(goodInfo.getUserId()));
goodInfo.setGoodSecondType(typeService.getSecondTypeById(goodInfo
.getSecondTypeId()));
List reviews = reviewService.gerReviewByGoodId(goodId);
for (Review review : reviews) {
review.setReplys(reviewService.gerReplyByReviewId(review.getId()));
List goods = goodService.getRECGoods(goodInfo.getSecondTypeId(),
goodInfo.getId());
model.addAttribute(“message”, message);
model.addAttribute(“reviews”, reviews);
model.addAttribute(“goodInfo”, goodInfo);
model.addAttribute(“images”, images);
model.addAttribute(“goodUser”, goodUser);
model.addAttribute(“goods”, goods);
model.addAttribute(“collect”, collect);
message = “”;
return “goods/goodInfo”;
@RequestMapping(value = “/goods/goodInfo”, method = RequestMethod.POST)
public String putReview(
@RequestParam(value = “goodId”, required = false) Integer goodId,
@RequestParam(value = “reviewId”, required = false) Integer reviewId,
@RequestParam(value = “fromUserId”, required = false) Integer fromUserId,
@RequestParam(value = “toUserId”, required = false) Integer toUserId,
@RequestParam(value = “fromUser”, required = false) String fromUser,
@RequestParam(value = “toUser”, required = false) String toUser,
@RequestParam(value = “replyText”, required = false, defaultValue = “”) String replyText,
@RequestParam(value = “reviewText”, required = false, defaultValue = “”) String reviewText) {
if (reviewText.equals(“”)) {
if (replyText.equals(“”)) {
message = “内容不能为空!”;
return “redirect:/goods/goodInfo?goodId=” + goodId;
} else {
Reply reply = new Reply();
reply.setReviewId(reviewId);
reply.setFromUser(fromUser);
reply.setFromUserId(fromUserId);
reply.setToUser(toUser);
reply.setToUserId(toUserId);
reply.setText(replyText);
if (reviewService.insertReply(reply) == 1) {
message = “回复成功!”;
return “redirect:/goods/goodInfo?goodId=” + goodId;
} else {
message = “回复失败!”;
return “redirect:/goods/goodInfo?goodId=” + goodId;
} else {
Review review = new Review();
review.setGoodId(goodId);
review.setFromUser(fromUser);
review.setFromUserId(fromUserId);
review.setToUserId(toUserId);
review.setText(reviewText);
if (reviewService.insertReview(review) == 1) {
message = “评论成功!”;
return “redirect:/goods/goodInfo?goodId=” + goodId;
} else {
message = “评论失败!”;
return “redirect:/goods/goodInfo?goodId=” + goodId;
@RequestMapping(value = “/goods/publishGood”, method = RequestMethod.GET)
public String getPublishGood(ModelMap model, HttpSession session) {
User user = (User) session.getAttribute(“user”);
if (user == null) {
return “redirect:/”;
Good good = new Good();
List firstTypes = typeService.getAllFirstType();
List goods = goodService.getAllGoods(0, 5);
model.addAttribute(“goods”, goods);
model.addAttribute(“good”, good);
model.addAttribute(“firstTypes”, firstTypes);
return “goods/publishGood”;
@RequestMapping(value = “/goods/publishGood”, method = RequestMethod.POST)
public String getGoodId(ModelMap model, HttpSession session,
@Valid Good good) {
List firstTypes = typeService.getAllFirstType();
User user = (User) session.getAttribute(“user”);
List goods = goodService.getAllGoods(0, 5);
good.setUserId(user.getId());
good.setPhotoUrl(“/statics/image/goods/default/nophoto.png”);
if (goodService.insertGood(good) != 1) {
System.out.println(“插入物品失败!”);
model.addAttribute(“goods”, goods);
model.addAttribute(“good”, good);
model.addAttribute(“firstTypes”, firstTypes);
return “goods/publishGood”;
@RequestMapping(value = “/goods/publishGood/uploadImage”, method = RequestMethod.POST)
public String uploadImage(
HttpSession session,
@RequestParam(value = “goodId”, required = false) Integer goodId,
@RequestParam(value = “mainFile”, required = false) MultipartFile mainFile,
@RequestParam(value = “file”, required = false) MultipartFile[] file)
throws IOException {
User user = (User) session.getAttribute(“user”);
FileCheck fileCheck = new FileCheck();
RandomString randomString = new RandomString();
String filePath = “/statics/image/goods/” + user.getId() + “/” + goodId;
String pathRoot = fileCheck.checkGoodFolderExist(filePath);
String name;
if (!mainFile.isEmpty()) {
String fileName = goodId + randomString.getRandomString(10);
String contentType = mainFile.getContentType();
String imageName = contentType
.substring(contentType.indexOf(“/”) + 1);
name = fileName + “.” + imageName;
mainFile.transferTo(new File(pathRoot + name));
String photoUrl = filePath + “/” + name;
goodService.updateGoodPhotoUrl(photoUrl, goodId);
for (MultipartFile mf : file) {
if (!mf.isEmpty()) {
// 生成uuid作为文件名称
String fileName = goodId + randomString.getRandomString(10);
// 获得文件类型(可以判断如果不是图片,禁止上传)
String contentType = mf.getContentType();
// 获得文件后缀名称
String imageName = contentType.substring(contentType
.indexOf(“/”) + 1);
name = fileName + “.” + imageName;
System.out.println(“name:” + name);
mf.transferTo(new File(pathRoot + name));
Image image = new Image();
image.setGoodId(goodId);
image.setName(name);
image.setUrl(filePath + “/” + name);
imageService.insertImage(image);
} else {
System.out.println(“文件为空!”);
return “redirect:/goods/goodInfo?goodId=” + goodId;
@RequestMapping(value = “/goods/userGoods”, method = RequestMethod.GET)
public String getUserGoods(ModelMap model,
@RequestParam(value = “userId”, required = false) Integer userId) {
User user = userService.getUserById(userId);
List userGoods = goodService.getGoodStatusByUserId(userId);
List goods = goodService.getAllGoods(0, 4);
model.addAttribute(“user”, user);
model.addAttribute(“userGoods”, userGoods);
model.addAttribute(“goods”, goods);
return “goods/userGood”;
@RequestMapping(value = “/goods/userGoodEdit”, method = RequestMethod.GET)
public String getUserGoodEdit(ModelMap model,
@RequestParam(value = “goodId”, required = false) Integer goodId,
HttpSession session) {
User user = (User) session.getAttribute(“user”);
if (user == null) {
return “redirect:/”;
Good good = goodService.getGoodById(goodId);
List firstTypes = typeService.getAllFirstType();
List goods = goodService.getAllGoods(0, 5);
List goodImages = imageService.getImageByGoodId(goodId);
model.addAttribute(“goods”, goods);
model.addAttribute(“good”, good);
model.addAttribute(“goodImages”, goodImages);
model.addAttribute(“firstTypes”, firstTypes);
return “goods/userGoodEdit”;
@RequestMapping(value = “/goods/userGoodEdit”, method = RequestMethod.POST)
public String postGoodEdit(ModelMap model, HttpSession session,
@Valid Good good) {
List firstTypes = typeService.getAllFirstType();
User user = (User) session.getAttribute(“user”);
if (user == null) {
return “redirect:/”;
List goods = goodService.getAllGoods(0, 5);
if (!(goodService.updateGood(good) > 0)) {
System.out.println(“修改物品失败!”);
List goodImages = imageService.getImageByGoodId(good.getId());
model.addAttribute(“goods”, goods);
model.addAttribute(“good”, good);
model.addAttribute(“goodImages”, goodImages);
model.addAttribute(“firstTypes”, firstTypes);
return “goods/userGoodEdit”;
@RequestMapping(value = “/goods/userGoodEdit/updateImage”, method = RequestMethod.POST)
public String updateImage(
HttpSession session,
@RequestParam(value = “goodId”, required = false) Integer goodId,
@RequestParam(value = “mainFile”, required = false) MultipartFile mainFile,
@RequestParam(value = “file”, required = false) MultipartFile[] file)
throws IOException {
User user = (User) session.getAttribute(“user”);
FileCheck fileCheck = new FileCheck();
imageService.deleteImage(goodId);
RandomString randomString = new RandomString();
String filePath = “/statics/image/goods/” + user.getId() + “/” + goodId;
String pathRoot = fileCheck.checkGoodFolderExist(filePath);
String name;
if (!mainFile.isEmpty()) {
String contentType = mainFile.getContentType();
String fileName = goodId + randomString.getRandomString(10);
String imageName = contentType
.substring(contentType.indexOf(“/”) + 1);
name = fileName + “.” + imageName;
mainFile.transferTo(new File(pathRoot + name));
String photoUrl = filePath + “/” + name;
goodService.updateGoodPhotoUrl(photoUrl, goodId);
for (MultipartFile mf : file) {
if (!mf.isEmpty()) {
String contentType = mf.getContentType();
String fileName = goodId + randomString.getRandomString(10);
String imageName = contentType.substring(contentType
.indexOf(“/”) + 1);
name = fileName + “.” + imageName;
System.out.println(“name:” + name);
mf.transferTo(new File(pathRoot + name));
Image image = new Image();
image.setGoodId(goodId);
image.setName(name);
image.setUrl(filePath + “/” + name);
imageService.insertImage(image);
return “redirect:/goods/goodInfo?goodId=” + goodId;
@RequestMapping(value = “/goods/userGoodEdit/delete/{goodId}”, method = RequestMethod.GET)
public ResponseEntity deleteGood(@PathVariable Integer goodId) {
Boolean success;
success = goodService.deleteGood(goodId) > 0;
return ResponseEntity.ok(success);
@RequestMapping(value = “/goods/userGoodEdit/updateGoodStatus/{goodId}”, method = RequestMethod.GET)
public ResponseEntity updateGoodStatus(@PathVariable Integer goodId) {
Boolean success;
success = goodService.updateGoodStatusId(0, goodId) > 0;
return ResponseEntity.ok(success);
@RequestMapping(value = “/admin/goods/allGoods”, method = RequestMethod.GET)
public ResponseEntity adminGetAllGoods() {
List goodList = goodService.getAllGoodList();
for (Good good : goodList) {
good.setGoodUser(userService.getUserById(good.getUserId()));
good.setGoodSecondType(typeService.getSecondTypeById(good
.getSecondTypeId()));
return ResponseEntity.ok(goodList);
订单控制层:
@Controller
public class OrderController {
private final GoodService goodService;
private final OrderService orderService;
@Autowired
public OrderController(GoodService goodService, OrderService orderService) {
this.goodService = goodService;
this.orderService = orderService;
@RequestMapping(value = “/user/orderInfo”, method = RequestMethod.GET)
public String getOrderInfo(ModelMap model,
@RequestParam(value = “orderId”, required = false) Integer orderId,
HttpSession session) {
User sessionUser = (User) session.getAttribute(“user”);
if (sessionUser == null) {
return “redirect:/”;
Order orderInfo = orderService.getOrderById(orderId);
List orders = orderService.getOtherOrderByCustomerId(
sessionUser.getId(), orderId);
model.addAttribute(“orderInfo”, orderInfo);
model.addAttribute(“orders”, orders);
return “user/orderInfo”;
@RequestMapping(value = “/user/sellerInfo”, method = RequestMethod.GET)
public String getSellerInfo(ModelMap model,
@RequestParam(value = “orderId”, required = false) Integer orderId,
HttpSession session) {
User sessionUser = (User) session.getAttribute(“user”);
if (sessionUser == null) {
return “redirect:/”;
Order orderInfo = orderService.getOrderById(orderId);
List orders = orderService.getOtherOrderBySellerId(
sessionUser.getId(), orderId);
model.addAttribute(“orderInfo”, orderInfo);
model.addAttribute(“orders”, orders);
System.out.println(“sellerInfo.size:” + orders.size());
return “user/sellerInfo”;
@RequestMapping(value = “/user/order/delete/{orderId}”, method = RequestMethod.GET)
public ResponseEntity deleteOrderById(@PathVariable Integer orderId) {
Boolean success;
success = orderService.deleteOrderById(orderId) > 0;
return ResponseEntity.ok(success);
@RequestMapping(value = “/user/sellerOrder/delete/{orderId}&{goodId}”, method = RequestMethod.GET)
public ResponseEntity deleteSellerOrderById(@PathVariable Integer orderId,
@PathVariable Integer goodId) {
Boolean success;
success = goodService.updateGoodStatusId(1, goodId) > 0;
if (success) {
success = orderService.deleteOrderById(orderId) > 0;
return ResponseEntity.ok(success);
@RequestMapping(value = “/user/order/update/status/{orderId}&{statusId}”, method = RequestMethod.GET)
public ResponseEntity updateOrderStatus(@PathVariable Integer orderId,
@PathVariable Integer statusId) {
Boolean success = orderService.updateStatus(statusId, orderId) > 0;
if (success) {
Order order = orderService.getOrderById(orderId);
return ResponseEntity.ok(order);
return ResponseEntity.ok(success);
@RequestMapping(value = “/user/order/create”, method = RequestMethod.POST)
public ResponseEntity createOrder(@RequestBody Order order) {
Boolean success = orderService.insertOrder(order) > 0;
if (success) {
success = goodService.updateGoodStatusId(0, order.getGoodId()) > 0;
if (success) {
return ResponseEntity.ok(order.getId());
} else {
orderService.deleteOrderById(order.getId());
return ResponseEntity.ok(success);
return ResponseEntity.ok(success);
@RequestMapping(value = “/user/order/allOrder”, method = RequestMethod.GET)
public ResponseEntity getAllOrders() {
List orderList = orderService.getOrderList();
return ResponseEntity.ok(orderList);