spring boot+mybatis实现用户登录

在之前mybatisplus学习的基础上,完成了用户登录到主页的功能

1.建立数据库

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `role` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'xiaoli', '123', 1);
INSERT INTO `user` VALUES (2, 'xiaoxiong', '123', 1);
INSERT INTO `user` VALUES (3, 'xiaoxiao', '123', 0);

SET FOREIGN_KEY_CHECKS = 1;

2.建立实体类user

package com.cqgcxy.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private int id;
    private String username;
    private String password;
    private int role; // role字段用来表示用户角色,1表示管理员,0表示普通用户
}

3.新建UserMappe继承BaseMapper

package com.cqgcxy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cqgcxy.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

4.UserService使用mybatisplus的seleteOne和insert完成登录注册

package com.cqgcxy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqgcxy.entity.User;
import com.cqgcxy.mapper.UserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public User Login(String username,String password){
        // 使用MyBatis Plus的selectOne方法,封装查询条件为"username = #{username} AND password = #{password}"
        // 并返回查询结果
        return userMapper.selectOne(new QueryWrapper<User>().eq("username", username).eq("password", password));
    }

    public void Insert(String username,String password,int role){
        // 创建User对象,并设置用户名和密码
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);
        // 使用MyBatis Plus的insert方法,将User对象插入到数据库中
        userMapper.insert(user);
    }
}

5.UserController调用service里的方法,完成登录注册跳转逻辑

package com.cqgcxy.controller;

import com.cqgcxy.entity.User;
import com.cqgcxy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;

@Controller
public class UserController {
    @Resource
    UserService userService;

    // 登录页面
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    // 用户登录
    @RequestMapping(value = "/loginIn", method = RequestMethod.POST)
    public String loginIn(@RequestParam String username, @RequestParam String password) {
        // 调用UserService的Login方法进行登录验证
        User user = userService.Login(username, password);

        if (user != null) {
            //1为管理员,哦、0为普通用户
            if (user.getRole() == 1) {
                // 管理员登录成功,跳转到管理员主页
                return "adminIndex";
            } else {
                // 普通用户登录成功,跳转到普通用户网页
                return "index";
            }
        } else {
            // 登录失败,返回错误页面
            return "error";
        }
    }

    // 注册页面
    @RequestMapping("/signup")
    public String signUpPage() {
        return "signup";
    }

    // 用户注册
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String signUp(@RequestParam String username, @RequestParam String password, @RequestParam int role) {
        // 调用UserService的Insert方法进行用户注册
        userService.Insert(username, password, role);

        return "success";
    }
}

成果展示

管理员登陆,跳转到管理员主页

普通用户登录,跳转到主页

注册用户只能注册普通用户,管理员只能数据库手动添加

今天的学习到此为止吧,晚安

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值