用户管理中心——数据库设计&用户注册逻辑设计

规整项目目录

在这里插入图片描述
utils–存放工具类,比如加密转换等

1. 数据库自动生成器的使用

在这里插入图片描述

实现基本的数据库操作(操作user表)

模型 user 对象 => 和数据库关联,自动生成

【操作user表中的数据,从中查询数据,(在model中)先创建一个user对象,和数据库字段关联】

使用mybatis-plus自动生成
使用插件MyBatisX ,自动根据数据库生成:
在这里插入图片描述

在这里插入图片描述

代码迁移,创建测试类

在这里插入图片描述
在这里插入图片描述
安装一个插件
在这里插入图片描述

在创建的对象上按Alt + Enter,可以生成调用这个对象所有的set方法。

在这里插入图片描述
ctrl+P :可以查看这个方法要用哪些参数

最后的测试类:
在这里插入图片描述

2. 注册逻辑的设计

(1) 写注册逻辑

在这里插入图片描述

(2) 实现

 @Override
    public long userRegister(String userAccount, String userPassword, String checkPassword) {
        // 1. 校验
        // if(userAccount == null || userPassword == null || checkPassword == null)
        if(StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)){
            return -1;
        }
        if(userAccount.length() < 4){
            return -1;
        }
        if(userPassword.length() < 8 || checkPassword.length() < 8){
            return -1;
        }

        // 账户不能包含特殊字符
        String validPattern = "\\pP|\\pS|\\s+";
        Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
        if(matcher.find()){
            return -1;
        }
        // 密码与校验密码相同
        if(!userPassword.equals(checkPassword)){
            return -1;
        }

        // 账户不能重复——从数据库中查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount", userAccount);
        long count = userMapper.selectCount(queryWrapper);
        if(count > 0){
            return -1;
        }
        // 2. 加密
        final String SALT = "yupi";
        String newPassword = DigestUtils.md5DigestAsHex((SALT + "mypassword").getBytes());

        // 3. 插入数据
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserPassword(userPassword);
        boolean saveResult = this.save(user);
        if(!saveResult){
            return -1;
        }

        return user.getId();
    }

(3) 测试代码

@Test
    void userRegister() {
        // 非空
        String userAccount = "hhhhh";
        String userPassword = "";
        String checkPassword = "12345678";
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertEquals(-1, result);
        // 账号长度不小于4位
        userAccount = "yu";
        result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertEquals(-1, result);
        // 密码不小于8位
        userAccount = "hhhhh";
        userPassword = "123456";
        result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertEquals(-1, result);
        // 账户不能重复
        userAccount = "yupi";
        userPassword = "12345678";
        result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertEquals(-1, result);
        // 账户不包含特殊字符
        userAccount = "yu pi";
        userPassword = "12345678";
        result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertEquals(-1, result);
        // 密码和校验密码不同
        userAccount = "hhhhh";
        userPassword = "12345678";
        checkPassword = "123456789";
        result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertEquals(-1, result);
        checkPassword = "12345678";
        result = userService.userRegister(userAccount, userPassword, checkPassword);
        Assertions.assertTrue(result > 0);


    }

3. 遇到的问题

  1. 问题:Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘user_account’ in ‘field list’
    ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Unknown column ‘user_account’ in ‘field list’

在这里插入图片描述

原因:查找mybatis-plus官网配置文件,发现自动将下划线映射为驼峰

在这里插入图片描述
解决: 修改配置文件
在这里插入图片描述

  1. 如何解决idea插件安装过慢?
    两步带你解决IDEA 插件下载安装慢、超时、不成功问题
    在这里插入图片描述

相关推荐

  1. 三、 mariadb数据库用户管理

    2024-05-12 18:06:07       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 18:06:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 18:06:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 18:06:07       82 阅读
  4. Python语言-面向对象

    2024-05-12 18:06:07       91 阅读

热门阅读

  1. C++ 利用标准库多字节转宽字节字符

    2024-05-12 18:06:07       28 阅读
  2. LeetCode //C - 87. Scramble String

    2024-05-12 18:06:07       30 阅读
  3. 马尔可夫链 学习笔记

    2024-05-12 18:06:07       30 阅读
  4. timestamp和datetime的区别

    2024-05-12 18:06:07       31 阅读
  5. 若依前端分离版-APP(UNI APP)表单添加验证

    2024-05-12 18:06:07       28 阅读
  6. GDB 使用python

    2024-05-12 18:06:07       36 阅读
  7. sql中的lag()和lead()是什么意思

    2024-05-12 18:06:07       36 阅读
  8. Go语言基础知识学习

    2024-05-12 18:06:07       26 阅读