用户管理中心——数据库设计&用户注册逻辑设计
规整项目目录
utils–存放工具类,比如加密转换等
1. 数据库自动生成器的使用
实现基本的数据库操作(操作user表)
模型 user 对象 => 和数据库关联,自动生成
【操作user表中的数据,从中查询数据,(在model中)先创建一个user对象,和数据库字段关联】
使用mybatis-plus自动生成
使用插件MyBatisX
,自动根据数据库生成:
代码迁移,创建测试类
安装一个插件
在创建的对象上按Alt + Enter,可以生成调用这个对象所有的set方法。
ctrl+P :可以查看这个方法要用哪些参数
最后的测试类:
2. 注册逻辑的设计
(1) 写注册逻辑
(2) 实现
java"> @Overridepublic 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) 测试代码
java">@Testvoid 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. 遇到的问题
- 问题: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官网配置文件,发现自动将下划线映射为驼峰
解决: 修改配置文件
- 如何解决idea插件安装过慢?
两步带你解决IDEA 插件下载安装慢、超时、不成功问题