SpringBoot商城系统

本文介绍了使用SpringBoot、MySQL和MyBatis开发的商城系统,展示了登录验证、偏好设置、登出及用户状态检查的控制器代码。系统演示地址提供了一个实战案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意:本商城系统无后台管理功能和页面
技术架构:
springboot+mysql+mybatis+html+css+js
部分代码展示:
登录功能:

package com.moon.tmail.controller;

import com.moon.tmail.pojo.Common;
import com.moon.tmail.pojo.Prefer;
import com.moon.tmail.pojo.TabUser;
import com.moon.tmail.service.TabUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PathVariable;
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.HttpSession;
import java.util.List;

@RestController
@RequestMapping("user")
public class TabLoginController {
    @Autowired
    private TabUserService tabUserService;
    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(6);

    @RequestMapping("login")
    public boolean login(@RequestBody TabUser user, HttpSession httpsession) {
        TabUser res = tabUserService.findbyphone(user.getPhone());
        if (res == null) {
            return false;
        }
        if (passwordEncoder.matches(user.getPassword(), res.getPassword())) {
            httpsession.setAttribute("userId", res.getId());
            httpsession.setAttribute("nickname", res.getNickname());
            httpsession.setAttribute("phone", res.getPhone());
            httpsession.setAttribute("address",res.getAddress());
            return true;
        }
        return false;
    }

    @RequestMapping("checkPrefer")
    public boolean prefer(HttpSession httpSession) {
        int userId = Integer.parseInt(httpSession.getAttribute("userId").toString());
        List<Prefer> prefers = tabUserService.findbyuserId(userId);
        if (prefers == null || prefers.size() == 0) {
            return true;
        }
        return false;
    }

    @RequestMapping("logout")
    public boolean logout(HttpSession httpSession) {
        httpSession.invalidate();
        return true;
    }

    @RequestMapping("checkLogin")
    public boolean isLogin(HttpSession httpSession) {
        if (httpSession.getAttribute("phone") == null || "".equals(httpSession.getAttribute("phone"))) {
            return false;
        }
        return true;
    }
    @RequestMapping("check/{phone}")
    public boolean isExist(@PathVariable String phone){
        Long p = Long.parseLong(phone);
        if(tabUserService.findbyphone(p)==null){
            return true;
        };
        return false;
    }
    @RequestMapping("register")
    public boolean register(@RequestBody TabUser user){
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setIsEmp(false);
        int i = tabUserService.add(user);
        if(i!=1) {
            return false;
        }
        return true;
    }
    @RequestMapping("addPrefer")
    public void addPrefer(@RequestBody List<Common> categories,HttpSession httpSession){
        Integer i = (Integer) httpSession.getAttribute("userId");
        for(Common c:categories){
            Prefer prefer = new Prefer();
            prefer.setUserId(i);
            prefer.setCategoryId(c.getCategoryId());
            tabUserService.addPrefer(prefer);
        }
    }

}

系统演示地址:
链接:https://pan.baidu.com/s/1rpqBqlh08b2wahi1DnCk-Q
提取码:7q7q

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值