作者主页:编程指南针
作者简介:Java领域优质创作者、CSDN博客专家 、掘金特邀作者、多年架构师设计经验、腾讯课堂常驻讲师
主要内容:Java项目、毕业设计、简历模板、学习资料、面试题库、技术互助
文末获取源码
项目编号:BS-PT-066
一,项目简介
本项目基于springboot+vue实现了一个前后端分离模式的校园二手交易平台。校内师生可以在此平台上注册自己的账户,然后发布自己想要处理的二手物品,平台本身不实现在线交易功能,发布的二手物品由买卖双方自行联系进行线下交易。主要实现的功能有用户注册、登陆、发布商品、收藏商品、统计浏览量和收藏量、在线留言、全文检索、管理个人发布的商品和留言等功能。系统功能完整,业务模式清晰,开发结构简单,比较符合目前后端分离模式开发的主流诉求,可以用作毕业设计或课程设计以及期未作业使用。
二,环境介绍
语言环境:Java: jdk1.8
数据库:Mysql: mysql5.7 REDIS缓存数据库
应用服务器:Tomcat: tomcat8.5.31
开发工具:IDEA或eclipse
后台开发技术:springboot+mybatisplus+jdk8+mysql5.6
前后台开发技术:VUE+ElementsUI
三,系统展示
用户登陆注册
发布二手物品
查看详情
查看我的收藏
查看我发布的商品:可以进行商品上下架 和删除操作
管理我发布的留言
四,核心代码展示
package cn.fleamarket.controller;
import cn.fleamarket.common.R;
import cn.fleamarket.domain.Favorites;
import cn.fleamarket.domain.User;
import cn.fleamarket.service.FavoritesService;
import cn.fleamarket.service.UserService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* 产品收藏表
*
* @author znz
* @date 2022-09-12 10:46:22
*/
@RestController
@RequestMapping("/favorites")
public class FavoritesController {
@Autowired
private FavoritesService favoritesService;
@Autowired
UserService userService;
@PostMapping(value = "/favoriteList", produces = "application/json")
@ApiOperation("收藏分页查询列表,入参是page:第几页,number:每页几条")
public R<List<Favorites>> favoritesList(@RequestBody Map<String,Object> params) {
if (Objects.isNull(params)) {
return R.error("参数错误!");
}
User nowUser = userService.qureyByUserName(params.getOrDefault("username","").toString());
if (Objects.isNull(nowUser)) {
return R.error("用户未登录!");
}
params.put("userId",nowUser.getId());
Page<Favorites> favoritesPage = favoritesService.selectListPage(params);
List<Favorites> data = favoritesPage
.getRecords().stream()
.filter(distinctByKey(Favorites::getProductId))
.collect(Collectors.toList());
return R.pageBuild(favoritesPage.getTotal(),
favoritesPage.hasNext(),
favoritesPage.hasPrevious(), data);
}
@PostMapping(value = "/addFavorites", produces = "application/json")
@ApiOperation("添加收藏,pid:商品id")
public JSONObject addFavorites(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) {
JSONObject ret = new JSONObject();
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
List<Favorites> list = favoritesService.selectByUid(user.getId());
if (list.size() != 0) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getProductId().equals(jsonObject.getString("pid"))) {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "添加失败");
return ret;
}
}
}
if (user != null) {
String productId = jsonObject.getString("pid");
Integer state = jsonObject.getInteger("state");
Favorites favorites = new Favorites();
favorites.setId(StringTool.getUUID());
favorites.setUserId(user.getId());
favorites.setProductId(productId);
favorites.setCreateTime(new Date());
favorites.setState(state);
Integer isS = favoritesService.addFavorites(favorites);
if (isS > 0) {
ret.put("data", true);
ret.put("code", 0);
ret.put("msg", "添加成功");
} else {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "添加失败");
}
} else {
ret.put("msg", "用户未登录");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "未知错误");
}
return ret;
}
@PostMapping(value = "/deleteFavorites", produces = "application/json")
@ApiOperation("删除收藏,fid:收藏id")
public JSONObject deleteFavorites(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) {
JSONObject ret = new JSONObject();
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
if (user != null) {
String fid = jsonObject.getString("fid");
Integer isS = favoritesService.deleteFavorites(fid);
if (isS > 0) {
ret.put("code", 0);
ret.put("data", true);
ret.put("msg", "删除成功");
}
} else {
ret.put("msg", "用户未登录");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "删除失败");
}
return ret;
}
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
package cn.fleamarket.controller;
import cn.fleamarket.common.R;
import cn.fleamarket.config.PathConfig;
import cn.fleamarket.domain.Image;
import cn.fleamarket.service.ImageService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.Objects;
/**
* 图片表
*
* @author znz
* @date 2022-09-12 10:46:22
*/
@RestController
@RequestMapping("/image")
@Api("图片接口")
@CrossOrigin
public class ImageController {
@Autowired
ImageService imageService;
@Autowired
private PathConfig pathConfig;
/**
* 上传img
*
* @param file 文件
* @return {@link JSONObject}
*/
@SneakyThrows
@PostMapping("/uploadImg")
@ApiOperation("图片上传")
public R<String> uploadImg(MultipartFile file) {
JSONObject ret = new JSONObject();
String fileName = file.getOriginalFilename();
String newFileName = StringTool.getUUID() + Objects.requireNonNull(fileName).substring(fileName.indexOf("."));
Image image = new Image();
File file1;
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
file1 = new File(pathConfig.getWinPath(), newFileName);
} else {
file1 = new File(pathConfig.getWinPath(), newFileName);
}
if (!file1.exists()) {
System.out.println(file1.mkdir());
}
file.transferTo(file1);
image.setId(StringTool.getUUID());
image.setImgUrl(newFileName);
imageService.insert(image);
return R.ok(0,"图片上传成功",File.separator + "static" + File.separator + "img" + File.separator + image.getImgUrl());
}
}
package cn.fleamarket.controller;
import cn.fleamarket.domain.Message;
import cn.fleamarket.domain.User;
import cn.fleamarket.service.MessageService;
import cn.fleamarket.service.UserService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 留言接口
*/
@RestController
@RequestMapping("/message")
@Api("留言接口")
public class MessageController {
@Autowired
MessageService messageService;
@Autowired
UserService userService;
@PostMapping(value = "/messageList", produces = "application/json")
@ApiOperation("分页查询留言列表,入参是page:第几页,number:每页几条,pId:属于哪个商品的id")
public JSONObject messageList(@RequestBody JSONObject jsonObject) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
String pId = jsonObject.getString("pId");
Map<String, Object> map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("pId", pId);
if (page != null && number != null) {
Page<Message> messagePage = messageService.selectListPage(map);
List<Message> messagesList = messagePage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(messagesList));
ret.put("total", messagePage.getTotal());//总数
ret.put("next", messagePage.hasNext());//下一页
ret.put("previous", messagePage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping("/addMessage")
@ApiOperation("新增留言接口,text:留言内容,tid:发送人(不用填),fid:接受人(填商品id)")
public JSONObject addMessage(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
Message message = new Message();
try {
User user = userService.qureyByUserName(jsonObject.getString("username"));
message.setTid(user.getId());
message.setTime(new Date());
message.setId(StringTool.getUUID());
message.setFid(jsonObject.getString("fid"));
message.setText(jsonObject.getString("text"));
if (messageService.addMessage(message) > 0) {
ret.put("code", 0);
ret.put("data", true);
ret.put("msg", "新增留言成功");
} else {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "新增留言失败");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "新增留言失败");
}
return ret;
}
@PostMapping(value = "/messageListByUser", produces = "application/json")
@ApiOperation("分页查询我的留言,入参是page:第几页,number:每页几条")
public JSONObject productListByUser(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
Map<String, Object> map = new HashMap<>();
User user = userService.qureyByUserName(jsonObject.getString("username"));
map.put("page", page);
map.put("number", number);
map.put("userId", user.getId());
if (page != null && number != null) {
Page<Message> messagePage = messageService.selectListPageByUser(map);
List<Message> messageList = messagePage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(messageList));
ret.put("total", messagePage.getTotal());//总数
ret.put("next", messagePage.hasNext());//下一页
ret.put("previous", messagePage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
e.printStackTrace();
}
return ret;
}
@PostMapping("/delete")
@ApiOperation("删除留言接口,主要传留言id即可")
public JSONObject delete(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
String pId = jsonObject.getString("id");
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
int i = messageService.delete(user.getId(), pId);
if (i > 0) {
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "删除留言成功");
} else {
ret.put("code", "-1");
ret.put("data", false) ;
ret.put("msg", "删除留言失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "删除留言失败");
e.printStackTrace();
}
return ret;
}
}
package cn.fleamarket.controller;
import java.util.*;
import cn.fleamarket.domain.Product;
import cn.fleamarket.domain.User;
import cn.fleamarket.service.MessageService;
import cn.fleamarket.service.ProductService;
import cn.fleamarket.service.UserService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* 商品表
*
* @author znz
* @email ${email}
* @date 2022-09-12 10:46:22
*/
@RestController
@RequestMapping("/product")
@Api("商品接口")
public class ProductController {
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
MessageService messageService;
@PostMapping(value = "/productList", produces = "application/json")
@ApiOperation("分页查询列表,入参是page:第几页,number:每页几条,key:查询条件(可选)")
public JSONObject productList(@RequestBody JSONObject jsonObject) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
String key = jsonObject.getString("key");
JSONArray jsonArray = jsonObject.getJSONArray("pId");
Map<String, Object> map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("key", key);
if (page != null && number != null) {
Page<Product> productPage = productService.selectListPage(map);
List<Product> productList = productPage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("total", productPage.getTotal());//总数
ret.put("next", productPage.hasNext());//下一页
ret.put("previous", productPage.hasPrevious());//上一页
ret.put("msg", "查询成功");
return ret;
}
if (jsonArray.size() != 0 && jsonArray != null) {
List<Product> list = new ArrayList<>();
for (int i = 0; i < jsonArray.size(); i++) {
Product product = productService.selectById(jsonArray.get(i).toString());
list.add(product);
}
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(list));
ret.put("msg", "查询成功");
return ret;
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping(value = "/productListByUser", produces = "application/json")
@ApiOperation("分页查询属于某个用户的商品列表,就是我发布的商品,入参是page:第几页,number:每页几条")
public JSONObject productListByUser(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
Map<String, Object> map = new HashMap<>();
User user = userService.qureyByUserName(jsonObject.getString("username"));
map.put("page", page);
map.put("number", number);
map.put("userId", user.getId());
if (page != null && number != null) {
Page<Product> productPage = productService.selectListPageByUser(map);
List<Product> productList = productPage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("total", productPage.getTotal());//总数
ret.put("next", productPage.hasNext());//下一页
ret.put("previous", productPage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping(value = "/productListById", produces = "application/json")
@ApiOperation("分页查询属于某个用户的商品列表,就是我发布的商品,入参是page:第几页,number:每页几条")
public JSONObject productListById(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
Object[] pIds = jsonObject.getJSONArray("pId").toArray();
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < pIds.length; i++) {
if (!list.contains(pIds[i])) {
list.add(pIds[i]);
}
}
Object[] pIdss = list.toArray();
Map<String, Object> map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("pId", pIdss);
if (page != null && number != null && pIds != null) {
Page<Product> productPage = productService.selectListsPageById(map);
List<Product> productList = productPage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("total", productPage.getTotal());//总数
ret.put("next", productPage.hasNext());//下一页
ret.put("previous", productPage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping(value = "/productListByIds", produces = "application/json")
@ApiOperation("分页查询属于某个用户的商品列表,就是我发布的商品,入参是page:第几页,number:每页几条")
public JSONObject productListByIds(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Object[] pIds = jsonObject.getJSONArray("pId").toArray();
List<Product> productList = new ArrayList<>();
for (int i = 0; i < pIds.length; i++) {
Product product = productService.selectById(pIds[i].toString());
productList.add(product);
}
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("msg", "查询成功");
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PutMapping("/addProduct")
@ApiOperation("增加商品,入参是要增加的商品信息,记得带上上传图片接口返回的url")
public JSONObject addProduct(@RequestBody JSONObject par, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Product product = new Product();
product.setId(StringTool.getUUID());
product.setCreateTime(new Date());
product.setBprice(par.getDouble("bprice"));
product.setTitle(par.getString("title"));
product.setImgUrl(par.getString("imgUrl"));
product.setPrice(par.getDouble("price"));
product.setContent(par.getString("content"));
User user = userService.qureyByUserName(par.getString("username"));
product.setUserId(user.getId());
int i = productService.insert(product);
if (i > 0) {
ret.put("code", 0);
ret.put("data", true);
ret.put("msg", "增加成功");
} else {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "增加失败");
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "增加失败");
}
return ret;
}
@PutMapping("/update")
@ApiOperation("修改商品信息,传入要修改的信息,主要是商品id")
public JSONObject update(Product product) {
JSONObject ret = new JSONObject();
product.setCreateTime(new Date());
try {
if (product.getId() != null && productService.update(product) > 0) {
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "修改成功");
} else {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "修改失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "修改失败");
e.printStackTrace();
}
return ret;
}
@PutMapping("/obtained")
@ApiOperation("上架下架接口,主要传id即可")
public JSONObject Obtained(@RequestBody JSONObject id) {
JSONObject ret = new JSONObject();
try {
String dbId = id.getString("id");
Product product = productService.selectById(dbId);
if (product != null && product.getIsShow() == 1) {
productService.updateById(dbId, true);
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "下架成功");
} else if (product != null && product.getIsShow() == 0) {
productService.updateById(dbId, false);
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "上架成功");
} else {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "修改失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "修改失败");
e.printStackTrace();
}
return ret;
}
@GetMapping("/selectById")
@ApiOperation("查询商品详情,传id即可,id是个字符串")
public JSONObject selectById(String id) {
JSONObject ret = new JSONObject();
try {
Product product = productService.selectById(id);
if (product != null) {
ret.put("code", "0");
ret.put("data", StringTool.ObjectToJSONObject(product));
ret.put("msg", "查询商品详情成功");
} else {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "查询商品详情失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "查询商品详情失败");
e.printStackTrace();
}
return ret;
}
@PutMapping("/addCount")
@ApiOperation("增加浏览次数接口,主要传id即可,当用户打开商品页的时候发这个请求")
public JSONObject Increase(@RequestBody JSONObject id) {
JSONObject ret = new JSONObject();
try {
String dbId = id.getString("id");
Product product = productService.selectById(dbId);
if (product != null) {
productService.updateIncrease(product);
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "增加浏览次数成功");
} else {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "增加浏览次数失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "增加浏览次数失败");
e.printStackTrace();
}
return ret;
}
@PostMapping("/delete")
@ApiOperation("删除商品接口,主要传商品id即可")
public JSONObject delete(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
String pId = jsonObject.getString("id");
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
int i = productService.delete(user.getId(), pId);
if (i > 0) {
if(messageService.deletebyFid(pId)>0) {
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "删除商品成功");
}
} else {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "删除商品失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "删除商品失败");
e.printStackTrace();
}
return ret;
}
}