Java项目:JSP校园二手物品交易兼社交网站

127 篇文章 15 订阅
118 篇文章 0 订阅

作者主页:夜未央5788

 简介:Java领域优质创作者、Java项目、学习资料、技术互助

文末获取源码

项目介绍

个人中心,修改个人信息,发布商品,发布物品没有通过可以发送消息,审核商品,收藏夹,登录界面,站内消息,购物车购买成功,首页等功能。

由于本程序规模不大,可供课程设计,毕业设计学习演示之用

环境需要

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.数据库:MySql 5.7版本;

技术栈

HTML+CSS+JavaScript+jsp+mysql

使用说明

1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;

4. 运行项目,输入localhost:8080/login.jsp 登录

运行截图

 

 

 

 

 

 

相关代码 

管理端控制器

@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<Good> goodList = goodService.getAllGoodList();
        for (Good good : goodList) {
            good.setGoodUser(userService.getUserById(good.getUserId()));
            good.setGoodSecondType(typeService.getSecondTypeById(good.getSecondTypeId()));
        }
        List<User> userList = userService.getAllUser();
        List<FirstType> firstTypeList = typeService.getAllFirstType();
        List<Order> 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<User> 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<User> 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<Good> goods = goodService.getGoodsBySearchAndType(searchText, secondTypeId, offset, limit);
        double goodsNum = goodService.getGoodsBySearchAndTypeCount(searchText, secondTypeId);
        List<FirstType> 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<Image> images = imageService.getImageByGoodId(goodId);
        User goodUser = userService.getUserById(goodInfo.getUserId());
        goodInfo.setGoodUser(userService.getUserById(goodInfo.getUserId()));
        goodInfo.setGoodSecondType(typeService.getSecondTypeById(goodInfo.getSecondTypeId()));
        List<Review> reviews = reviewService.gerReviewByGoodId(goodId);
        for (Review review : reviews){
            review.setReplys(reviewService.gerReplyByReviewId(review.getId()));
        }
        List<Good> 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<FirstType> firstTypes = typeService.getAllFirstType();
        List<Good> 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<FirstType> firstTypes = typeService.getAllFirstType();
        User user = (User)session.getAttribute("user");
        List<Good> 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<Good> userGoods = goodService.getGoodStatusByUserId(userId);
        List<Good> 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<FirstType> firstTypes = typeService.getAllFirstType();
        List<Good> goods = goodService.getAllGoods(0, 5);
        List<Image> 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<FirstType> firstTypes = typeService.getAllFirstType();
        User user = (User)session.getAttribute("user");
        if (user == null){
            return "redirect:/";
        }
        List<Good> goods = goodService.getAllGoods(0, 5);
        if (!(goodService.updateGood(good) > 0)){
            System.out.println("修改商品失败!");
        }
        List<Image> 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<Good> 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<Order> 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<Order> 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<Order> orderList = orderService.getOrderList();
        return ResponseEntity.ok(orderList);
    }
}

如果也想学习本系统,下面领取。关注并回复:094jsp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜未央5788

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值