源码获取:博客首页 "资源" 里下载!
本次设计的是一个校园二手交易平台(C2C),C2C指个人与个人之间的电子商务,买家可以查看所有卖家发布的商品,并且根据分类进行商品过滤,也可以根据站内搜索引擎进行商品的查询,并且与卖家联系,达成交易的意向,也可以发布求购的信息,让卖家查看之后,与之联系,进行交易。而此次主要是针对校园用户所设计的网站,对于数据的分类应该更多的考虑校园用户的需求,例如二手书籍、二手数码等的分类应该更加细致。由于是C2C的项目,因此本项目无需后台管理。
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. 前端:Thymeleaf+Html+jQuery
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行;
3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/xxx 登录
@Controller
@RequestMapping(value = "user")
public class UserController {
private final GoodService goodService;
private final OrderService orderService;
private final ReviewService reviewService;
private final UserService userService;
private final CollectService collectService;
@Autowired
public UserController(GoodService goodService, OrderService orderService,
ReviewService reviewService, UserService userService,
CollectService collectService) {
this.goodService = goodService;
this.orderService = orderService;
this.reviewService = reviewService;
this.userService = userService;
this.collectService = collectService;
}
@RequestMapping(value = "userProfile", method = RequestMethod.GET)
public String getMyProfile(ModelMap model, HttpSession session) {
User user = (User) session.getAttribute("user");
if (user == null) {
return "redirect:/";
}
List collects = collectService
.getCollectByUserId(user.getId());
for (Collect collect : collects) {
collect.setGood(goodService.getGoodById(collect.getGoodId()));
}
List goods = goodService.getGoodByUserId(user.getId());
List orders = orderService.getOrderByCustomerId(user.getId());
List reviews = reviewService.gerReviewByToUserId(user.getId());
List replies = reviewService.gerReplyByToUserId(user.getId());
List sellGoods = orderService.getOrderBySellerId(user.getId());
model.addAttribute("collects", collects);
model.addAttribute("goods", goods);
model.addAttribute("orders", orders);
model.addAttribute("reviews", reviews);
model.addAttribute("replies", replies);
model.addAttribute("sellGoods", sellGoods);
return "user/userProfile";
}
@RequestMapping(value = "/review", method = RequestMethod.GET)
public String getReviewInfo(@RequestParam(required = false) Integer goodId,
@RequestParam(required = false) Integer reviewId) {
System.out.println("reviewId" + reviewId);
if (reviewId != null) {
System.out.println("reviewId" + reviewId);
if (reviewService.updateReviewStatus(1, reviewId) == 1) {
return "redirect:/goods/goodInfo?goodId=" + goodId;
}
}
return "redirect:/user/userProfile";
}
@RequestMapping(value = "/reply", method = RequestMethod.GET)
public String getReplyInfo(
@RequestParam(required = false) Integer reviewId,
@RequestParam(required = false) Integer replyId) {
if (replyId != null) {
if (reviewService.updateReplyStatus(1, replyId) == 1) {
Integer goodId = reviewService.getGoodIdByReviewId(reviewId);
return "redirect:/goods/goodInfo?goodId=" + goodId;
}
}
return "redirect:/user/userProfile";
}
@RequestMapping(value = "/userEdit", method = RequestMethod.GET)
public String getUserEdit(ModelMap model,
@RequestParam(value = "userId", required = false) Integer userId,
HttpSession session) {
User sessionUser = (User) session.getAttribute("user");
if (sessionUser == null) {
return "redirect:/";
}
User user = userService.getUserById(userId);
List sellGoods = orderService.getOrderBySellerId(user.getId());
List reviews = reviewService.gerReviewByToUserId(user.getId());
List replies = reviewService.gerReplyByToUserId(user.getId());
model.addAttribute("user", user);
model.addAttribute("sellGoods", sellGoods);
model.addAttribute("reviews", reviews);
model.addAttribute("replies", replies);
return "user/userEdit";
}
@RequestMapping(value = "/userEdit", method = RequestMethod.POST)
public String postUserEdit(ModelMap model, @Valid User user,
HttpSession session,
@RequestParam(value = "photo", required = false) MultipartFile photo)
throws IOException {
String status;
Boolean insertSuccess;
User sessionUser = (User) session.getAttribute("user");
user.setId(sessionUser.getId());
InfoCheck infoCheck = new InfoCheck();
if (!infoCheck.isMobile(user.getMobile())) {
status = "请输入正确的手机号!";
} else if (!infoCheck.isEmail(user.getEmail())) {
status = "请输入正确的邮箱!";
} else if (userService.getUserByMobile(user.getMobile()).getId() != user
.getId()) {
System.out.println(userService.getUserByMobile(user.getMobile())
.getId() + " " + user.getId());
status = "此手机号码已使用!";
} else if (userService.getUserByEmail(user.getEmail()).getId() != user
.getId()) {
status = "此邮箱已使用!";
} else {
if (!photo.isEmpty()) {
RandomString randomString = new RandomString();
FileCheck fileCheck = new FileCheck();
String filePath = "/statics/image/photos/" + user.getId();
String pathRoot = fileCheck.checkGoodFolderExist(filePath);
String fileName = user.getId()
+ randomString.getRandomString(10);
String contentType = photo.getContentType();
String imageName = contentType.substring(contentType
.indexOf("/") + 1);
String name = fileName + "." + imageName;
photo.transferTo(new File(pathRoot + name));
String photoUrl = filePath + "/" + name;
user.setPhotoUrl(photoUrl);
} else {
String photoUrl = userService.getUserById(user.getId())
.getPhotoUrl();
user.setPhotoUrl(photoUrl);
}
insertSuccess = userService.updateUser(user);
if (insertSuccess) {
session.removeAttribute("user");
session.setAttribute("user", user);
return "redirect:/user/userProfile";
} else {
status = "修改失败!";
model.addAttribute("user", user);
model.addAttribute("status", status);
return "user/userEdit";
}
}
System.out.println(user.getMobile());
System.out.println(status);
model.addAttribute("user", user);
model.addAttribute("status", status);
return "user/userEdit";
}
@RequestMapping(value = "/password/edit", method = RequestMethod.POST)
public ResponseEntity editPassword(@RequestBody Password password) {
User user = userService.getUserById(password.getUserId());
String oldPass = DigestUtils
.md5DigestAsHex((password.getOldPassword() + user.getCode())
.getBytes());
if (oldPass.equals(user.getPassword())) {
RandomString randomString = new RandomString();
String code = (randomString.getRandomString(5));
String md5Pass = DigestUtils.md5DigestAsHex((password
.getNewPassword() + code).getBytes());
Boolean success = userService.updatePassword(md5Pass, code,
password.getUserId());
if (success) {
return ResponseEntity.ok(true);
} else {
return ResponseEntity.ok("密码修改失败!");
}
} else {
return ResponseEntity.ok("原密码输入不正确!");
}
}
}
@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);
}
}
@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);
}
}
源码获取:博客首页 "资源" 里下载!